summaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/feedtoken/feedtoken.cpp
blob: 9df65ae3437773b254d25a3936427537349f608e (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/messagebus/emptyreply.h>
#include <vespa/messagebus/testlib/receptor.h>
#include <vespa/documentapi/messagebus/messages/removedocumentreply.h>
#include <vespa/searchcore/proton/common/feedtoken.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/exceptions.h>

using namespace proton;

class LocalTransport : public FeedToken::ITransport {
private:
    mbus::Receptor _receptor;
    double         _latency_ms;

public:
    LocalTransport()
        : _receptor(),
          _latency_ms(0.0)
    {
        // empty
    }

    void send(mbus::Reply::UP reply, ResultUP, bool, double latency_ms) override {
        _receptor.handleReply(std::move(reply));
        _latency_ms = latency_ms;
    }

    mbus::Reply::UP getReply() {
        return _receptor.getReply();
    }

    double getLatencyMs() const {
        return _latency_ms;
    }
};

class Test : public vespalib::TestApp {
private:
    void testAck();
    void testAutoReply();
    void testFail();
    void testHandover();
    void testIntegrity();

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

        testAck();       TEST_FLUSH();
//        testAutoReply(); TEST_FLUSH();
        testFail();      TEST_FLUSH();
        testHandover();  TEST_FLUSH();
//        testIntegrity(); TEST_FLUSH();

        TEST_DONE();
    }
};

TEST_APPHOOK(Test);

void
Test::testAck()
{
    LocalTransport transport;
    mbus::Reply::UP msg(new documentapi::RemoveDocumentReply());
    FeedToken token(transport, std::move(msg));
    token.ack();
    mbus::Reply::UP reply = transport.getReply();
    ASSERT_TRUE(reply.get() != NULL);
    EXPECT_TRUE(!reply->hasErrors());
}

void
Test::testAutoReply()
{
    mbus::Receptor receptor;
    mbus::Reply::UP reply(new documentapi::RemoveDocumentReply());
    reply->pushHandler(receptor);
    {
        LocalTransport transport;
        FeedToken token(transport, std::move(reply));
    }
    reply = receptor.getReply(0);
    ASSERT_TRUE(reply.get() != NULL);
    EXPECT_TRUE(reply->hasErrors());
}

void
Test::testFail()
{
    LocalTransport transport;
    mbus::Reply::UP reply(new documentapi::RemoveDocumentReply());
    FeedToken token(transport, std::move(reply));
    token.fail(69, "6699");
    reply = transport.getReply();
    ASSERT_TRUE(reply.get() != NULL);
    EXPECT_EQUAL(1u, reply->getNumErrors());
    EXPECT_EQUAL(69u, reply->getError(0).getCode());
    EXPECT_EQUAL("6699", reply->getError(0).getMessage());
}

void
Test::testHandover()
{
    struct MyHandover {
        static FeedToken handover(FeedToken token) {
            return token;
        }
    };

    LocalTransport transport;
    mbus::Reply::UP reply(new documentapi::RemoveDocumentReply());

    FeedToken token(transport, std::move(reply));
    token = MyHandover::handover(token);
    token.ack();
    reply = transport.getReply();
    ASSERT_TRUE(reply.get() != NULL);
    EXPECT_TRUE(!reply->hasErrors());
}

void
Test::testIntegrity()
{
    LocalTransport transport;
    try {
        FeedToken token(transport, mbus::Reply::UP());
        EXPECT_TRUE(false); // should throw an exception
    } catch (vespalib::IllegalArgumentException &e) {
        (void)e; // expected
    }
}