-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_server_tests.cpp
More file actions
328 lines (267 loc) · 9.86 KB
/
Copy pathrpc_server_tests.cpp
File metadata and controls
328 lines (267 loc) · 9.86 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* @file rpc_server_tests.cpp
* @brief Unit tests for RpcServer - Internal RPC server for node-to-node communication
*/
#include <gtest/gtest.h>
#include <atomic>
#include <csignal>
#include <cstdint>
#include <cstring>
#include <memory>
#include <thread>
#include <vector>
#include "network/rpc_client.hpp"
#include "network/rpc_message.hpp"
#include "network/rpc_server.hpp"
using namespace cloudsql::network;
namespace {
// Ignore SIGPIPE to prevent crashes when writing to closed sockets
struct SigpipeGuard {
SigpipeGuard() { std::signal(SIGPIPE, SIG_IGN); }
};
SigpipeGuard g_sigpipe;
class RpcServerTests : public ::testing::Test {
protected:
void SetUp() override {
// Use a unique port for each test to avoid TIME_WAIT issues
port_ = TEST_PORT_BASE_ + next_port_++;
server_ = std::make_unique<RpcServer>(port_);
handler_called_ = false;
}
void TearDown() override {
if (server_) {
server_->stop();
}
// Small delay to allow socket to settle
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
static constexpr uint16_t TEST_PORT_BASE_ = 6300;
static std::atomic<uint16_t> next_port_;
uint16_t port_;
std::unique_ptr<RpcServer> server_;
std::atomic<bool> handler_called_{false};
};
std::atomic<uint16_t> RpcServerTests::next_port_{0};
TEST_F(RpcServerTests, LifecycleStartStop) {
ASSERT_TRUE(server_->start());
server_->stop();
ASSERT_TRUE(server_->start());
server_->stop();
}
TEST_F(RpcServerTests, DoubleStartReturnsFalse) {
ASSERT_TRUE(server_->start());
ASSERT_FALSE(server_->start());
// Force cleanup before next test
server_->stop();
// Give socket time to release
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
TEST_F(RpcServerTests, SetAndGetHandler) {
auto handler = [](const RpcHeader&, const std::vector<uint8_t>&, int) {};
server_->set_handler(RpcType::Heartbeat, handler);
auto retrieved = server_->get_handler(RpcType::Heartbeat);
ASSERT_NE(retrieved, nullptr);
}
TEST_F(RpcServerTests, GetHandlerNotSet) {
auto retrieved = server_->get_handler(RpcType::RegisterNode);
EXPECT_EQ(retrieved, nullptr);
}
TEST_F(RpcServerTests, HandlerOverride) {
int call_count = 0;
auto handler1 = [&](const RpcHeader&, const std::vector<uint8_t>&, int) { call_count++; };
auto handler2 = [&](const RpcHeader&, const std::vector<uint8_t>&, int) { call_count += 10; };
server_->set_handler(RpcType::Heartbeat, handler1);
server_->set_handler(RpcType::Heartbeat, handler2);
auto retrieved = server_->get_handler(RpcType::Heartbeat);
ASSERT_NE(retrieved, nullptr);
}
TEST_F(RpcServerTests, ClearHandlersAfterStop) {
auto handler = [](const RpcHeader&, const std::vector<uint8_t>&, int) {};
server_->set_handler(RpcType::Heartbeat, handler);
server_->start();
server_->stop();
auto retrieved = server_->get_handler(RpcType::Heartbeat);
EXPECT_EQ(retrieved, nullptr);
}
TEST_F(RpcServerTests, ZeroPayloadHandler) {
server_->start();
bool called = false;
server_->set_handler(RpcType::Heartbeat,
[&called](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
called = true;
EXPECT_EQ(p.size(), 0U);
});
// Connect and send RPC with zero payload
int fd = socket(AF_INET, SOCK_STREAM, 0);
ASSERT_GE(fd, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port_);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
ASSERT_EQ(connect(fd, (sockaddr*)&addr, sizeof(addr)), 0);
// Send header
RpcHeader hdr;
hdr.type = RpcType::Heartbeat;
hdr.payload_len = 0;
char h_buf[RpcHeader::HEADER_SIZE];
hdr.encode(h_buf);
send(fd, h_buf, RpcHeader::HEADER_SIZE, 0);
// Give time for the server to process and call the handler
for (int i = 0; i < 10 && !called; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
EXPECT_TRUE(called);
close(fd);
}
TEST_F(RpcServerTests, MultipleConnections) {
server_->start();
int call_count = 0;
server_->set_handler(
RpcType::Heartbeat,
[&call_count](const RpcHeader&, const std::vector<uint8_t>&, int) { call_count++; });
std::vector<int> fds;
for (int i = 0; i < 5; ++i) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port_);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
if (connect(fd, (sockaddr*)&addr, sizeof(addr)) == 0) {
fds.push_back(fd);
}
}
// Send RPCs
for (int fd : fds) {
RpcHeader hdr;
hdr.type = RpcType::Heartbeat;
hdr.payload_len = 0;
char h_buf[RpcHeader::HEADER_SIZE];
hdr.encode(h_buf);
send(fd, h_buf, RpcHeader::HEADER_SIZE, 0);
}
// Give time for the server to process all 5
for (int i = 0; i < 20 && call_count < 5; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
for (int fd : fds) {
close(fd);
}
EXPECT_EQ(call_count, 5);
}
TEST_F(RpcServerTests, ClientDisconnectMidHeader) {
server_->start();
server_->set_handler(RpcType::Heartbeat,
[](const RpcHeader&, const std::vector<uint8_t>&, int) {});
int fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port_);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
connect(fd, (sockaddr*)&addr, sizeof(addr));
// Send partial header then disconnect
char partial[6];
std::memset(partial, 0, 6);
send(fd, partial, 6, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
close(fd);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
TEST_F(RpcServerTests, ClientDisconnectMidPayload) {
server_->start();
server_->set_handler(RpcType::Heartbeat,
[](const RpcHeader&, const std::vector<uint8_t>&, int) {});
int fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port_);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
connect(fd, (sockaddr*)&addr, sizeof(addr));
// Send full header indicating payload but don't send payload
RpcHeader hdr;
hdr.type = RpcType::Heartbeat;
hdr.payload_len = 100; // Request 100 bytes but we won't send them
char h_buf[RpcHeader::HEADER_SIZE];
hdr.encode(h_buf);
send(fd, h_buf, RpcHeader::HEADER_SIZE, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
close(fd);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
TEST_F(RpcServerTests, FullRoundTripWithClient) {
server_->start();
server_->set_handler(RpcType::QueryResults,
[](const RpcHeader& h, const std::vector<uint8_t>& p, int fd) {
// Echo back the payload
RpcHeader resp_h;
resp_h.type = RpcType::QueryResults;
resp_h.payload_len = static_cast<uint16_t>(p.size());
char h_buf[RpcHeader::HEADER_SIZE];
resp_h.encode(h_buf);
send(fd, h_buf, RpcHeader::HEADER_SIZE, 0);
if (!p.empty()) {
send(fd, p.data(), p.size(), 0);
}
});
RpcClient client("127.0.0.1", port_);
ASSERT_TRUE(client.connect());
std::vector<uint8_t> payload = {1, 2, 3, 4, 5};
std::vector<uint8_t> response;
ASSERT_TRUE(client.call(RpcType::QueryResults, payload, response, 0));
EXPECT_EQ(response.size(), 5U);
EXPECT_EQ(response[0], 1);
EXPECT_EQ(response[4], 5);
}
TEST_F(RpcServerTests, NoHandlerRegistered) {
server_->start();
// Don't set any handler
int fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port_);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
connect(fd, (sockaddr*)&addr, sizeof(addr));
// Send an RPC with no handler registered
RpcHeader hdr;
hdr.type = RpcType::Error;
hdr.payload_len = 0;
char h_buf[RpcHeader::HEADER_SIZE];
hdr.encode(h_buf);
send(fd, h_buf, RpcHeader::HEADER_SIZE, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
close(fd);
server_->stop();
}
TEST_F(RpcServerTests, LargePayload) {
server_->start();
// Set a handler that tracks if it was called
server_->set_handler(RpcType::PushData, [&](const RpcHeader&, const std::vector<uint8_t>&,
int) { handler_called_ = true; });
int fd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port_);
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
connect(fd, (sockaddr*)&addr, sizeof(addr));
// Send large payload (10KB)
RpcHeader hdr;
hdr.type = RpcType::PushData;
hdr.payload_len = 10240;
char h_buf[RpcHeader::HEADER_SIZE];
hdr.encode(h_buf);
send(fd, h_buf, RpcHeader::HEADER_SIZE, 0);
// Send payload data
std::vector<char> large_data(10240, 'x');
send(fd, large_data.data(), large_data.size(), 0);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
EXPECT_TRUE(handler_called_);
close(fd);
server_->stop();
}
TEST_F(RpcServerTests, ServerOnSpecificPort) {
// Test server can bind to specific port
constexpr uint16_t specific_port = 6399;
auto specific_server = std::make_unique<RpcServer>(specific_port);
ASSERT_TRUE(specific_server->start());
specific_server->stop();
}
} // namespace