summaryrefslogtreecommitdiffstats
path: root/messagebus/src/tests/timeout/timeout.cpp
blob: 497af9c5c0e76c74959bc727f1f935fddb660f54 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/messagebus/errorcode.h>
#include <vespa/messagebus/emptyreply.h>
#include <vespa/messagebus/sourcesession.h>
#include <vespa/messagebus/sourcesessionparams.h>
#include <vespa/messagebus/destinationsession.h>
#include <vespa/messagebus/network/identity.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/gtest/gtest.h>

using namespace mbus;
using namespace std::chrono_literals;


TEST(TimeoutTest, test_zero_timeout)
{
    Slobrok slobrok;
    TestServer srcServer(Identity("src"), RoutingSpec(), slobrok);
    TestServer dstServer(Identity("dst"), RoutingSpec(), slobrok);

    Receptor srcHandler;
    SourceSession::UP srcSession = srcServer.mb.createSourceSession(srcHandler, SourceSessionParams().setTimeout(0s));
    Receptor dstHandler;
    DestinationSession::UP dstSession = dstServer.mb.createDestinationSession("session", true, dstHandler);

    ASSERT_TRUE(srcServer.waitSlobrok("dst/session", 1));
    ASSERT_TRUE(srcSession->send(Message::UP(new SimpleMessage("msg")), "dst/session", true).isAccepted());

    Reply::UP reply = srcHandler.getReply();
    ASSERT_TRUE(reply);
    EXPECT_EQ(1u, reply->getNumErrors());
    EXPECT_EQ((uint32_t)ErrorCode::TIMEOUT, reply->getError(0).getCode());
}

TEST(TimeoutTest, test_message_expires)
{
    Slobrok slobrok;
    TestServer srcServer(Identity("src"), RoutingSpec(), slobrok);
    TestServer dstServer(Identity("dst"), RoutingSpec(), slobrok);

    Receptor srcHandler, dstHandler;
    SourceSession::UP srcSession = srcServer.mb.createSourceSession(srcHandler, SourceSessionParams().setTimeout(1s));
    DestinationSession::UP dstSession = dstServer.mb.createDestinationSession("session", true, dstHandler);

    ASSERT_TRUE(srcServer.waitSlobrok("dst/session", 1));
    ASSERT_TRUE(srcSession->send(Message::UP(new SimpleMessage("msg")), "dst/session", true).isAccepted());

    Reply::UP reply = srcHandler.getReply();
    ASSERT_TRUE(reply);
    EXPECT_EQ(1u, reply->getNumErrors());
    EXPECT_EQ((uint32_t)ErrorCode::TIMEOUT, reply->getError(0).getCode());

    Message::UP msg = dstHandler.getMessage(1s);
    if (msg) {
        msg->discard();
    }
}

GTEST_MAIN_RUN_ALL_TESTS()