-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.cc
313 lines (275 loc) · 11 KB
/
server_test.cc
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) 2017 Nuxi (https://nuxi.nl/) and contributors.
//
// SPDX-License-Identifier: BSD-2-Clause
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <cstdint>
#include <memory>
#include <thread>
#include <arpc++/arpc++.h>
#include <gtest/gtest.h>
#include <argdata.hpp>
#include "server_test_proto.ad.h"
TEST(Server, EndOfFile) {
// Close one half of a socket pair. Reading requests should return
// end-of-file, which is encoded as -1.
int fds[2];
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
EXPECT_EQ(0, close(fds[0]));
arpc::ServerBuilder builder(std::make_shared<arpc::FileDescriptor>(fds[1]));
EXPECT_EQ(-1, builder.Build()->HandleRequest());
}
TEST(Server, BadMessage) {
// A single byte does not correspond with a single message. The error
// EBADMSG generated by the Argdata reader should propagate.
int fds[2];
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
EXPECT_EQ(1, write(fds[0], "a", 1));
EXPECT_EQ(0, close(fds[0]));
arpc::ServerBuilder builder(std::make_shared<arpc::FileDescriptor>(fds[1]));
EXPECT_EQ(EBADMSG, builder.Build()->HandleRequest());
}
TEST(Server, InvalidOperation) {
// Writing some garbage Argdata should make the server return EOPNOTSUPP.
// TODO(ed): Should this just return an error to the client?
int fds[2];
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
std::unique_ptr<argdata_writer_t> writer = argdata_writer_t::create();
writer->set(argdata_t::null());
EXPECT_EQ(0, writer->push(fds[0]));
EXPECT_EQ(0, close(fds[0]));
arpc::ServerBuilder builder(std::make_shared<arpc::FileDescriptor>(fds[1]));
EXPECT_EQ(EOPNOTSUPP, builder.Build()->HandleRequest());
}
TEST(Server, ServiceNotRegistered) {
// Invoke an RPC on a server that has no services registered. Any RPC
// should fail with UNIMPLEMENTED to indicate the service's absence.
int fds[2];
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
std::shared_ptr<arpc::Channel> channel =
arpc::CreateChannel(std::make_shared<arpc::FileDescriptor>(fds[0]));
std::unique_ptr<server_test_proto::UnaryService::Stub> stub =
server_test_proto::UnaryService::NewStub(channel);
std::thread caller([&stub]() {
arpc::ClientContext context;
server_test_proto::UnaryInput input;
server_test_proto::UnaryOutput output;
arpc::Status status = stub->UnaryCall(&context, input, &output);
EXPECT_EQ(arpc::StatusCode::UNIMPLEMENTED, status.error_code());
EXPECT_EQ("Service not registered", status.error_message());
});
arpc::ServerBuilder builder(std::make_shared<arpc::FileDescriptor>(fds[1]));
EXPECT_EQ(0, builder.Build()->HandleRequest());
caller.join();
}
namespace {
// Simple service that does nothing more than echoing responses.
class EchoService final : public server_test_proto::UnaryService::Service {
public:
arpc::Status UnaryCall(arpc::ServerContext* context,
const server_test_proto::UnaryInput* request,
server_test_proto::UnaryOutput* response) override {
response->set_text(request->text());
response->set_file_descriptor(request->file_descriptor());
return arpc::Status::OK;
}
};
} // namespace
TEST(Server, UnaryEcho) {
// Invoke RPCs on the EchoService and check whether the input text
// properly ends up in the output.
int fds[2];
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
std::shared_ptr<arpc::Channel> channel =
arpc::CreateChannel(std::make_shared<arpc::FileDescriptor>(fds[0]));
std::unique_ptr<server_test_proto::UnaryService::Stub> stub =
server_test_proto::UnaryService::NewStub(channel);
std::thread caller([&stub]() {
arpc::ClientContext context;
server_test_proto::UnaryInput input;
server_test_proto::UnaryOutput output;
input.set_text("Hello, world!");
EXPECT_TRUE(stub->UnaryCall(&context, input, &output).ok());
EXPECT_EQ("Hello, world!", output.text());
input.set_text("Goodbye, world!");
EXPECT_TRUE(stub->UnaryCall(&context, input, &output).ok());
EXPECT_EQ("Goodbye, world!", output.text());
});
arpc::ServerBuilder builder(std::make_shared<arpc::FileDescriptor>(fds[1]));
EchoService service;
builder.RegisterService(&service);
std::shared_ptr<arpc::Server> server = builder.Build();
EXPECT_EQ(0, server->HandleRequest());
EXPECT_EQ(0, server->HandleRequest());
caller.join();
}
TEST(Server, UnaryFileDesciptorPassing) {
// Use the EchoService to pass a file descriptor back to us.
int fds[2];
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
std::shared_ptr<arpc::Channel> channel =
arpc::CreateChannel(std::make_shared<arpc::FileDescriptor>(fds[0]));
std::unique_ptr<server_test_proto::UnaryService::Stub> stub =
server_test_proto::UnaryService::NewStub(channel);
std::thread caller([&stub]() {
arpc::ClientContext context;
server_test_proto::UnaryInput input;
server_test_proto::UnaryOutput output;
// Write something into the pipe and send the read side to the
// EchoService.
int pfds[2];
EXPECT_EQ(0, pipe(pfds));
EXPECT_EQ(5, write(pfds[1], "Hello", 5));
EXPECT_EQ(0, close(pfds[1]));
input.set_file_descriptor(std::make_shared<arpc::FileDescriptor>(pfds[0]));
EXPECT_TRUE(stub->UnaryCall(&context, input, &output).ok());
// Original message should still be contained in the pipe.
char buf[6];
EXPECT_EQ(5, read(output.file_descriptor()->get(), buf, sizeof(buf)));
EXPECT_EQ("Hello", std::string_view(buf, 5));
});
arpc::ServerBuilder builder(std::make_shared<arpc::FileDescriptor>(fds[1]));
EchoService service;
builder.RegisterService(&service);
std::shared_ptr<arpc::Server> server = builder.Build();
EXPECT_EQ(0, server->HandleRequest());
caller.join();
}
namespace {
// Service that adds a stream of numbers.
class AdderService final
: public server_test_proto::ClientStreamAdderService::Service {
public:
arpc::Status Add(arpc::ServerContext* context,
arpc::ServerReader<server_test_proto::AdderInput>* reader,
server_test_proto::AdderOutput* response) override {
server_test_proto::AdderInput input;
std::int32_t sum = 0;
while (reader->Read(&input))
sum += input.value();
response->set_sum(sum);
return arpc::Status::OK;
}
};
} // namespace
TEST(Server, ClientStreamAdder) {
// Use the AdderService to add some numbers together.
int fds[2];
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
std::shared_ptr<arpc::Channel> channel =
arpc::CreateChannel(std::make_shared<arpc::FileDescriptor>(fds[0]));
std::unique_ptr<server_test_proto::ClientStreamAdderService::Stub> stub =
server_test_proto::ClientStreamAdderService::NewStub(channel);
std::thread caller([&stub]() {
arpc::ClientContext context;
server_test_proto::AdderInput input;
server_test_proto::AdderOutput output;
// Write numbers and extract the sum.
std::unique_ptr<arpc::ClientWriter<server_test_proto::AdderInput>> writer(
stub->Add(&context, &output));
input.set_value(237);
EXPECT_TRUE(writer->Write(input));
input.set_value(7845);
EXPECT_TRUE(writer->Write(input));
input.set_value(57592);
EXPECT_TRUE(writer->Write(input));
input.set_value(3);
EXPECT_TRUE(writer->Write(input));
input.set_value(7284);
EXPECT_TRUE(writer->Write(input));
EXPECT_TRUE(writer->WritesDone());
EXPECT_TRUE(writer->Finish().ok());
EXPECT_EQ(72961, output.sum());
});
arpc::ServerBuilder builder(std::make_shared<arpc::FileDescriptor>(fds[1]));
AdderService service;
builder.RegisterService(&service);
std::shared_ptr<arpc::Server> server = builder.Build();
EXPECT_EQ(0, server->HandleRequest());
caller.join();
}
namespace {
// Service that generates a stream of numbers.
class FibonacciService final
: public server_test_proto::ServerStreamFibonacciService::Service {
public:
arpc::Status GetSequence(
arpc::ServerContext* context,
const server_test_proto::FibonacciInput* request,
arpc::ServerWriter<server_test_proto::FibonacciOutput>* writer) override {
std::uint64_t a = request->a();
std::uint64_t b = request->b();
for (std::uint32_t i = 0; i < request->terms(); ++i) {
server_test_proto::FibonacciOutput output;
output.set_term(a);
if (!writer->Write(output))
break;
std::tie(a, b) = std::make_pair(b, a + b);
}
return arpc::Status::OK;
}
};
} // namespace
TEST(Server, ServerStreamFibonacci) {
// Use the FibonacciService to stream a sequence of messages to a client.
int fds[2];
EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
std::shared_ptr<arpc::Channel> channel =
arpc::CreateChannel(std::make_shared<arpc::FileDescriptor>(fds[0]));
EXPECT_EQ(ARPC_CHANNEL_READY, channel->GetState(false));
std::unique_ptr<server_test_proto::ServerStreamFibonacciService::Stub> stub =
server_test_proto::ServerStreamFibonacciService::NewStub(channel);
std::thread caller([&stub]() {
arpc::ClientContext context;
server_test_proto::FibonacciInput input;
server_test_proto::FibonacciOutput output;
// Request the first five Brady numbers (OEIS A247698).
input.set_a(2308);
input.set_b(4261);
input.set_terms(5);
std::unique_ptr<arpc::ClientReader<server_test_proto::FibonacciOutput>>
reader(stub->GetSequence(&context, input));
EXPECT_TRUE(reader->Read(&output));
EXPECT_EQ(2308, output.term());
EXPECT_TRUE(reader->Read(&output));
EXPECT_EQ(4261, output.term());
EXPECT_TRUE(reader->Read(&output));
EXPECT_EQ(6569, output.term());
EXPECT_TRUE(reader->Read(&output));
EXPECT_EQ(10830, output.term());
EXPECT_TRUE(reader->Read(&output));
EXPECT_EQ(17399, output.term());
EXPECT_FALSE(reader->Read(&output));
EXPECT_TRUE(reader->Finish().ok());
});
// Process a single request from a client. The channel should still be
// in the ready state after the RPC completes.
{
arpc::ServerBuilder builder(std::make_shared<arpc::FileDescriptor>(fds[1]));
FibonacciService service;
builder.RegisterService(&service);
std::shared_ptr<arpc::Server> server = builder.Build();
EXPECT_EQ(0, server->HandleRequest());
caller.join();
EXPECT_EQ(ARPC_CHANNEL_READY, channel->GetState(false));
}
// Destroying the server immediately causes the channel to be switched
// to the shut down state.
EXPECT_EQ(ARPC_CHANNEL_SHUTDOWN, channel->GetState(false));
// Sending another RPC after the server has terminated should cause
// the RPC to fail. The channel should remain in the shut down state.
{
arpc::ClientContext context;
server_test_proto::FibonacciInput input;
server_test_proto::FibonacciOutput output;
input.set_a(1);
input.set_b(1);
input.set_terms(5);
std::unique_ptr<arpc::ClientReader<server_test_proto::FibonacciOutput>>
reader(stub->GetSequence(&context, input));
EXPECT_FALSE(reader->Read(&output));
EXPECT_FALSE(reader->Finish().ok());
EXPECT_EQ(ARPC_CHANNEL_SHUTDOWN, channel->GetState(false));
}
}