aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/tests/local_rpc_monitor_map/local_rpc_monitor_map_test.cpp
blob: b7235155f8cf5f9ae7e11e0adf2a3d310276cdfa (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 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/slobrok/server/local_rpc_monitor_map.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/time.h>
#include <vespa/fnet/scheduler.h>
#include <map>

using namespace vespalib;
using namespace slobrok;
using vespalib::make_string_short::fmt;

struct MapCall {
    vespalib::string name;
    ServiceMapping mapping;
    ServiceMapping old;
    static MapCall add(const ServiceMapping &m) { return {"add", m, {"",""}}; }
    static MapCall remove(const ServiceMapping &m) { return {"remove", m, {"",""}}; }
    static MapCall update(const ServiceMapping &o, const ServiceMapping &m) { return {"update", m, o}; }
    void check(const MapCall &rhs) const {
        EXPECT_EQ(name, rhs.name);
        EXPECT_EQ(mapping, rhs.mapping);
        EXPECT_EQ(old, rhs.old);
    }
    ~MapCall();
};
MapCall::~MapCall() = default;

struct MonitorCall {
    vespalib::string name;
    ServiceMapping mapping;
    bool hurry;
    static MonitorCall start(const ServiceMapping &m, bool h) { return {"start", m, h}; }
    static MonitorCall stop(const ServiceMapping &m) { return {"stop", m, false}; }
    void check(const MonitorCall &rhs) const {
        EXPECT_EQ(name, rhs.name);
        EXPECT_EQ(mapping, rhs.mapping);
        EXPECT_EQ(hurry, rhs.hurry);
    }
    ~MonitorCall();
};
MonitorCall::~MonitorCall() = default;

template <typename Call>
class CallLog {
private:
    std::vector<Call> _calls;
    size_t _checked;
public:
    CallLog() noexcept : _calls(), _checked(0) {}
    ~CallLog() { EXPECT_EQ(_calls.size(), _checked); }
    void log(Call call) { _calls.push_back(call); }
    void expect(std::initializer_list<Call> list) {
        ASSERT_EQ(list.size(), (_calls.size() - _checked));
        for (const auto &call: list) {
            call.check(_calls[_checked++]);
        }
    }
};

struct MapLog : CallLog<MapCall>, MapListener {
    ~MapLog() override;
    void add(const ServiceMapping &mapping) override {
        log(MapCall::add(mapping));
    }
    void remove(const ServiceMapping &mapping) override {
        log(MapCall::remove(mapping));
    }
    void update(const ServiceMapping &old_mapping,
                const ServiceMapping &new_mapping) override
    {
        log(MapCall::update(old_mapping, new_mapping));
    }
};

MapLog::~MapLog() = default;

struct MonitorLog : CallLog<MonitorCall>, MappingMonitor {
    void start(const ServiceMapping& mapping, bool hurry) override {
        log(MonitorCall::start(mapping, hurry));
    }
    void stop(const ServiceMapping& mapping) override {
        log(MonitorCall::stop(mapping));
    }
};

struct MyMappingMonitor : MappingMonitor {
    MonitorLog &monitor;
    explicit MyMappingMonitor(MonitorLog &m) : monitor(m) {}
    void start(const ServiceMapping& mapping, bool hurry) override {
        monitor.start(mapping, hurry);
    }
    void stop(const ServiceMapping& mapping) override {
        monitor.stop(mapping);
    }
};

struct LocalRpcMonitorMapTest : public ::testing::Test {
    steady_time time;
    FNET_Scheduler scheduler;
    MonitorLog monitor_log;
    MapLog map_log;
    LocalRpcMonitorMap map;
    std::unique_ptr<MapSubscription> subscription;
    ServiceMapping mapping;
    ServiceMapping mapping_conflict;
    LocalRpcMonitorMapTest()
      : time(duration::zero()),
        scheduler(&time), monitor_log(), map_log(),
        map(&scheduler, [this](auto &owner)
            {
                EXPECT_EQ(&owner, &map);
                return std::make_unique<MyMappingMonitor>(monitor_log);
            }),
        subscription(MapSubscription::subscribe(map.dispatcher(), map_log)),
        mapping("dummy_service", "dummy_spec"),
        mapping_conflict("dummy_service", "conflicting_dummy_spec")
    {}
    void tick(duration elapsed = FNET_Scheduler::tick_ms) {
        time += elapsed;
        scheduler.CheckTasks();
    }
    void add_mapping(const ServiceMapping &m, bool is_up) {
        map.add(m); // <- add from consensus map
        monitor_log.expect({});
        tick(0ms); // <- process delayed add event
        monitor_log.expect({MonitorCall::start(m, false)});
        map_log.expect({});
        if (is_up) {
            map.up(m); // <- up from monitor
            map_log.expect({MapCall::add(m)});
        } else {
            map.down(m); // <- down from monitor
            map_log.expect({});
        }
    }
    void flip_up_state(const ServiceMapping &m, bool was_up, size_t cnt) {
        for (size_t i = 0; i < cnt; ++i) {
            if (was_up) {
                map.up(m);
                map_log.expect({});
                map.down(m);
                map_log.expect({MapCall::remove(m)});
            } else {
                map.down(m);
                map_log.expect({});
                map.up(m);
                map_log.expect({MapCall::add(m)});
            }
            was_up = !was_up;
        }
        monitor_log.expect({});
    }
    void remove_mapping(const ServiceMapping &m, bool was_up) {
        map.remove(m); // <- remove from consensus map
        monitor_log.expect({});
        tick(0ms); // <- process delayed remove event
        monitor_log.expect({MonitorCall::stop(m)});
        if (was_up) {
            map_log.expect({MapCall::remove(m)});
        } else {
            map_log.expect({});
        }
    }
    ~LocalRpcMonitorMapTest() override;
};
LocalRpcMonitorMapTest::~LocalRpcMonitorMapTest() = default;

struct MyAddLocalHandler : CompletionHandler {
    std::unique_ptr<OkState> &state;
    bool &handler_deleted;
    MyAddLocalHandler(std::unique_ptr<OkState> &s, bool &hd)
      : state(s), handler_deleted(hd) {} 
    void doneHandler(OkState result) override {
        state = std::make_unique<OkState>(result);
    }
    ~MyAddLocalHandler() override {
        handler_deleted = true;
    }
};

TEST_F(LocalRpcMonitorMapTest, external_add_remove_while_up) {
    add_mapping(mapping, true);
    remove_mapping(mapping, true);
}

TEST_F(LocalRpcMonitorMapTest, external_add_remove_while_down) {
    add_mapping(mapping, false);
    remove_mapping(mapping, false);
}

TEST_F(LocalRpcMonitorMapTest, server_up_down_up_down) {
    add_mapping(mapping, true);
    flip_up_state(mapping, true, 3);
    remove_mapping(mapping, false);
}

TEST_F(LocalRpcMonitorMapTest, server_down_up_down_up) {
    add_mapping(mapping, false);
    flip_up_state(mapping, false, 3);
    remove_mapping(mapping, true);
}

TEST_F(LocalRpcMonitorMapTest, multi_mapping) {
    ServiceMapping m1("dummy_service1", "dummy_spec1");
    ServiceMapping m2("dummy_service2", "dummy_spec2");
    ServiceMapping m3("dummy_service3", "dummy_spec3");
    add_mapping(m1, true);
    add_mapping(m2, false);
    add_mapping(m3, true);
    flip_up_state(m1, true, 3);
    flip_up_state(m2, false, 3);
    flip_up_state(m3, true, 3);
    remove_mapping(m1, false);
    remove_mapping(m2, true);
    remove_mapping(m3, false);
}

TEST_F(LocalRpcMonitorMapTest, local_add_ok) {
    std::unique_ptr<OkState> state;
    bool handler_deleted;
    map.addLocal(mapping, std::make_unique<MyAddLocalHandler>(state, handler_deleted));
    monitor_log.expect({MonitorCall::start(mapping, true)});
    map_log.expect({});
    map.up(mapping);
    monitor_log.expect({});
    map_log.expect({MapCall::add(mapping)});
    ASSERT_TRUE(state);
    EXPECT_TRUE(state->ok());
    ASSERT_TRUE(handler_deleted);
}

TEST_F(LocalRpcMonitorMapTest, local_add_already_up) {
    std::unique_ptr<OkState> state;
    bool handler_deleted;
    add_mapping(mapping, true);
    map.addLocal(mapping, std::make_unique<MyAddLocalHandler>(state, handler_deleted));
    monitor_log.expect({});
    map_log.expect({});
    ASSERT_TRUE(state);
    EXPECT_TRUE(state->ok());
    ASSERT_TRUE(handler_deleted);
}

TEST_F(LocalRpcMonitorMapTest, local_add_unknown_comes_up) {
    std::unique_ptr<OkState> state;
    bool handler_deleted;
    add_mapping(mapping, false);
    map.addLocal(mapping, std::make_unique<MyAddLocalHandler>(state, handler_deleted));
    monitor_log.expect({MonitorCall::stop(mapping), MonitorCall::start(mapping, true)});
    map_log.expect({});
    EXPECT_FALSE(state);
    map.up(mapping);
    map_log.expect({MapCall::add(mapping)});
    ASSERT_TRUE(state);
    EXPECT_TRUE(state->ok());
    ASSERT_TRUE(handler_deleted);
}

TEST_F(LocalRpcMonitorMapTest, local_add_unknown_goes_down) {
    std::unique_ptr<OkState> state;
    bool handler_deleted;
    add_mapping(mapping, false);
    map.addLocal(mapping, std::make_unique<MyAddLocalHandler>(state, handler_deleted));
    monitor_log.expect({MonitorCall::stop(mapping), MonitorCall::start(mapping, true)});
    map_log.expect({});
    EXPECT_FALSE(state);
    map.down(mapping);
    map_log.expect({});
    ASSERT_TRUE(state);
    EXPECT_FALSE(state->ok());
    ASSERT_TRUE(handler_deleted);
}

TEST_F(LocalRpcMonitorMapTest, local_add_conflict) {
    std::unique_ptr<OkState> state;
    bool handler_deleted;
    add_mapping(mapping, true);
    map.addLocal(mapping_conflict, std::make_unique<MyAddLocalHandler>(state, handler_deleted));
    monitor_log.expect({});
    map_log.expect({});
    ASSERT_TRUE(state);
    EXPECT_TRUE(state->failed());
    ASSERT_TRUE(handler_deleted);
}

TEST_F(LocalRpcMonitorMapTest, local_multi_add) {
    std::unique_ptr<OkState> state1;
    bool handler_deleted1;
    std::unique_ptr<OkState> state2;
    bool handler_deleted2;
    map.addLocal(mapping, std::make_unique<MyAddLocalHandler>(state1, handler_deleted1));
    monitor_log.expect({MonitorCall::start(mapping, true)});
    map.addLocal(mapping, std::make_unique<MyAddLocalHandler>(state2, handler_deleted2));
    monitor_log.expect({});
    map_log.expect({});
    EXPECT_FALSE(state1);
    EXPECT_FALSE(state2);
    map.up(mapping);
    monitor_log.expect({});
    map_log.expect({MapCall::add(mapping)});
    ASSERT_TRUE(state1);
    ASSERT_TRUE(state2);
    EXPECT_TRUE(state1->ok());
    EXPECT_TRUE(state2->ok());
    ASSERT_TRUE(handler_deleted1);
    ASSERT_TRUE(handler_deleted2);
}

TEST_F(LocalRpcMonitorMapTest, local_remove) {
    add_mapping(mapping, true);
    map.removeLocal(mapping);
    monitor_log.expect({MonitorCall::stop(mapping), MonitorCall::start(mapping, false)});
    map_log.expect({MapCall::remove(mapping)});
    map.up(mapping); // timeout case (should normally not happen)
    map_log.expect({MapCall::add(mapping)});
}

TEST_F(LocalRpcMonitorMapTest, local_add_local_remove) {
    std::unique_ptr<OkState> state;
    bool handler_deleted;
    map.addLocal(mapping, std::make_unique<MyAddLocalHandler>(state, handler_deleted));
    monitor_log.expect({MonitorCall::start(mapping, true)});
    map_log.expect({});
    map.removeLocal(mapping);
    monitor_log.expect({MonitorCall::stop(mapping)});
    map_log.expect({});
    ASSERT_TRUE(state);
    EXPECT_TRUE(state->failed());
    ASSERT_TRUE(handler_deleted);
}

GTEST_MAIN_RUN_ALL_TESTS()