summaryrefslogtreecommitdiffstats
path: root/messagebus/src/tests/shutdown/shutdown.cpp
blob: 5f5fe847f57e1259a1576a9fffb597e34a574ea2 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/log/log.h>
LOG_SETUP("shutdown_test");

#include <vespa/messagebus/emptyreply.h>
#include <vespa/messagebus/errorcode.h>
#include <vespa/messagebus/messagebus.h>
#include <vespa/messagebus/routing/errordirective.h>
#include <vespa/messagebus/routing/retrytransienterrorspolicy.h>
#include <vespa/messagebus/testlib/receptor.h>
#include <vespa/messagebus/testlib/simplemessage.h>
#include <vespa/messagebus/testlib/simpleprotocol.h>
#include <vespa/messagebus/testlib/slobrok.h>
#include <vespa/messagebus/testlib/testserver.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/vstringfmt.h>

using namespace mbus;

class Test : public vespalib::TestApp {
private:
    void requireThatListenFailedIsExceptionSafe();
    void requireThatShutdownOnSourceWithPendingIsSafe();
    void requireThatShutdownOnIntermediateWithPendingIsSafe();

public:
    int Main() override {
        TEST_INIT("shutdown_test");

        requireThatListenFailedIsExceptionSafe();             TEST_FLUSH();
        requireThatShutdownOnSourceWithPendingIsSafe();       TEST_FLUSH();
        requireThatShutdownOnIntermediateWithPendingIsSafe(); TEST_FLUSH();

        TEST_DONE();
    }
};

static const double TIMEOUT = 120;

TEST_APPHOOK(Test);

void
Test::requireThatListenFailedIsExceptionSafe()
{
    FRT_Supervisor orb;
    ASSERT_TRUE(orb.Listen(0));
    ASSERT_TRUE(orb.Start());

    Slobrok slobrok;
    try {
        TestServer bar(MessageBusParams(),
                       RPCNetworkParams()
                       .setListenPort(orb.GetListenPort())
                       .setSlobrokConfig(slobrok.config()));
        EXPECT_TRUE(false);
    } catch (vespalib::Exception &e) {
        EXPECT_EQUAL("Failed to start network.",
                   e.getMessage());
    }
    orb.ShutDown(true);
}

void
Test::requireThatShutdownOnSourceWithPendingIsSafe()
{
    Slobrok slobrok;
    TestServer dstServer(MessageBusParams()
                         .addProtocol(IProtocol::SP(new SimpleProtocol())),
                         RPCNetworkParams()
                         .setIdentity(Identity("dst"))
                         .setSlobrokConfig(slobrok.config()));
    Receptor dstHandler;
    DestinationSession::UP dstSession = dstServer.mb.createDestinationSession(
            DestinationSessionParams()
            .setName("session")
            .setMessageHandler(dstHandler));
    ASSERT_TRUE(dstSession.get() != NULL);

    for (uint32_t i = 0; i < 10; ++i) {
        Message::UP msg(new SimpleMessage("msg"));
        {
            TestServer srcServer(MessageBusParams()
                    .setRetryPolicy(IRetryPolicy::SP(new RetryTransientErrorsPolicy()))
                    .addProtocol(IProtocol::SP(new SimpleProtocol())),
                    RPCNetworkParams()
                    .setSlobrokConfig(slobrok.config()));
            Receptor srcHandler;
            SourceSession::UP srcSession = srcServer.mb.createSourceSession(SourceSessionParams()
                    .setThrottlePolicy(IThrottlePolicy::SP())
                    .setReplyHandler(srcHandler));
            ASSERT_TRUE(srcSession.get() != NULL);
            ASSERT_TRUE(srcServer.waitSlobrok("dst/session", 1));
            ASSERT_TRUE(srcSession->send(std::move(msg), "dst/session", true).isAccepted());
            msg = dstHandler.getMessage(TIMEOUT);
            ASSERT_TRUE(msg.get() != NULL);
        }
        dstSession->acknowledge(std::move(msg));
    }
}

void
Test::requireThatShutdownOnIntermediateWithPendingIsSafe()
{
    Slobrok slobrok;
    TestServer dstServer(MessageBusParams()
                         .addProtocol(IProtocol::SP(new SimpleProtocol())),
                         RPCNetworkParams()
                         .setIdentity(Identity("dst"))
                         .setSlobrokConfig(slobrok.config()));
    Receptor dstHandler;
    DestinationSession::UP dstSession = dstServer.mb.createDestinationSession(
            DestinationSessionParams()
            .setName("session")
            .setMessageHandler(dstHandler));
    ASSERT_TRUE(dstSession.get() != NULL);

    TestServer srcServer(MessageBusParams()
                         .setRetryPolicy(IRetryPolicy::SP())
                         .addProtocol(IProtocol::SP(new SimpleProtocol())),
                         RPCNetworkParams()
                         .setSlobrokConfig(slobrok.config()));
    Receptor srcHandler;
    SourceSession::UP srcSession = srcServer.mb.createSourceSession(SourceSessionParams()
            .setThrottlePolicy(IThrottlePolicy::SP())
            .setReplyHandler(srcHandler));
    ASSERT_TRUE(srcSession.get() != NULL);
    ASSERT_TRUE(srcServer.waitSlobrok("dst/session", 1));

    for (uint32_t i = 0; i < 10; ++i) {
        Message::UP msg(new SimpleMessage("msg"));
        {
            TestServer itrServer(MessageBusParams()
                    .setRetryPolicy(IRetryPolicy::SP(new RetryTransientErrorsPolicy()))
                    .addProtocol(IProtocol::SP(new SimpleProtocol())),
                    RPCNetworkParams()
                    .setIdentity(Identity("itr"))
                    .setSlobrokConfig(slobrok.config()));
            Receptor itrHandler;
            IntermediateSession::UP itrSession = itrServer.mb.createIntermediateSession(
                    IntermediateSessionParams()
                    .setName("session")
                    .setMessageHandler(itrHandler)
                    .setReplyHandler(itrHandler));
            ASSERT_TRUE(itrSession.get() != NULL);
            ASSERT_TRUE(srcServer.waitSlobrok("itr/session", 1));
            ASSERT_TRUE(srcSession->send(std::move(msg), "itr/session dst/session", true).isAccepted());
            msg = itrHandler.getMessage(TIMEOUT);
            ASSERT_TRUE(msg.get() != NULL);
            itrSession->forward(std::move(msg));
            msg = dstHandler.getMessage(TIMEOUT);
            ASSERT_TRUE(msg.get() != NULL);
        }
        ASSERT_TRUE(srcServer.waitSlobrok("itr/session", 0));
        dstSession->acknowledge(std::move(msg));
        dstServer.mb.sync();
    }
}