aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/util/statefile/statefile_test.cpp
blob: a9f087c773bbd13446b6a7a8aae5931108b8aa74 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/searchlib/util/statefile.h>
#include <atomic>
#include <iostream>
#include <fstream>
#include <string>
#include <vespa/searchlib/test/statefile.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <vespa/log/log.h>
LOG_SETUP("statefile_test");

using namespace search::test::statefile;

namespace search {

namespace {

bool
hasFile(const char *name)
{
    return access(name, R_OK | W_OK) == 0;
}


void
addState(StateFile &sf, const char *buf)
{
    size_t bufLen = strlen(buf);
    sf.addState(buf, bufLen, false);
}

void
addSignalState(StateFile &sf, const char *buf)
{
    size_t bufLen = strlen(buf);
    sf.addState(buf, bufLen, true);
}


bool
assertHistory(std::vector<vespalib::string> &exp,
              std::vector<vespalib::string> &act)
{
    if (!EXPECT_EQUAL(exp.size(), act.size())) {
        return false;
    }
    for (size_t i = 0; i < exp.size(); ++i) {
        if (!EXPECT_EQUAL(exp[i], act[i])) {
            return false;
        }
    }
    return true;
}


int64_t
getSize(const char *name)
{
    struct stat stbuf;
    if (stat(name, &stbuf) != 0)
        return 0;
    return stbuf.st_size;
}


void
setSize(const char *name, int64_t newSize)
{
    int truncRes = truncate(name, newSize);
    (void) truncRes;
    assert(truncRes == 0);
}


}


TEST("Test lock free atomic int used by async signal safe lock primitive")
{
    std::atomic<int> f;
    ASSERT_TRUE(f.is_lock_free());
}


TEST("Test that statefile can be created")
{
    StateFile::erase("state");
    EXPECT_FALSE(hasFile("state"));
    EXPECT_FALSE(hasFile("state.history"));
    StateFile sf("state");
    EXPECT_TRUE(hasFile("state"));
    EXPECT_TRUE(hasFile("state.history"));
    EXPECT_EQUAL(0, sf.getGen());
    StateFile::erase("state");
    EXPECT_FALSE(hasFile("state"));
    EXPECT_FALSE(hasFile("state.history"));
    StateFile::erase("state");
    EXPECT_FALSE(hasFile("state"));
    EXPECT_FALSE(hasFile("state.history"));
}


TEST("Test that statefile can add event")
{
    StateFile::erase("state");
    StateFile sf("state");

    addState(sf, "Hello world\n");
    vespalib::string check = readState(sf);
    EXPECT_EQUAL("Hello world\n", check);
    EXPECT_EQUAL(1, sf.getGen());
}

TEST("Test that history is appended to")
{
    StateFile::erase("state");
    StateFile sf("state");

    addState(sf, "Hello world\n");
    addState(sf, "Foo bar\n");
    vespalib::string check = readState(sf);
    EXPECT_EQUAL("Foo bar\n", check);
    EXPECT_EQUAL(2, sf.getGen());
    {
        std::vector<vespalib::string> exp({ "Hello world\n", "Foo bar\n" });
        std::vector<vespalib::string> act(readHistory("state.history"));
        TEST_DO(assertHistory(exp, act));
    }
}


TEST("Test that truncated history is truncated at event boundary")
{
    StateFile::erase("state");
    int64_t histSize = 1;
    {
        StateFile sf("state");
        addState(sf, "Hello world\n");
        addState(sf, "Foo bar\n");
        EXPECT_EQUAL(2, sf.getGen());
        histSize = getSize("state.history");
        EXPECT_EQUAL(20, histSize);
        addState(sf, "zap\n");
        EXPECT_EQUAL(3, sf.getGen());
    }
    // Lose 2 last events in history
    setSize("state.history", histSize - 1);
    // Last event is restored to history from main state file
    StateFile sf("state");
    vespalib::string check = readState(sf);
    EXPECT_EQUAL("zap\n", check);
    EXPECT_EQUAL(0, sf.getGen());
    {
        std::vector<vespalib::string> exp({ "Hello world\n", "zap\n" });
        std::vector<vespalib::string> act(readHistory("state.history"));
        TEST_DO(assertHistory(exp, act));
    }
}


TEST("Test that async signal safe path adds event")
{
    StateFile::erase("state");
    StateFile sf("state");

    addSignalState(sf, "Hello world\n");
    addSignalState(sf, "Foo bar\n");
    vespalib::string check = readState(sf);
    EXPECT_EQUAL("Foo bar\n", check);
    EXPECT_EQUAL(2, sf.getGen());
    {
        std::vector<vespalib::string> exp({ "Hello world\n", "Foo bar\n" });
        std::vector<vespalib::string> act(readHistory("state.history"));
        TEST_DO(assertHistory(exp, act));
    }
}


TEST("Test that state file can be restored from history")
{
    StateFile::erase("state");
    {
        StateFile sf("state");
        addState(sf, "Hello world\n");
        addState(sf, "Foo bar\n");
        EXPECT_EQUAL(2, sf.getGen());
    }
    // Lose event in main state file
    setSize("state", 0);
    EXPECT_EQUAL(0, getSize("state"));
    // Last event is restored to history from main state file
    StateFile sf("state");
    EXPECT_NOT_EQUAL(0, getSize("state"));
    vespalib::string check = readState(sf);
    EXPECT_EQUAL("Foo bar\n", check);
    {
        std::vector<vespalib::string> exp({ "Hello world\n", "Foo bar\n" });
        std::vector<vespalib::string> act(readHistory("state.history"));
        TEST_DO(assertHistory(exp, act));
    }
}


TEST("Test that different entry is added to history")
{
    StateFile::erase("state");
    {
        StateFile sf("state");
        addState(sf, "Hello world\n");
        EXPECT_EQUAL(1, sf.getGen());
    }
    // Write changed entry to main state file
    {
        std::ofstream of("state");
        of << "zap\n";
    }
    // Add changed event to history
    StateFile sf("state");
    EXPECT_NOT_EQUAL(0, getSize("state"));
    vespalib::string check = readState(sf);
    EXPECT_EQUAL("zap\n", check);
    {
        std::vector<vespalib::string> exp({ "Hello world\n", "zap\n" });
        std::vector<vespalib::string> act(readHistory("state.history"));
        TEST_DO(assertHistory(exp, act));
    }
}


TEST("Test that state history stops at NUL byte")
{
    StateFile::erase("state");
    {
        StateFile sf("state");
        addState(sf, "Hello world\n");
        addState(sf, "Foo bar\n");
        EXPECT_EQUAL(2, sf.getGen());
    }
    // Corrupt history state file
    {
        char buf[1];
        buf[0] = '\0';
        std::ofstream of("state.history");
        of.write(&buf[0], 1);
    }
    StateFile sf("state");
    vespalib::string check = readState(sf);
    EXPECT_EQUAL("Foo bar\n", check);
    {
        std::vector<vespalib::string> exp({ "Foo bar\n" });
        std::vector<vespalib::string> act(readHistory("state.history"));
        TEST_DO(assertHistory(exp, act));
    }

}

TEST("Test that main state stops at NUL byte")
{
    StateFile::erase("state");
    {
        StateFile sf("state");
        addState(sf, "Hello world\n");
        addState(sf, "Foo bar\n");
        EXPECT_EQUAL(2, sf.getGen());
    }
    // Corrupt history state file
    {
        char buf[10];
        strcpy(buf, "zap");
        std::ofstream of("state");
        of.write(&buf[0], strlen(buf) + 1);
    }
    StateFile sf("state");
    vespalib::string check = readState(sf);
    EXPECT_EQUAL("Foo bar\n", check);
    {
        std::vector<vespalib::string> exp({ "Hello world\n", "Foo bar\n" });
        std::vector<vespalib::string> act(readHistory("state.history"));
        TEST_DO(assertHistory(exp, act));
    }

}

}

TEST_MAIN()
{
    TEST_RUN_ALL();
    search::StateFile::erase("state");
}