summaryrefslogtreecommitdiffstats
path: root/messagebus/src/tests/slobrok/slobrok.cpp
blob: fd86747fd27b9f59a28a8c296dad99f544e608fc (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/messagebus/testlib/slobrok.h>
#include <vespa/slobrok/sbmirror.h>
#include <vespa/messagebus/network/rpcnetwork.h>
#include <vespa/messagebus/network/rpcnetworkparams.h>
#include <vespa/vespalib/util/host_name.h>
#include <thread>

using slobrok::api::IMirrorAPI;

using namespace mbus;

string
createSpec(int port)
{
    std::ostringstream str;
    str << "tcp/";
    str << vespalib::HostName::get();
    str << ":";
    str << port;
    return str.str();
}

struct SpecList
{
    IMirrorAPI::SpecList _specList;
    SpecList() : _specList() {}
    SpecList(IMirrorAPI::SpecList input) : _specList(input) {}
    SpecList &add(const string &name, const string &spec) {
        _specList.push_back(std::make_pair(string(name),
                                      string(spec)));
        return *this;
    }
    void sort() {
        std::sort(_specList.begin(), _specList.end());
    }
    bool operator==(SpecList &rhs) { // NB: MUTATE!
        sort();
        rhs.sort();
        return _specList == rhs._specList;
    }
};

bool
compare(const IMirrorAPI &api, const string &pattern, SpecList expect)
{
    for (int i = 0; i < 250; ++i) {
        SpecList actual(api.lookup(pattern));
        if (actual == expect) {
            return true;
        }
        std::this_thread::sleep_for(100ms);
    }
    return false;
}

TEST_SETUP(Test);

int
Test::Main()
{
    TEST_INIT("slobrok_test");
    Slobrok slobrok;
    RPCNetwork net1(RPCNetworkParams(slobrok.config())
                    .setIdentity(Identity("net/a")));
    RPCNetwork net2(RPCNetworkParams(slobrok.config())
                    .setIdentity(Identity("net/b")));
    RPCNetwork net3(RPCNetworkParams(slobrok.config())
                    .setIdentity(Identity("net/c")));
    ASSERT_TRUE(net1.start());
    ASSERT_TRUE(net2.start());
    ASSERT_TRUE(net3.start());
    string spec1 = createSpec(net1.getPort());
    string spec2 = createSpec(net2.getPort());
    string spec3 = createSpec(net3.getPort());

    net1.registerSession("foo");
    net2.registerSession("foo");
    net2.registerSession("bar");
    net3.registerSession("foo");
    net3.registerSession("bar");
    net3.registerSession("baz");

    EXPECT_TRUE(compare(net1.getMirror(), "*/*/*", SpecList()
                       .add("net/a/foo", spec1)
                       .add("net/b/foo", spec2)
                       .add("net/b/bar", spec2)
                       .add("net/c/foo", spec3)
                       .add("net/c/bar", spec3)
                       .add("net/c/baz", spec3)));
    EXPECT_TRUE(compare(net2.getMirror(), "*/*/*", SpecList()
                       .add("net/a/foo", spec1)
                       .add("net/b/foo", spec2)
                       .add("net/b/bar", spec2)
                       .add("net/c/foo", spec3)
                       .add("net/c/bar", spec3)
                       .add("net/c/baz", spec3)));
    EXPECT_TRUE(compare(net3.getMirror(), "*/*/*", SpecList()
                       .add("net/a/foo", spec1)
                       .add("net/b/foo", spec2)
                       .add("net/b/bar", spec2)
                       .add("net/c/foo", spec3)
                       .add("net/c/bar", spec3)
                       .add("net/c/baz", spec3)));

    net2.unregisterSession("bar");
    net3.unregisterSession("bar");
    net3.unregisterSession("baz");

    EXPECT_TRUE(compare(net1.getMirror(), "*/*/*", SpecList()
                       .add("net/a/foo", spec1)
                       .add("net/b/foo", spec2)
                       .add("net/c/foo", spec3)));
    EXPECT_TRUE(compare(net2.getMirror(), "*/*/*", SpecList()
                       .add("net/a/foo", spec1)
                       .add("net/b/foo", spec2)
                       .add("net/c/foo", spec3)));
    EXPECT_TRUE(compare(net3.getMirror(), "*/*/*", SpecList()
                       .add("net/a/foo", spec1)
                       .add("net/b/foo", spec2)
                       .add("net/c/foo", spec3)));

    net3.shutdown();
    net2.shutdown();
    net1.shutdown();
    TEST_DONE();
}