aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/tests/file_acquirer/file_acquirer_test.cpp
blob: 7ea6556c0745412c4238adf9819d2fd89f6791a8 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/config/file_acquirer/file_acquirer.h>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/rpcrequest.h>
#include <vespa/fnet/transport.h>
#include <vespa/vespalib/util/stringfmt.h>

using namespace config;

struct ServerFixture : FRT_Invokable {
    fnet::frt::StandaloneFRT server;
    FNET_Transport transport;
    FRT_Supervisor &orb;
    vespalib::string spec;

    void init_rpc() {
        FRT_ReflectionBuilder rb(&orb);
        rb.DefineMethod("waitFor", "s", "s", FRT_METHOD(ServerFixture::RPC_waitFor), this);
        rb.MethodDesc("wait for and resolve file reference");
        rb.ParamDesc("file_ref", "file reference to wait for and resolve");
        rb.ReturnDesc("file_path", "actual path to the requested file");
    }

    ServerFixture()
        : server(),
          transport(),
          orb(server.supervisor())
    {
        init_rpc();
        orb.Listen(0);
        spec = vespalib::make_string("tcp/localhost:%d", orb.GetListenPort());
        transport.Start();
    }

    void RPC_waitFor(FRT_RPCRequest *req) {
        FRT_Values &params = *req->GetParams();
        FRT_Values &ret = *req->GetReturn();
        if (strcmp(params[0]._string._str, "my_ref") == 0) {
            ret.AddString("my_path");
        } else {
            req->SetError(FRTE_RPC_METHOD_FAILED, "invalid file reference");
        }
    }

    ~ServerFixture() {
        transport.ShutDown(true);
    }
};

TEST_FF("require that files can be acquired over rpc", ServerFixture(), RpcFileAcquirer(f1.transport, f1.spec)) {
    EXPECT_EQUAL("my_path", f2.wait_for("my_ref", 60.0));
    EXPECT_EQUAL("", f2.wait_for("bogus_ref", 60.0));
}

TEST_MAIN() { TEST_RUN_ALL(); }