aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/tests/mirrorapi/mirrorapi.cpp
blob: 6dac6b22b05b3f16eb5aba184bd7af23baece53b (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
// 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/slobrok/sbmirror.h>
#include <vespa/config-slobroks.h>
#include <algorithm>
#include <vespa/slobrok/server/slobrokserver.h>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/target.h>
#include <vespa/fnet/transport.h>
#include <thread>

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

using slobrok::api::MirrorAPI;
using slobrok::SlobrokServer;

TEST_SETUP(Test);

//-----------------------------------------------------------------------------

class Server : public FRT_Invokable
{
private:
    fnet::frt::StandaloneFRT _server;
    std::string    _name;
    std::string    _slobrokSpec;

public:
    Server(std::string name, int port, std::string slobrokSpec);
    ~Server();
    void rpc_listNamesServed(FRT_RPCRequest *req);
    void reg();
};


Server::Server(std::string name, int port, std::string slobrokSpec)
    : _server(),
      _name(name),
      _slobrokSpec(slobrokSpec)
{
    {
        FRT_ReflectionBuilder rb(&_server.supervisor());
        //---------------------------------------------------------------------
        rb.DefineMethod("slobrok.callback.listNamesServed", "", "S",
                        FRT_METHOD(Server::rpc_listNamesServed), this);
        rb.MethodDesc("Look up a rpcserver");
        rb.ReturnDesc("names", "The rpcserver names on this server");
        //---------------------------------------------------------------------
    }
    _server.supervisor().Listen(port);
}


void
Server::reg()
{
    char spec[64];
    snprintf(spec, sizeof(spec), "tcp/localhost:%d", _server.supervisor().GetListenPort());

    FRT_RPCRequest *req = _server.supervisor().AllocRPCRequest();
    req->SetMethodName("slobrok.registerRpcServer");
    req->GetParams()->AddString(_name.c_str());
    req->GetParams()->AddString(spec);

    FRT_Target *sb = _server.supervisor().GetTarget(_slobrokSpec.c_str());
    sb->InvokeSync(req, 5.0);
    sb->internal_subref();
    req->internal_subref();
}


void
Server::rpc_listNamesServed(FRT_RPCRequest *req)
{
    FRT_Values &dst = *req->GetReturn();
    FRT_StringValue *names = dst.AddStringArray(1);
    dst.SetString(&names[0], _name.c_str());
}


Server::~Server() = default;

//-----------------------------------------------------------------------------

struct SpecList
{
    MirrorAPI::SpecList _specList;
    SpecList() : _specList() {}
    SpecList(MirrorAPI::SpecList input) : _specList(input) {}
    SpecList &add(const char *name, const char *spec) {
        _specList.push_back(make_pair(std::string(name),
                                      std::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(MirrorAPI &api, const char *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;
}


int
Test::Main()
{
    TEST_INIT("mirrorapi_test");

    SlobrokServer mock(18501);
    std::this_thread::sleep_for(300ms);

    Server a("A/x/w", 18502, "tcp/localhost:18501");
    Server b("B/x",   18503, "tcp/localhost:18501");
    Server c("C/x/z", 18504, "tcp/localhost:18501");
    Server d("D/y/z", 18505, "tcp/localhost:18501");
    Server e("E/y",   18506, "tcp/localhost:18501");
    Server f("F/y/w", 18507, "tcp/localhost:18501");

    cloud::config::SlobroksConfigBuilder specBuilder;
    cloud::config::SlobroksConfig::Slobrok slobrok;
    slobrok.connectionspec = "tcp/localhost:18501";
    specBuilder.slobrok.push_back(slobrok);
    FNET_Transport transport;
    FRT_Supervisor supervisor(&transport);
    MirrorAPI mirror(supervisor, slobrok::ConfiguratorFactory(config::ConfigUri::createFromInstance(specBuilder)));
    EXPECT_TRUE(!mirror.ready());
    transport.Start();
    std::this_thread::sleep_for(1s);

    a.reg();
    EXPECT_TRUE(compare(mirror, "A/x/w", SpecList().add("A/x/w", "tcp/localhost:18502")));
    EXPECT_TRUE(compare(mirror, "*/*", SpecList()));
    EXPECT_TRUE(compare(mirror, "*/*/*", SpecList().add("A/x/w", "tcp/localhost:18502")));
    EXPECT_TRUE(compare(mirror, "*/*/w*", SpecList().add("A/x/w", "tcp/localhost:18502")));
    EXPECT_TRUE(compare(mirror, "A**", SpecList().add("A/x/w", "tcp/localhost:18502")));
    EXPECT_TRUE(compare(mirror, "**", SpecList().add("A/x/w", "tcp/localhost:18502")));
    EXPECT_TRUE(mirror.ready());

    TEST_FLUSH();
    b.reg();
    EXPECT_TRUE(compare(mirror, "B/x", SpecList().add("B/x", "tcp/localhost:18503")));
    EXPECT_TRUE(compare(mirror, "*/*", SpecList().add("B/x", "tcp/localhost:18503")));
    EXPECT_TRUE(compare(mirror, "*/*/*", SpecList().add("A/x/w", "tcp/localhost:18502")));

    TEST_FLUSH();
    c.reg();
    EXPECT_TRUE(compare(mirror, "C/x/z", SpecList().add("C/x/z", "tcp/localhost:18504")));
    EXPECT_TRUE(compare(mirror, "*/*", SpecList().add("B/x", "tcp/localhost:18503")));
    EXPECT_TRUE(compare(mirror, "*/*/*", SpecList()
                       .add("A/x/w", "tcp/localhost:18502")
                       .add("C/x/z", "tcp/localhost:18504")));

    TEST_FLUSH();
    d.reg();
    EXPECT_TRUE(compare(mirror, "D/y/z", SpecList().add("D/y/z", "tcp/localhost:18505")));
    EXPECT_TRUE(compare(mirror, "*/*", SpecList().add("B/x", "tcp/localhost:18503")));
    EXPECT_TRUE(compare(mirror, "*/*/*", SpecList()
                       .add("A/x/w", "tcp/localhost:18502")
                       .add("C/x/z", "tcp/localhost:18504")
                       .add("D/y/z", "tcp/localhost:18505")));

    TEST_FLUSH();
    e.reg();
    EXPECT_TRUE(compare(mirror, "E/y", SpecList().add("E/y", "tcp/localhost:18506")));
    EXPECT_TRUE(compare(mirror, "*/*", SpecList()
                       .add("B/x", "tcp/localhost:18503")
                       .add("E/y", "tcp/localhost:18506")));
    EXPECT_TRUE(compare(mirror, "*/*/*", SpecList()
                       .add("A/x/w", "tcp/localhost:18502")
                       .add("C/x/z", "tcp/localhost:18504")
                       .add("D/y/z", "tcp/localhost:18505")));

    TEST_FLUSH();
    f.reg();
    EXPECT_TRUE(compare(mirror, "F/y/w", SpecList().add("F/y/w", "tcp/localhost:18507")));
    EXPECT_TRUE(compare(mirror, "*/*", SpecList()
                       .add("B/x", "tcp/localhost:18503")
                       .add("E/y", "tcp/localhost:18506")));
    EXPECT_TRUE(compare(mirror, "*/*/*", SpecList()
                       .add("A/x/w", "tcp/localhost:18502")
                       .add("C/x/z", "tcp/localhost:18504")
                       .add("D/y/z", "tcp/localhost:18505")
                       .add("F/y/w", "tcp/localhost:18507")));


    EXPECT_TRUE(compare(mirror, "*", SpecList()));

    EXPECT_TRUE(compare(mirror, "B/*", SpecList()
                       .add("B/x", "tcp/localhost:18503")));

    EXPECT_TRUE(compare(mirror, "*/y", SpecList()
                       .add("E/y", "tcp/localhost:18506")));

    EXPECT_TRUE(compare(mirror, "*/x/*", SpecList()
                       .add("A/x/w", "tcp/localhost:18502")
                       .add("C/x/z", "tcp/localhost:18504")));

    EXPECT_TRUE(compare(mirror, "*/*/z", SpecList()
                       .add("C/x/z", "tcp/localhost:18504")
                       .add("D/y/z", "tcp/localhost:18505")));

    EXPECT_TRUE(compare(mirror, "A/*/z", SpecList()));

    EXPECT_TRUE(compare(mirror, "A/*/w", SpecList()
                       .add("A/x/w", "tcp/localhost:18502")));

    mock.stop();
    transport.ShutDown(true);
    TEST_DONE();
}