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

#include <vespa/messagebus/testlib/receptor.h>
#include <vespa/messagebus/testlib/simplemessage.h>
#include <vespa/messagebus/testlib/simplereply.h>
#include <vespa/messagebus/message.h>
#include <vespa/messagebus/reply.h>
#include <vespa/messagebus/errorcode.h>
#include <vespa/messagebus/error.h>

#include <vespa/vespalib/testkit/testapp.h>

using namespace mbus;
using namespace std::chrono_literals;

TEST_SETUP(Test);

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

    {
        // Test message swap state.
        SimpleMessage foo("foo");
        Route fooRoute = Route::parse("foo");
        foo.setRoute(fooRoute);
        foo.setRetry(1);
        foo.setTimeReceivedNow();
        foo.setTimeRemaining(2ms);

        SimpleMessage bar("bar");
        Route barRoute = Route::parse("bar");
        bar.setRoute(barRoute);
        bar.setRetry(3);
        bar.setTimeReceivedNow();
        bar.setTimeRemaining(4ms);

        foo.swapState(bar);
        EXPECT_EQUAL(barRoute.toString(), foo.getRoute().toString());
        EXPECT_EQUAL(fooRoute.toString(), bar.getRoute().toString());
        EXPECT_EQUAL(3u, foo.getRetry());
        EXPECT_EQUAL(1u, bar.getRetry());
        EXPECT_TRUE(foo.getTimeReceived() >= bar.getTimeReceived());
        EXPECT_EQUAL(4ms, foo.getTimeRemaining());
        EXPECT_EQUAL(2ms, bar.getTimeRemaining());
    }
    {
        // Test reply swap state.
        SimpleReply foo("foo");
        foo.setMessage(Message::UP(new SimpleMessage("foo")));
        foo.setRetryDelay(1);
        foo.addError(Error(ErrorCode::APP_FATAL_ERROR, "fatal"));
        foo.addError(Error(ErrorCode::APP_TRANSIENT_ERROR, "transient"));

        SimpleReply bar("bar");
        bar.setMessage(Message::UP(new SimpleMessage("bar")));
        bar.setRetryDelay(2);
        bar.addError(Error(ErrorCode::ERROR_LIMIT, "err"));

        foo.swapState(bar);
        EXPECT_EQUAL("bar", static_cast<SimpleMessage&>(*foo.getMessage()).getValue());
        EXPECT_EQUAL("foo", static_cast<SimpleMessage&>(*bar.getMessage()).getValue());
        EXPECT_EQUAL(2.0, foo.getRetryDelay());
        EXPECT_EQUAL(1.0, bar.getRetryDelay());
        EXPECT_EQUAL(1u, foo.getNumErrors());
        EXPECT_EQUAL(2u, bar.getNumErrors());
    }
    {
        // Test message discard logic.
        Receptor handler;
        SimpleMessage msg("foo");
        msg.pushHandler(handler);
        msg.discard();

        Reply::UP reply = handler.getReplyNow();
        ASSERT_FALSE(reply);
    }
    {
        // Test reply discard logic.
        Receptor handler;
        SimpleMessage msg("foo");
        msg.pushHandler(handler);

        SimpleReply reply("bar");
        reply.swapState(msg);
        reply.discard();

        Reply::UP ap = handler.getReplyNow();
        ASSERT_FALSE(ap);
    }

    TEST_DONE();
}