aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/engine/proto_rpc_adapter/proto_rpc_adapter_test.cpp
blob: 6747e4e741e584472d5ed3012b75ac1032123bce (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
135
136
137
138
139
140
141
142
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/searchlib/engine/search_protocol_proto.h>
#include <vespa/searchlib/engine/proto_rpc_adapter.h>
#include <vespa/searchlib/engine/searchapi.h>
#include <vespa/searchlib/engine/docsumapi.h>
#include <vespa/searchlib/engine/monitorapi.h>
#include <vespa/fnet/frt/frt.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/data/slime/binary_format.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winline"

using namespace search::engine;

using vespalib::Slime;
using vespalib::Memory;
using vespalib::slime::BinaryFormat;

using ProtoSearchRequest = ProtoRpcAdapter::ProtoSearchRequest;
using ProtoSearchReply = ProtoRpcAdapter::ProtoSearchReply;
using ProtoDocsumRequest = ProtoRpcAdapter::ProtoDocsumRequest;
using ProtoDocsumReply = ProtoRpcAdapter::ProtoDocsumReply;
using ProtoMonitorRequest = ProtoRpcAdapter::ProtoMonitorRequest;
using ProtoMonitorReply = ProtoRpcAdapter::ProtoMonitorReply;

struct MySearchServer : SearchServer {
    SearchReply::UP search(SearchRequest::Source src, SearchClient &client) override {
        auto req = src.release();
        assert(req);
        auto reply = std::make_unique<SearchReply>();
        reply->totalHitCount = req->offset; // simplified search implementation
        client.searchDone(std::move(reply)); // simplified async response
        return std::unique_ptr<SearchReply>();
    }
};

struct MyDocsumServer : DocsumServer {
    DocsumReply::UP getDocsums(DocsumRequest::Source src, DocsumClient &client) override {
        auto req = src.release();
        assert(req);
        auto reply = std::make_unique<DocsumReply>();
        reply->_root = std::make_unique<Slime>();
        auto &list = reply->_root->setArray();
        list.addObject().setBool("use_root_slime", req->useRootSlime());
        list.addObject().setString("ranking", req->ranking);
        client.getDocsumsDone(std::move(reply)); // simplified async response
        return std::unique_ptr<DocsumReply>();
    }
};

struct MyMonitorServer : MonitorServer {
    MonitorReply::UP ping(MonitorRequest::UP req, MonitorClient &) override {
        (void) req;
        assert(req);
        auto reply = std::make_unique<MonitorReply>();
        reply->activeDocs = 53;
        return reply; // proton does sync response here
    }
};

struct ProtoRpcAdapterTest : ::testing::Test {
    fnet::frt::StandaloneFRT server;
    MySearchServer search;
    MyDocsumServer docsum;
    MyMonitorServer monitor;
    ProtoRpcAdapter adapter;
    ProtoRpcAdapterTest()
        : server(), adapter(search, docsum, monitor, server.supervisor())
    {
        server.supervisor().Listen(0);
    }
    FRT_Target *connect() {
        return server.supervisor().GetTarget(server.supervisor().GetListenPort());
    }
    ~ProtoRpcAdapterTest() = default;
};

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

TEST_F(ProtoRpcAdapterTest, require_that_plain_rpc_ping_works) {
    auto target = connect();
    auto *req = new FRT_RPCRequest();
    req->SetMethodName("frt.rpc.ping");
    target->InvokeSync(req, 60.0);
    EXPECT_TRUE(req->CheckReturnTypes(""));
    req->SubRef();
    target->SubRef();
}

TEST_F(ProtoRpcAdapterTest, require_that_proto_rpc_search_works) {
    auto target = connect();
    auto *rpc = new FRT_RPCRequest();
    ProtoSearchRequest req;
    req.set_offset(42);
    ProtoRpcAdapter::encode_search_request(req, *rpc);
    target->InvokeSync(rpc, 60.0);
    ProtoSearchReply reply;
    EXPECT_TRUE(ProtoRpcAdapter::decode_search_reply(*rpc, reply));
    EXPECT_EQ(reply.total_hit_count(), 42);
    rpc->SubRef();
    target->SubRef();
}

TEST_F(ProtoRpcAdapterTest, require_that_proto_rpc_getDocsums_works) {
    auto target = connect();
    auto *rpc = new FRT_RPCRequest();
    ProtoDocsumRequest req;
    req.set_rank_profile("mlr");
    ProtoRpcAdapter::encode_docsum_request(req, *rpc);
    target->InvokeSync(rpc, 60.0);
    ProtoDocsumReply reply;
    EXPECT_TRUE(ProtoRpcAdapter::decode_docsum_reply(*rpc, reply));
    const auto &mem = reply.slime_summaries();
    Slime slime;
    EXPECT_EQ(BinaryFormat::decode(Memory(mem.data(), mem.size()), slime), mem.size());
    EXPECT_EQ(slime.get()[0]["use_root_slime"].asBool(), true);
    EXPECT_EQ(slime.get()[1]["ranking"].asString().make_string(), "mlr");
    rpc->SubRef();
    target->SubRef();
}

TEST_F(ProtoRpcAdapterTest, require_that_proto_rpc_ping_works) {
    auto target = connect();
    auto *rpc = new FRT_RPCRequest();
    ProtoMonitorRequest req;
    ProtoRpcAdapter::encode_monitor_request(req, *rpc);
    target->InvokeSync(rpc, 60.0);
    ProtoMonitorReply reply;
    EXPECT_TRUE(ProtoRpcAdapter::decode_monitor_reply(*rpc, reply));
    EXPECT_EQ(reply.active_docs(), 53);
    rpc->SubRef();
    target->SubRef();
}

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

GTEST_MAIN_RUN_ALL_TESTS()

#pragma GCC diagnostic pop