aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/tests/trace-roundtrip/trace-roundtrip.cpp
blob: 35f232ba5616679e1f8d32af25de2072e66107c6 (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
// 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/emptyreply.h>
#include <vespa/messagebus/messagebus.h>
#include <vespa/messagebus/sourcesession.h>
#include <vespa/messagebus/intermediatesession.h>
#include <vespa/messagebus/destinationsession.h>
#include <vespa/messagebus/testlib/slobrok.h>
#include <vespa/messagebus/testlib/testserver.h>
#include <vespa/messagebus/routing/routingspec.h>
#include <vespa/messagebus/testlib/receptor.h>
#include <vespa/messagebus/sourcesessionparams.h>
#include <vespa/messagebus/testlib/simplemessage.h>

using namespace mbus;

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

class Proxy : public IMessageHandler,
              public IReplyHandler
{
private:
    IntermediateSession::UP _session;
public:
    Proxy(MessageBus &bus);
    void handleMessage(Message::UP msg) override;
    void handleReply(Reply::UP reply) override;
};

Proxy::Proxy(MessageBus &bus)
    : _session(bus.createIntermediateSession("session", true, *this, *this))
{
}

void
Proxy::handleMessage(Message::UP msg) {
    msg->getTrace().trace(1, "Proxy message", false);
    _session->forward(std::move(msg));
}

void
Proxy::handleReply(Reply::UP reply) {
    reply->getTrace().trace(1, "Proxy reply", false);
    _session->forward(std::move(reply));
}

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

class Server : public IMessageHandler
{
private:
    DestinationSession::UP _session;
public:
    Server(MessageBus &bus);
    void handleMessage(Message::UP msg) override;
};

Server::Server(MessageBus &bus)
    : _session(bus.createDestinationSession("session", true, *this))
{
}

void
Server::handleMessage(Message::UP msg) {
    msg->getTrace().trace(1, "Server message", false);
    Reply::UP reply(new EmptyReply());
    msg->swapState(*reply);
    reply->getTrace().trace(1, "Server reply", false);
    _session->reply(std::move(reply));
}

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

TEST_SETUP(Test);

RoutingSpec getRouting() {
    return RoutingSpec()
        .addTable(RoutingTableSpec("Simple")
                  .addHop(HopSpec("pxy", "test/pxy/session"))
                  .addHop(HopSpec("dst", "test/dst/session"))
                  .addRoute(RouteSpec("test").addHop("pxy").addHop("dst")));
}

int
Test::Main()
{
    TEST_INIT("simple-roundtrip_test");

    Slobrok     slobrok;
    TestServer  srcNet(Identity("test/src"), getRouting(), slobrok);
    TestServer  pxyNet(Identity("test/pxy"), getRouting(), slobrok);
    TestServer  dstNet(Identity("test/dst"), getRouting(), slobrok);

    Receptor    src;
    Proxy       pxy(pxyNet.mb);
    Server      dst(dstNet.mb);

    SourceSession::UP ss = srcNet.mb.createSourceSession(src, SourceSessionParams());

    // wait for slobrok registration
    ASSERT_TRUE(srcNet.waitSlobrok("test/pxy/session"));
    ASSERT_TRUE(srcNet.waitSlobrok("test/dst/session"));
    ASSERT_TRUE(pxyNet.waitSlobrok("test/dst/session"));

    Message::UP msg(new SimpleMessage(""));
    msg->getTrace().setLevel(1);
    msg->getTrace().trace(1, "Client message", false);
    ss->send(std::move(msg), "test");
    Reply::UP reply = src.getReply();
    reply->getTrace().trace(1, "Client reply", false);
    EXPECT_TRUE(reply->getNumErrors() == 0);

    TraceNode t = TraceNode()
                  .addChild("Client message")
                  .addChild("Proxy message")
                  .addChild("Server message")
                  .addChild("Server reply")
                  .addChild("Proxy reply")
                  .addChild("Client reply");
    EXPECT_TRUE(reply->getTrace().encode() == t.encode());
    TEST_DONE();
}