summaryrefslogtreecommitdiffstats
path: root/messagebus/src/tests/serviceaddress/serviceaddress.cpp
blob: 15a2e9fdac8fb9c8871564dc3fb8c50158004b3f (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
// Copyright 2017 Yahoo Holdings. 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/messagebus/testlib/testserver.h>
#include <vespa/messagebus/testlib/receptor.h>
#include <vespa/messagebus/testlib/simplemessage.h>
#include <vespa/messagebus/testlib/simplereply.h>
#include <vespa/messagebus/testlib/simpleprotocol.h>
#include <vespa/messagebus/messagebus.h>
#include <vespa/messagebus/sourcesession.h>
#include <vespa/messagebus/intermediatesession.h>
#include <vespa/messagebus/destinationsession.h>
#include <vespa/messagebus/emptyreply.h>
#include <vespa/messagebus/error.h>
#include <vespa/messagebus/errorcode.h>
#include <vespa/messagebus/routing/routingspec.h>
#include <vespa/messagebus/network/rpcservice.h>
#include <vespa/messagebus/sourcesessionparams.h>

using namespace mbus;

class Test : public vespalib::TestApp {
public:
    int Main() override;
    void testAddrServiceAddress();
    void testNameServiceAddress();

private:
    bool waitSlobrok(RPCNetwork &network, const string &pattern, size_t num);
    bool testAddress(RPCNetwork& network, const string &pattern,
                     const string &expectedSpec, const string &expectedSession);
    bool testNullAddress(RPCNetwork &network, const string &pattern);
};

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

    testAddrServiceAddress(); TEST_FLUSH();
    testNameServiceAddress(); TEST_FLUSH();

    TEST_DONE();
}

TEST_APPHOOK(Test);

void
Test::testAddrServiceAddress()
{
    Slobrok slobrok;
    RPCNetwork network(RPCNetworkParams()
                       .setIdentity(Identity("foo"))
                       .setSlobrokConfig(slobrok.config()));
    ASSERT_TRUE(network.start());

    EXPECT_TRUE(testNullAddress(network, "tcp"));
    EXPECT_TRUE(testNullAddress(network, "tcp/"));
    EXPECT_TRUE(testNullAddress(network, "tcp/localhost"));
    EXPECT_TRUE(testNullAddress(network, "tcp/localhost:"));
    EXPECT_TRUE(testNullAddress(network, "tcp/localhost:1977"));
    EXPECT_TRUE(testNullAddress(network, "tcp/localhost:1977/"));
    EXPECT_TRUE(testAddress(network, "tcp/localhost:1977/session", "tcp/localhost:1977", "session"));
    EXPECT_TRUE(testNullAddress(network, "tcp/localhost:/session"));
    EXPECT_TRUE(testNullAddress(network, "tcp/:1977/session"));
    EXPECT_TRUE(testNullAddress(network, "tcp/:/session"));

    network.shutdown();
}

void
Test::testNameServiceAddress()
{
    Slobrok slobrok;
    RPCNetwork network(RPCNetworkParams()
                       .setIdentity(Identity("foo"))
                       .setSlobrokConfig(slobrok.config()));
    ASSERT_TRUE(network.start());

    network.unregisterSession("session");
    ASSERT_TRUE(waitSlobrok(network, "foo/session", 0));
    EXPECT_TRUE(testNullAddress(network, "foo/session"));

    network.registerSession("session");
    ASSERT_TRUE(waitSlobrok(network, "foo/session", 1));
    EXPECT_TRUE(testAddress(network, "foo/session", network.getConnectionSpec().c_str(), "session"));

    network.shutdown();
}

bool
Test::waitSlobrok(RPCNetwork &network, const string &pattern, size_t num)
{
    for (int i = 0; i < 1000; i++) {
        slobrok::api::IMirrorAPI::SpecList res = network.getMirror().lookup(pattern);
        if (res.size() == num) {
            return true;
        }
        FastOS_Thread::Sleep(10);
    }
    return false;
}

bool
Test::testNullAddress(RPCNetwork &network, const string &pattern)
{
    RPCService service(network.getMirror(), pattern);
    RPCServiceAddress::UP obj = service.resolve();
    if (!EXPECT_TRUE(obj.get() == NULL)) {
        return false;
    }
    return true;
}

bool
Test::testAddress(RPCNetwork &network, const string &pattern,
                    const string &expectedSpec, const string &expectedSession)
{
    RPCService service(network.getMirror(), pattern);
    RPCServiceAddress::UP obj = service.resolve();
    if (!EXPECT_TRUE(obj.get() != NULL)) {
        return false;
    }
    if (!EXPECT_EQUAL(expectedSpec, obj->getConnectionSpec())) {
        return false;
    }
    if (!EXPECT_EQUAL(expectedSession, obj->getSessionName())) {
        return false;
    }
    return true;
}