aboutsummaryrefslogtreecommitdiffstats
path: root/fnet/src/tests/frt/rpc/sharedblob.cpp
blob: 94f57a136a448bb4ee8043d02047eed47f70b801 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// 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/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/target.h>
#include <vespa/fnet/frt/rpcrequest.h>
#include <vector>

constexpr size_t ALLOC_LIMIT=1024;

struct MyBlob : FRT_ISharedBlob
{
    int refcnt;
    MyBlob() : refcnt(1) {}
    uint32_t getLen() override { return (strlen("blob_test") + 1); }
    const char *getData() override { return "blob_test"; }
    void addRef() override { ++refcnt; }
    void subRef() override { --refcnt; }
};

struct Data
{
    enum {
        SMALL = (ALLOC_LIMIT / 2),
        LARGE = (ALLOC_LIMIT * 2)
    };

    char    *buf;
    uint32_t len;

    Data(const char *pt, uint32_t l) : buf(new char[l]), len(l) {
        if (len > 0) {
            memcpy(buf, pt, len);
        }
    }
    Data(uint32_t l, char c) : buf(new char[l]), len(l) {
        if (len > 0) {
            memset(buf, c, len);
        }
    }
    Data(const Data &rhs) : buf(new char[rhs.len]), len(rhs.len) {
        if (len > 0) {
            memcpy(buf, rhs.buf, len);
        }
    }
    Data &operator=(const Data &rhs) {
        if (this != &rhs) {
            delete [] buf;
            buf = new char[rhs.len];
            len = rhs.len;
            if (len > 0) {
                memcpy(buf, rhs.buf, len);
            }
        }
        return *this;
    }
    bool check(uint32_t l, char c) {
        if (l != len) {
            fprintf(stderr, "blob length was %u, expected %u\n", len, l);
            return false;
        }
        for (uint32_t i = 0; i < l; ++i) {
            if (buf[i] != c) {
                fprintf(stderr, "byte at offset %u was %c, expected %c\n", i, buf[i], c);
                return false;
            }
        }
        return true;
    }
    ~Data() {
        delete [] buf;
    }
};

struct DataSet
{
    std::vector<Data> blobs;

    void sample(FRT_Values &v) {
        blobs.push_back(Data(v.GetNumValues(), 'V'));
        for (uint32_t i = 0; i < v.GetNumValues(); ++i) {
            if (v.GetType(i) == FRT_VALUE_DATA) {
                blobs.push_back(Data(1, 'x'));
                blobs.push_back(Data(v[i]._data._buf, v[i]._data._len));
            } else if (v.GetType(i) == FRT_VALUE_DATA_ARRAY) {
                blobs.push_back(Data(v[i]._data_array._len, 'X'));
                for (uint32_t j = 0; j < v[i]._data_array._len; ++j) {
                    blobs.push_back(Data(v[i]._data_array._pt[j]._buf,
                                            v[i]._data_array._pt[j]._len));
                }
            }
        }
    }
};

struct ServerSampler : public FRT_Invokable
{
    DataSet        &dataSet;
    FRT_RPCRequest *clientReq;
    FRT_RPCRequest *serverReq;

    ServerSampler(DataSet &ds, FRT_RPCRequest *cr) : dataSet(ds), clientReq(cr), serverReq(0) {}

    void RPC_test(FRT_RPCRequest *req)
    {
        if (clientReq != 0) {
            dataSet.sample(*clientReq->GetParams()); // client params after drop
        }

        // store away parameters
        FNET_DataBuffer buf;
        buf.EnsureFree(req->GetParams()->GetLength());
        req->GetParams()->EncodeCopy(&buf);

        dataSet.sample(*req->GetParams()); // server params before drop
        req->DiscardBlobs();
        dataSet.sample(*req->GetParams()); // server params after drop

        // restore parameters into return values
        req->GetReturn()->DecodeCopy(&buf, buf.GetDataLen());

        dataSet.sample(*req->GetReturn()); // server return before drop

        // keep request to sample return after drop
        req->internal_addref();
        serverReq = req;
    }
};

TEST("testExplicitShared") {
    fnet::frt::StandaloneFRT frt;
    FRT_Supervisor & orb = frt.supervisor();
    MyBlob         blob;

    FRT_RPCRequest *req = orb.AllocRPCRequest();
    EXPECT_TRUE(blob.refcnt == 1);

    req->GetParams()->AddSharedData(&blob);
    req->GetParams()->AddInt32(42);
    req->GetParams()->AddSharedData(&blob);
    req->GetParams()->AddInt32(84);
    req->GetParams()->AddSharedData(&blob);

    EXPECT_EQUAL(4, blob.refcnt);
    EXPECT_TRUE(strcmp(req->GetParamSpec(), "xixix") == 0);
    EXPECT_TRUE(req->GetParams()->GetValue(0)._data._len == blob.getLen());
    EXPECT_TRUE(req->GetParams()->GetValue(0)._data._buf == blob.getData());
    EXPECT_TRUE(req->GetParams()->GetValue(1)._intval32 == 42);
    EXPECT_TRUE(req->GetParams()->GetValue(2)._data._len == blob.getLen());
    EXPECT_TRUE(req->GetParams()->GetValue(2)._data._buf == blob.getData());
    EXPECT_TRUE(req->GetParams()->GetValue(3)._intval32 == 84);
    EXPECT_TRUE(req->GetParams()->GetValue(4)._data._len == blob.getLen());
    EXPECT_TRUE(req->GetParams()->GetValue(4)._data._buf == blob.getData());

    req->CreateRequestPacket(true)->Free(); // fake request send.

    EXPECT_EQUAL(1, blob.refcnt);
    EXPECT_TRUE(strcmp(req->GetParamSpec(), "xixix") == 0);
    EXPECT_TRUE(req->GetParams()->GetValue(0)._data._len == 0);
    EXPECT_TRUE(req->GetParams()->GetValue(0)._data._buf == nullptr);
    EXPECT_TRUE(req->GetParams()->GetValue(1)._intval32 == 42);
    EXPECT_TRUE(req->GetParams()->GetValue(2)._data._len == 0);
    EXPECT_TRUE(req->GetParams()->GetValue(2)._data._buf == nullptr);
    EXPECT_TRUE(req->GetParams()->GetValue(3)._intval32 == 84);
    EXPECT_TRUE(req->GetParams()->GetValue(4)._data._len == 0);
    EXPECT_TRUE(req->GetParams()->GetValue(4)._data._buf == nullptr);

    EXPECT_EQUAL(1, blob.refcnt);
    req = orb.AllocRPCRequest(req);
    EXPECT_EQUAL(1, blob.refcnt);

    req->GetParams()->AddSharedData(&blob);
    req->GetParams()->AddInt32(42);
    req->GetParams()->AddSharedData(&blob);
    req->GetParams()->AddInt32(84);
    req->GetParams()->AddSharedData(&blob);

    EXPECT_EQUAL(4, blob.refcnt);
    req->internal_subref();
    EXPECT_EQUAL(1, blob.refcnt);
}

TEST("testImplicitShared") {
    DataSet dataSet;
    fnet::frt::StandaloneFRT frt;
    FRT_Supervisor & orb = frt.supervisor();
    FRT_RPCRequest *req = orb.AllocRPCRequest();
    ServerSampler serverSampler(dataSet, req);
    {
        FRT_ReflectionBuilder rb(&orb);
        rb.DefineMethod("test", "*", "*",
                        FRT_METHOD(ServerSampler::RPC_test), &serverSampler);
    }
    orb.Listen(0);
    int port = orb.GetListenPort();
    ASSERT_TRUE(port != 0);

    char tmp[64];
    snprintf(tmp, sizeof(tmp), "tcp/localhost:%d", port);
    FRT_Target *target = orb.GetTarget(tmp);
    req->SetMethodName("test");
    {
        Data data(Data::SMALL, 'a');
        req->GetParams()->AddData(data.buf, data.len);
    }
    {
        Data data(Data::LARGE, 'b');
        req->GetParams()->AddData(data.buf, data.len);
    }
    {
        char *data = req->GetParams()->AddData(Data::LARGE);
        memset(data, 'c', Data::LARGE);
    }
    {
        Data data1(Data::SMALL, 'd');
        Data data2(Data::LARGE, 'e');
        FRT_DataValue *arr = req->GetParams()->AddDataArray(2);
        req->GetParams()->SetData(&arr[0], data1.buf, data1.len);
        req->GetParams()->SetData(&arr[1], data2.buf, data2.len);
    }

    dataSet.sample(*req->GetParams()); // client params before drop

    target->InvokeSync(req, 30.0);

    if (serverSampler.serverReq != 0) {
        dataSet.sample(*serverSampler.serverReq->GetReturn()); // server return after drop
    }
    dataSet.sample(*req->GetReturn()); // client return before drop

    req->DiscardBlobs();

    dataSet.sample(*req->GetReturn()); // client return after drop

    // verify blob samples
    EXPECT_EQUAL(dataSet.blobs.size(), 80u);

    for (int i = 0; i < 80; i += 20) {
        // before discard (client params, server params, server return, client return)
        EXPECT_TRUE(dataSet.blobs[i + 0].check(4, 'V'));
        EXPECT_TRUE(dataSet.blobs[i + 1].check(1, 'x'));
        EXPECT_TRUE(dataSet.blobs[i + 2].check(Data::SMALL, 'a'));
        EXPECT_TRUE(dataSet.blobs[i + 3].check(1, 'x'));
        EXPECT_TRUE(dataSet.blobs[i + 4].check(Data::LARGE, 'b'));
        EXPECT_TRUE(dataSet.blobs[i + 5].check(1, 'x'));
        EXPECT_TRUE(dataSet.blobs[i + 6].check(Data::LARGE, 'c'));
        EXPECT_TRUE(dataSet.blobs[i + 7].check(2, 'X'));
        EXPECT_TRUE(dataSet.blobs[i + 8].check(Data::SMALL, 'd'));
        EXPECT_TRUE(dataSet.blobs[i + 9].check(Data::LARGE, 'e'));

        // after discard (client params, server params, server return, client return)
        EXPECT_TRUE(dataSet.blobs[i + 10].check(4, 'V'));
        EXPECT_TRUE(dataSet.blobs[i + 11].check(1, 'x'));
        EXPECT_TRUE(dataSet.blobs[i + 12].check(Data::SMALL, 'a'));
        EXPECT_TRUE(dataSet.blobs[i + 13].check(1, 'x'));
        EXPECT_TRUE(dataSet.blobs[i + 14].check(0, 0));
        EXPECT_TRUE(dataSet.blobs[i + 15].check(1, 'x'));
        EXPECT_TRUE(dataSet.blobs[i + 16].check(0, 0));
        EXPECT_TRUE(dataSet.blobs[i + 17].check(2, 'X'));
        EXPECT_TRUE(dataSet.blobs[i + 18].check(Data::SMALL, 'd'));
        EXPECT_TRUE(dataSet.blobs[i + 19].check(0, 0));
    }

    if (serverSampler.serverReq != 0) {
        serverSampler.serverReq->internal_subref();
    }
    req->internal_subref();
    target->internal_subref();
}

TEST_MAIN() { TEST_RUN_ALL(); }