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

#include <vespa/slobrok/server/mock_map_listener.h>
#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;

TEST(UnionServiceMapTest, forwards_simple_requests) {
    ProxyMapSource source;
    UnionServiceMap unionizer;
    MockMapListener observer;
    auto subscription1 = MapSubscription::subscribe(unionizer, observer);
    auto subscription2 = MapSubscription::subscribe(source, unionizer);

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

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

    source.remove(one);
    EXPECT_EQ(observer.last_event, MockEvent::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, MockEvent::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;
    MockMapListener observer;
    auto subscription1 = MapSubscription::subscribe(unionizer, observer);
    auto subscription2 = MapSubscription::subscribe(source1, unionizer);
    auto subscription3 = MapSubscription::subscribe(source2, unionizer);
    auto subscription4 = MapSubscription::subscribe(source3, unionizer);

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

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

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

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

TEST(UnionServiceMapTest, handles_conflicts) {
    ProxyMapSource source1;
    ProxyMapSource source2;
    ProxyMapSource source3;
    UnionServiceMap unionizer;
    MockMapListener observer;
    auto subscription1 = MapSubscription::subscribe(unionizer, observer);
    auto subscription2 = MapSubscription::subscribe(source1, unionizer);
    auto subscription3 = MapSubscription::subscribe(source2, unionizer);
    auto subscription4 = MapSubscription::subscribe(source3, unionizer);

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

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

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

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

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

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


GTEST_MAIN_RUN_ALL_TESTS()