aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/tests/union_service_map/union_service_map_test.cpp
blob: 5f1f70fb9fb6eed5c063f95540572370e43f7074 (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/slobrok/server/union_service_map.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/stringfmt.h>

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

enum class Event { NONE, ADD, REMOVE, UPDATE };

struct MapObserver : public MapListener {
    MapObserver();
    virtual ~MapObserver();
    void add(const ServiceMapping &mapping) override;
    void remove(const ServiceMapping &mapping) override;
    void update(const ServiceMapping &old_mapping,
                const ServiceMapping &new_mapping) override;

    Event last_event = Event::NONE;
    ServiceMapping last_add = {{}, {}};
    ServiceMapping last_remove = {{}, {}};

    void clear() { last_event = Event::NONE; }
};

MapObserver::MapObserver() = default;
MapObserver::~MapObserver() = default;

void MapObserver::add(const ServiceMapping &mapping) {
    last_event = Event::ADD;
    last_add = mapping;
}

void MapObserver::remove(const ServiceMapping &mapping) {
    last_event = Event::REMOVE;
    last_remove = mapping;
}

void MapObserver::update(const ServiceMapping &old_mapping,
                         const ServiceMapping &new_mapping)
{
    last_event = Event::UPDATE;
    last_remove = old_mapping;
    last_add = new_mapping;
}

TEST(UnionServiceMapTest, forwards_simple_requests) {
    ProxyMapSource source;
    UnionServiceMap unionizer;
    MapObserver observer;
    unionizer.registerListener(observer);
    source.registerListener(unionizer);

    EXPECT_EQ(observer.last_event, Event::NONE);

    ServiceMapping one{"foo/1", "bar/1"};
    source.add(one);
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_add, one);
    ServiceMapping two{"foo/2", "bar/2"};
    source.add(two);
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_add, two);

    source.remove(one);
    EXPECT_EQ(observer.last_event, Event::REMOVE);
    EXPECT_EQ(observer.last_remove, one);

    ServiceMapping two_q{"foo/2", "qux/2"};
    source.update(two, two_q);
    // update implemented ass remove+add:
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_remove, two);
    EXPECT_EQ(observer.last_add, two_q);
}

TEST(UnionServiceMapTest, handles_refcount) {
    ProxyMapSource source1;
    ProxyMapSource source2;
    ProxyMapSource source3;
    UnionServiceMap unionizer;
    MapObserver observer;
    unionizer.registerListener(observer);
    source1.registerListener(unionizer);
    source2.registerListener(unionizer);
    source3.registerListener(unionizer);

    EXPECT_EQ(observer.last_event, Event::NONE);
    ServiceMapping one{"foo/1", "bar/1"};
    source1.add(one);
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_add, one);
    observer.clear();
    EXPECT_EQ(observer.last_event, Event::NONE);
    source2.add(one);
    EXPECT_EQ(observer.last_event, Event::NONE);
    source3.add(one);
    EXPECT_EQ(observer.last_event, Event::NONE);
    ServiceMapping two{"foo/2", "bar/2"};
    source1.add(two);
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_add, two);
    observer.clear();
    EXPECT_EQ(observer.last_event, Event::NONE);
    source2.add(two);
    EXPECT_EQ(observer.last_event, Event::NONE);

    source1.remove(one);
    EXPECT_EQ(observer.last_event, Event::NONE);
    source2.remove(one);
    EXPECT_EQ(observer.last_event, Event::NONE);

    source1.remove(two);
    EXPECT_EQ(observer.last_event, Event::NONE);
    source2.remove(two);
    EXPECT_EQ(observer.last_event, Event::REMOVE);
    EXPECT_EQ(observer.last_remove, two);

    observer.clear();
    EXPECT_EQ(observer.last_event, Event::NONE);
    source3.remove(one);
    EXPECT_EQ(observer.last_event, Event::REMOVE);
    EXPECT_EQ(observer.last_remove, one);
}

TEST(UnionServiceMapTest, handles_conflicts) {
    ProxyMapSource source1;
    ProxyMapSource source2;
    ProxyMapSource source3;
    UnionServiceMap unionizer;
    MapObserver observer;
    unionizer.registerListener(observer);
    source1.registerListener(unionizer);
    source2.registerListener(unionizer);
    source3.registerListener(unionizer);

    EXPECT_EQ(observer.last_event, Event::NONE);
    ServiceMapping one{"foo/1", "bar/1"};
    source1.add(one);
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_add, one);
    observer.clear();
    source2.add(one);
    EXPECT_EQ(observer.last_event, Event::NONE);

    ServiceMapping two{"foo/2", "bar/2"};
    source1.add(two);
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_add, two);
    observer.clear();
    source2.add(two);
    EXPECT_EQ(observer.last_event, Event::NONE);

    ServiceMapping one_q{"foo/1", "qux/1"};
    source3.add(one_q);
    EXPECT_EQ(observer.last_event, Event::REMOVE);
    EXPECT_EQ(observer.last_remove, one);

    ServiceMapping two_q{"foo/2", "qux/2"};
    source3.add(two_q);
    EXPECT_EQ(observer.last_event, Event::REMOVE);
    EXPECT_EQ(observer.last_remove, two);

    source3.remove(one_q);
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_add, one);

    observer.clear();
    source1.remove(two);
    EXPECT_EQ(observer.last_event, Event::NONE);
    source2.remove(two);
    EXPECT_EQ(observer.last_event, Event::ADD);
    EXPECT_EQ(observer.last_add, two_q);
}


GTEST_MAIN_RUN_ALL_TESTS()