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

#include <vespa/messagebus/testlib/custompolicy.h>
#include <vespa/messagebus/testlib/receptor.h>
#include <vespa/messagebus/testlib/simplemessage.h>
#include <vespa/messagebus/testlib/slobrok.h>
#include <vespa/messagebus/testlib/testserver.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/messagebus/emptyreply.h>
#include <vespa/messagebus/errorcode.h>
#include <vespa/messagebus/routing/retrytransienterrorspolicy.h>

using namespace mbus;

class TestData {
public:
    Slobrok                        _slobrok;
    RetryTransientErrorsPolicy::SP _retryPolicy;
    TestServer                     _srcServer;
    SourceSession::UP              _srcSession;
    Receptor                       _srcHandler;
    TestServer                     _dstServer;
    DestinationSession::UP         _fooSession;
    Receptor                       _fooHandler;
    DestinationSession::UP         _barSession;
    Receptor                       _barHandler;
    DestinationSession::UP         _bazSession;
    Receptor                       _bazHandler;

public:
    TestData();
    ~TestData();
    bool start();
};

class Test : public vespalib::TestApp {
private:
    Message::UP createMessage(const string &msg);
    bool testTrace(const std::vector<string> &expected, const Trace &trace);

public:
    int Main() override;
    void testAdvanced(TestData &data);
};

TEST_APPHOOK(Test);

TestData::~TestData() = default;
TestData::TestData() :
    _slobrok(),
    _retryPolicy(std::make_shared<RetryTransientErrorsPolicy>()),
    _srcServer(MessageBusParams().setRetryPolicy(_retryPolicy).addProtocol(std::make_shared<SimpleProtocol>()),
               RPCNetworkParams(_slobrok.config())),
    _srcSession(),
    _srcHandler(),
    _dstServer(MessageBusParams().addProtocol(std::make_shared<SimpleProtocol>()),
               RPCNetworkParams(_slobrok.config()).setIdentity(Identity("dst"))),
    _fooSession(),
    _fooHandler(),
    _barSession(),
    _barHandler(),
    _bazSession(),
    _bazHandler()
{
    _retryPolicy->setBaseDelay(0);
}

bool
TestData::start()
{
    _srcSession = _srcServer.mb.createSourceSession(SourceSessionParams().setReplyHandler(_srcHandler));
    if ( ! _srcSession) {
        return false;
    }
    _fooSession = _dstServer.mb.createDestinationSession(DestinationSessionParams().setName("foo").setMessageHandler(_fooHandler));
    if ( ! _fooSession) {
        return false;
    }
    _barSession = _dstServer.mb.createDestinationSession(DestinationSessionParams().setName("bar").setMessageHandler(_barHandler));
    if ( ! _barSession) {
        return false;
    }
    _bazSession = _dstServer.mb.createDestinationSession(DestinationSessionParams().setName("baz").setMessageHandler(_bazHandler));
    if ( ! _bazSession) {
        return false;
    }
    if (!_srcServer.waitSlobrok("dst/*", 3u)) {
        return false;
    }
    return true;
}

Message::UP
Test::createMessage(const string &msg)
{
    auto ret = std::make_unique<SimpleMessage>(msg);
    ret->getTrace().setLevel(9);
    return ret;
}

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

    TestData data;
    ASSERT_TRUE(data.start());

    testAdvanced(data); TEST_FLUSH();

    TEST_DONE();
}

void
Test::testAdvanced(TestData &data)
{
    const duration TIMEOUT = 60s;
    IProtocol::SP protocol(new SimpleProtocol());
    auto &simple = dynamic_cast<SimpleProtocol&>(*protocol);
    simple.addPolicyFactory("Custom", SimpleProtocol::IPolicyFactory::SP(new CustomPolicyFactory(false, ErrorCode::NO_ADDRESS_FOR_SERVICE)));
    data._srcServer.mb.putProtocol(protocol);
    data._srcServer.mb.setupRouting(RoutingSpec().addTable(RoutingTableSpec(SimpleProtocol::NAME)
                                                           .addHop(HopSpec("bar", "dst/bar"))
                                                           .addHop(HopSpec("baz", "dst/baz"))
                                                           .addRoute(RouteSpec("baz").addHop("baz"))));
    string route = vespalib::make_string("[Custom:%s,bar,route:baz,dst/cox,?dst/unknown]",
                                              data._fooSession->getConnectionSpec().c_str());
    EXPECT_TRUE(data._srcSession->send(createMessage("msg"), Route::parse(route)).isAccepted());

    // Initial send.
    Message::UP msg = data._fooHandler.getMessage(TIMEOUT);
    ASSERT_TRUE(msg);
    data._fooSession->acknowledge(std::move(msg));
    msg = data._barHandler.getMessage(TIMEOUT);
    ASSERT_TRUE(msg);
    Reply::UP reply = std::make_unique<EmptyReply>();
    reply->swapState(*msg);
    reply->addError(Error(ErrorCode::TRANSIENT_ERROR, "bar"));
    data._barSession->reply(std::move(reply));
    msg = data._bazHandler.getMessage(TIMEOUT);
    ASSERT_TRUE(msg);
    reply = std::make_unique<EmptyReply>();
    reply->swapState(*msg);
    reply->addError(Error(ErrorCode::TRANSIENT_ERROR, "baz1"));
    data._bazSession->reply(std::move(reply));

    // First retry.
    msg = data._fooHandler.getMessageNow();
    ASSERT_FALSE(msg);
    msg = data._barHandler.getMessage(TIMEOUT);
    ASSERT_TRUE(msg);
    data._barSession->acknowledge(std::move(msg));
    msg = data._bazHandler.getMessage(TIMEOUT);
    ASSERT_TRUE(msg);
    reply = std::make_unique<EmptyReply>();
    reply->swapState(*msg);
    reply->addError(Error(ErrorCode::TRANSIENT_ERROR, "baz2"));
    data._bazSession->reply(std::move(reply));

    // Second retry.
    msg = data._fooHandler.getMessageNow();
    ASSERT_FALSE(msg);
    msg = data._barHandler.getMessageNow();
    ASSERT_FALSE(msg);
    msg = data._bazHandler.getMessage(TIMEOUT);
    ASSERT_TRUE(msg);
    reply = std::make_unique<EmptyReply>();
    reply->swapState(*msg);
    reply->addError(Error(ErrorCode::FATAL_ERROR, "baz3"));
    data._bazSession->reply(std::move(reply));

    // Done.
    reply = data._srcHandler.getReply();
    ASSERT_TRUE(reply);
    printf("%s", reply->getTrace().toString().c_str());
    EXPECT_EQUAL(2u, reply->getNumErrors());
    EXPECT_EQUAL((uint32_t)ErrorCode::FATAL_ERROR, reply->getError(0).getCode());
    EXPECT_EQUAL((uint32_t)ErrorCode::NO_ADDRESS_FOR_SERVICE, reply->getError(1).getCode());
}