aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/util/generationhandler/generationhandler_test.cpp
blob: 0bc72f93a9d3285506a8ee5a6c8465a9c8e7c9b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/generationhandler.h>
#include <deque>

namespace vespalib {

using GenGuard = GenerationHandler::Guard;

class GenerationHandlerTest : public ::testing::Test {
protected:
    GenerationHandler gh;
    GenerationHandlerTest();
    ~GenerationHandlerTest() override;
};

GenerationHandlerTest::GenerationHandlerTest()
    : ::testing::Test(),
      gh()
{
}

GenerationHandlerTest::~GenerationHandlerTest() = default;

TEST_F(GenerationHandlerTest, require_that_generation_can_be_increased)
{
    EXPECT_EQ(0u, gh.getCurrentGeneration());
    EXPECT_EQ(0u, gh.get_oldest_used_generation());
    gh.incGeneration();
    EXPECT_EQ(1u, gh.getCurrentGeneration());
    EXPECT_EQ(1u, gh.get_oldest_used_generation());
}

TEST_F(GenerationHandlerTest, require_that_readers_can_take_guards)
{
    EXPECT_EQ(0u, gh.getGenerationRefCount(0));
    {
        GenGuard g1 = gh.takeGuard();
        EXPECT_EQ(1u, gh.getGenerationRefCount(0));
        {
            GenGuard g2 = gh.takeGuard();
            EXPECT_EQ(2u, gh.getGenerationRefCount(0));
            gh.incGeneration();
            {
                GenGuard g3 = gh.takeGuard();
                EXPECT_EQ(2u, gh.getGenerationRefCount(0));
                EXPECT_EQ(1u, gh.getGenerationRefCount(1));
                EXPECT_EQ(3u, gh.getGenerationRefCount());
            }
            EXPECT_EQ(2u, gh.getGenerationRefCount(0));
            EXPECT_EQ(0u, gh.getGenerationRefCount(1));
            gh.incGeneration();
            {
                GenGuard g3 = gh.takeGuard();
                EXPECT_EQ(2u, gh.getGenerationRefCount(0));
                EXPECT_EQ(0u, gh.getGenerationRefCount(1));
                EXPECT_EQ(1u, gh.getGenerationRefCount(2));
            }
            EXPECT_EQ(2u, gh.getGenerationRefCount(0));
            EXPECT_EQ(0u, gh.getGenerationRefCount(1));
            EXPECT_EQ(0u, gh.getGenerationRefCount(2));
        }
        EXPECT_EQ(1u, gh.getGenerationRefCount(0));
        EXPECT_EQ(0u, gh.getGenerationRefCount(1));
        EXPECT_EQ(0u, gh.getGenerationRefCount(2));
    }
    EXPECT_EQ(0u, gh.getGenerationRefCount(0));
    EXPECT_EQ(0u, gh.getGenerationRefCount(1));
    EXPECT_EQ(0u, gh.getGenerationRefCount(2));
}

TEST_F(GenerationHandlerTest, require_that_guards_can_be_copied)
{
    GenGuard g1 = gh.takeGuard();
    EXPECT_EQ(1u, gh.getGenerationRefCount(0));
    GenGuard g2(g1);
    EXPECT_EQ(2u, gh.getGenerationRefCount(0));
    gh.incGeneration();
    GenGuard g3 = gh.takeGuard();
    EXPECT_EQ(2u, gh.getGenerationRefCount(0));
    EXPECT_EQ(1u, gh.getGenerationRefCount(1));
    g3 = g2;
    EXPECT_EQ(3u, gh.getGenerationRefCount(0));
    EXPECT_EQ(0u, gh.getGenerationRefCount(1));
}

TEST_F(GenerationHandlerTest, require_that_the_first_used_generation_is_correct)
{
    EXPECT_EQ(0u, gh.get_oldest_used_generation());
    gh.incGeneration();
    EXPECT_EQ(1u, gh.get_oldest_used_generation());
    {
        GenGuard g1 = gh.takeGuard();
        gh.incGeneration();
        EXPECT_EQ(1u, gh.getGenerationRefCount());
        EXPECT_EQ(1u, gh.get_oldest_used_generation());
    }
    EXPECT_EQ(1u, gh.get_oldest_used_generation());
    gh.update_oldest_used_generation();	// Only writer should call this
    EXPECT_EQ(0u, gh.getGenerationRefCount());
    EXPECT_EQ(2u, gh.get_oldest_used_generation());
    {
        GenGuard g1 = gh.takeGuard();
        gh.incGeneration();
        gh.incGeneration();
        EXPECT_EQ(1u, gh.getGenerationRefCount());
        EXPECT_EQ(2u, gh.get_oldest_used_generation());
        {
            GenGuard g2 = gh.takeGuard();
            EXPECT_EQ(2u, gh.get_oldest_used_generation());
        }
    }
    EXPECT_EQ(2u, gh.get_oldest_used_generation());
    gh.update_oldest_used_generation();	// Only writer should call this
    EXPECT_EQ(0u, gh.getGenerationRefCount());
    EXPECT_EQ(4u, gh.get_oldest_used_generation());
}

TEST_F(GenerationHandlerTest, require_that_generation_can_grow_large)
{
    std::deque<GenGuard> guards;
    for (size_t i = 0; i < 10000; ++i) {
        EXPECT_EQ(i, gh.getCurrentGeneration());
        guards.push_back(gh.takeGuard()); // take guard on current generation
        if (i >= 128) {
            EXPECT_EQ(i - 128, gh.get_oldest_used_generation());
            guards.pop_front();
            EXPECT_EQ(128u, gh.getGenerationRefCount());
        }
        gh.incGeneration();
    }
}

}

GTEST_MAIN_RUN_ALL_TESTS()