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

#include <vespa/messagebus/result.h>
#include <vespa/messagebus/error.h>
#include <vespa/messagebus/errorcode.h>
#include <vespa/messagebus/testlib/simplemessage.h>
#include <vespa/vespalib/gtest/gtest.h>

using namespace mbus;

namespace {

struct MyMessage : public SimpleMessage
{
    static int ctorCnt;
    static int dtorCnt;
    MyMessage(const string &str) : SimpleMessage(str) {
        ++ctorCnt;
    }
    virtual ~MyMessage() {
        ++dtorCnt;
    }
};
int MyMessage::ctorCnt = 0;
int MyMessage::dtorCnt = 0;

Result
sendOk(Message::UP msg)
{
    (void) msg;
    return Result();
}

Result
sendFail(Message::UP msg)
{
    return Result(Error(ErrorCode::FATAL_ERROR, "error"), std::move(msg));
}

}

TEST(ResultTest, test_result)
{
    { // test accepted
        Message::UP msg(new MyMessage("test"));
        Result res = sendOk(std::move(msg));
        EXPECT_TRUE(msg.get() == 0);
        EXPECT_TRUE(res.isAccepted());
        EXPECT_TRUE(res.getError().getCode() == ErrorCode::NONE);
        EXPECT_TRUE(res.getError().getMessage() == "");
        Message::UP back = res.getMessage();
        EXPECT_TRUE(back.get() == 0);
    }
    { // test failed
        Message::UP msg(new MyMessage("test"));
        Message *raw = msg.get();
        EXPECT_TRUE(raw != 0);
        Result res = sendFail(std::move(msg));
        EXPECT_TRUE(msg.get() == 0);
        EXPECT_TRUE(!res.isAccepted());
        EXPECT_TRUE(res.getError().getCode() == ErrorCode::FATAL_ERROR);
        EXPECT_TRUE(res.getError().getMessage() == "error");
        Message::UP back = res.getMessage();
        EXPECT_TRUE(back.get() == raw);
    }
    EXPECT_TRUE(MyMessage::ctorCnt == 2);
    EXPECT_TRUE(MyMessage::dtorCnt == 2);
}

GTEST_MAIN_RUN_ALL_TESTS()