Repository navigation
Expand file tree
/
Copy pathtest_rpc.cpp
More file actions
468 lines (371 loc) · 16.7 KB
/
Copy pathtest_rpc.cpp
File metadata and controls
468 lines (371 loc) · 16.7 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <livekit/livekit.h>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <mutex>
#include <random>
#include <string>
#include <thread>
#include <vector>
#include "../common/test_common.h"
namespace livekit::test {
using namespace std::chrono_literals;
// Maximum RPC payload size (15KB)
constexpr size_t kMaxRpcPayloadSize = 15 * 1024;
// Test configuration from environment variables
struct RpcTestConfig {
std::string url;
std::string token_a;
std::string token_b;
bool available = false;
static RpcTestConfig fromEnv() {
RpcTestConfig config;
const char* url = std::getenv("LIVEKIT_URL");
const char* token_a = std::getenv("LIVEKIT_TOKEN_A");
const char* token_b = std::getenv("LIVEKIT_TOKEN_B");
if (url && token_a && token_b) {
config.url = url;
config.token_a = token_a;
config.token_b = token_b;
config.available = true;
}
return config;
}
};
// Sample sentences for generating compressible payloads
static const std::vector<std::string> kSampleSentences = {
"The quick brown fox jumps over the lazy dog. ",
"LiveKit is a real-time communication platform for building video and "
"audio applications. ",
"RPC allows participants to call methods on remote peers with "
"request-response semantics. ",
"This test measures the performance and reliability of the RPC system. ",
"WebRTC enables peer-to-peer communication for real-time media streaming. ",
};
// Generate a payload of specified size using repeating sentences (compressible)
std::string generateRandomPayload(size_t size) {
static std::random_device rd;
static std::mt19937 gen(static_cast<std::mt19937::result_type>(rd()));
static std::uniform_int_distribution<size_t> dis(0, kSampleSentences.size() - 1);
std::string result;
result.reserve(size);
// Start with a random sentence to add some variation between payloads
size_t start_idx = dis(gen);
while (result.size() < size) {
const std::string& sentence = kSampleSentences[(start_idx + result.size()) % kSampleSentences.size()];
result += sentence;
}
return result.substr(0, size);
}
class RpcIntegrationTest : public ::testing::Test {
protected:
void SetUp() override {
livekit::initialize(livekit::LogLevel::Info);
config_ = RpcTestConfig::fromEnv();
}
void TearDown() override { livekit::shutdown(); }
RpcTestConfig config_;
};
// Test basic RPC round-trip
TEST_F(RpcIntegrationTest, BasicRpcRoundTrip) {
if (!config_.available) {
GTEST_SKIP() << "LIVEKIT_URL, LIVEKIT_TOKEN_A, and LIVEKIT_TOKEN_B not set";
}
// Create receiver room
auto receiver_room = std::make_unique<Room>();
RoomOptions receiver_options;
receiver_options.auto_subscribe = true;
bool receiver_connected = receiver_room->connect(config_.url, config_.token_b, receiver_options);
ASSERT_TRUE(receiver_connected) << "Receiver failed to connect";
std::string receiver_identity = receiver_room->localParticipant()->identity();
// Register RPC handler on receiver - returns size and checksum instead of
// full payload
std::atomic<int> rpc_calls_received{0};
receiver_room->localParticipant()->registerRpcMethod(
"echo", [&rpc_calls_received](const RpcInvocationData& data) -> std::optional<std::string> {
rpc_calls_received++;
size_t checksum = 0;
for (char c : data.payload) {
checksum += static_cast<unsigned char>(c);
}
return "echo:" + std::to_string(data.payload.size()) + ":" + std::to_string(checksum);
});
// Create caller room
auto caller_room = std::make_unique<Room>();
RoomOptions caller_options;
caller_options.auto_subscribe = true;
bool caller_connected = caller_room->connect(config_.url, config_.token_a, caller_options);
ASSERT_TRUE(caller_connected) << "Caller failed to connect";
// Wait for receiver to be visible to caller
bool receiver_visible = waitForParticipant(caller_room.get(), receiver_identity, 10s);
ASSERT_TRUE(receiver_visible) << "Receiver not visible to caller";
// Perform RPC call
std::string test_payload = "hello world";
std::string response = caller_room->localParticipant()->performRpc(receiver_identity, "echo", test_payload, 10.0);
// Verify response contains correct size and checksum
size_t expected_checksum = 0;
for (char c : test_payload) {
expected_checksum += static_cast<unsigned char>(c);
}
std::string expected_response =
"echo:" + std::to_string(test_payload.size()) + ":" + std::to_string(expected_checksum);
EXPECT_EQ(response, expected_response);
EXPECT_EQ(rpc_calls_received.load(), 1);
// Cleanup
receiver_room->localParticipant()->unregisterRpcMethod("echo");
caller_room.reset();
receiver_room.reset();
}
// Test maximum payload size (15KB)
TEST_F(RpcIntegrationTest, MaxPayloadSize) {
if (!config_.available) {
GTEST_SKIP() << "LIVEKIT_URL, LIVEKIT_TOKEN_A, and LIVEKIT_TOKEN_B not set";
}
auto receiver_room = std::make_unique<Room>();
RoomOptions options;
options.auto_subscribe = true;
bool receiver_connected = receiver_room->connect(config_.url, config_.token_b, options);
ASSERT_TRUE(receiver_connected) << "Receiver failed to connect";
std::string receiver_identity = receiver_room->localParticipant()->identity();
// Register handler that echoes payload size
receiver_room->localParticipant()->registerRpcMethod(
"payload-size",
[](const RpcInvocationData& data) -> std::optional<std::string> { return std::to_string(data.payload.size()); });
auto caller_room = std::make_unique<Room>();
bool caller_connected = caller_room->connect(config_.url, config_.token_a, options);
ASSERT_TRUE(caller_connected) << "Caller failed to connect";
bool receiver_visible = waitForParticipant(caller_room.get(), receiver_identity, 10s);
ASSERT_TRUE(receiver_visible) << "Receiver not visible to caller";
// Test with max payload size (15KB)
std::string max_payload = generateRandomPayload(kMaxRpcPayloadSize);
std::string response =
caller_room->localParticipant()->performRpc(receiver_identity, "payload-size", max_payload, 30.0);
EXPECT_EQ(response, std::to_string(kMaxRpcPayloadSize));
receiver_room->localParticipant()->unregisterRpcMethod("payload-size");
caller_room.reset();
receiver_room.reset();
}
// Test RPC timeout
TEST_F(RpcIntegrationTest, RpcTimeout) {
if (!config_.available) {
GTEST_SKIP() << "LIVEKIT_URL, LIVEKIT_TOKEN_A, and LIVEKIT_TOKEN_B not set";
}
auto receiver_room = std::make_unique<Room>();
RoomOptions options;
options.auto_subscribe = true;
bool receiver_connected = receiver_room->connect(config_.url, config_.token_b, options);
ASSERT_TRUE(receiver_connected) << "Receiver failed to connect";
std::string receiver_identity = receiver_room->localParticipant()->identity();
// Register handler that takes too long
receiver_room->localParticipant()->registerRpcMethod("slow-method",
[](const RpcInvocationData&) -> std::optional<std::string> {
std::this_thread::sleep_for(10s);
return "done";
});
auto caller_room = std::make_unique<Room>();
bool caller_connected = caller_room->connect(config_.url, config_.token_a, options);
ASSERT_TRUE(caller_connected) << "Caller failed to connect";
bool receiver_visible = waitForParticipant(caller_room.get(), receiver_identity, 10s);
ASSERT_TRUE(receiver_visible) << "Receiver not visible to caller";
// Call with short timeout - should fail
EXPECT_THROW({ caller_room->localParticipant()->performRpc(receiver_identity, "slow-method", "", 2.0); }, RpcError);
receiver_room->localParticipant()->unregisterRpcMethod("slow-method");
caller_room.reset();
receiver_room.reset();
}
// Test RPC with unsupported method
TEST_F(RpcIntegrationTest, UnsupportedMethod) {
if (!config_.available) {
GTEST_SKIP() << "LIVEKIT_URL, LIVEKIT_TOKEN_A, and LIVEKIT_TOKEN_B not set";
}
auto receiver_room = std::make_unique<Room>();
RoomOptions options;
options.auto_subscribe = true;
bool receiver_connected = receiver_room->connect(config_.url, config_.token_b, options);
ASSERT_TRUE(receiver_connected) << "Receiver failed to connect";
std::string receiver_identity = receiver_room->localParticipant()->identity();
auto caller_room = std::make_unique<Room>();
bool caller_connected = caller_room->connect(config_.url, config_.token_a, options);
ASSERT_TRUE(caller_connected) << "Caller failed to connect";
bool receiver_visible = waitForParticipant(caller_room.get(), receiver_identity, 10s);
ASSERT_TRUE(receiver_visible) << "Receiver not visible to caller";
// Call unregistered method
try {
caller_room->localParticipant()->performRpc(receiver_identity, "nonexistent-method", "", 5.0);
FAIL() << "Expected RpcError for unsupported method";
} catch (const RpcError& e) {
EXPECT_EQ(static_cast<RpcError::ErrorCode>(e.code()), RpcError::ErrorCode::UNSUPPORTED_METHOD);
}
caller_room.reset();
receiver_room.reset();
}
// Test RPC with application error
TEST_F(RpcIntegrationTest, ApplicationError) {
if (!config_.available) {
GTEST_SKIP() << "LIVEKIT_URL, LIVEKIT_TOKEN_A, and LIVEKIT_TOKEN_B not set";
}
auto receiver_room = std::make_unique<Room>();
RoomOptions options;
options.auto_subscribe = true;
bool receiver_connected = receiver_room->connect(config_.url, config_.token_b, options);
ASSERT_TRUE(receiver_connected) << "Receiver failed to connect";
std::string receiver_identity = receiver_room->localParticipant()->identity();
// Register handler that throws an error
receiver_room->localParticipant()->registerRpcMethod(
"error-method",
[](const RpcInvocationData&) -> std::optional<std::string> { throw std::runtime_error("intentional error"); });
auto caller_room = std::make_unique<Room>();
bool caller_connected = caller_room->connect(config_.url, config_.token_a, options);
ASSERT_TRUE(caller_connected) << "Caller failed to connect";
bool receiver_visible = waitForParticipant(caller_room.get(), receiver_identity, 10s);
ASSERT_TRUE(receiver_visible) << "Receiver not visible to caller";
try {
caller_room->localParticipant()->performRpc(receiver_identity, "error-method", "", 5.0);
FAIL() << "Expected RpcError for application error";
} catch (const RpcError& e) {
EXPECT_EQ(static_cast<RpcError::ErrorCode>(e.code()), RpcError::ErrorCode::APPLICATION_ERROR);
}
receiver_room->localParticipant()->unregisterRpcMethod("error-method");
caller_room.reset();
receiver_room.reset();
}
// Test multiple concurrent RPC calls
TEST_F(RpcIntegrationTest, ConcurrentRpcCalls) {
if (!config_.available) {
GTEST_SKIP() << "LIVEKIT_URL, LIVEKIT_TOKEN_A, and LIVEKIT_TOKEN_B not set";
}
auto receiver_room = std::make_unique<Room>();
RoomOptions options;
options.auto_subscribe = true;
bool receiver_connected = receiver_room->connect(config_.url, config_.token_b, options);
ASSERT_TRUE(receiver_connected) << "Receiver failed to connect";
std::string receiver_identity = receiver_room->localParticipant()->identity();
std::atomic<int> calls_processed{0};
receiver_room->localParticipant()->registerRpcMethod(
"counter", [&calls_processed](const RpcInvocationData& data) -> std::optional<std::string> {
int id = std::stoi(data.payload);
calls_processed++;
std::this_thread::sleep_for(100ms); // Simulate some work
return std::to_string(id * 2);
});
auto caller_room = std::make_unique<Room>();
bool caller_connected = caller_room->connect(config_.url, config_.token_a, options);
ASSERT_TRUE(caller_connected) << "Caller failed to connect";
bool receiver_visible = waitForParticipant(caller_room.get(), receiver_identity, 10s);
ASSERT_TRUE(receiver_visible) << "Receiver not visible to caller";
const int num_concurrent_calls = 10;
std::vector<std::thread> threads;
std::atomic<int> successful_calls{0};
for (int i = 0; i < num_concurrent_calls; ++i) {
threads.emplace_back([&, i]() {
try {
std::string response =
caller_room->localParticipant()->performRpc(receiver_identity, "counter", std::to_string(i), 30.0);
int expected = i * 2;
if (std::stoi(response) == expected) {
successful_calls++;
}
} catch (const std::exception& e) {
std::cerr << "RPC call " << i << " failed: " << e.what() << std::endl;
}
});
}
for (auto& t : threads) {
t.join();
}
EXPECT_EQ(successful_calls.load(), num_concurrent_calls);
EXPECT_EQ(calls_processed.load(), num_concurrent_calls);
receiver_room->localParticipant()->unregisterRpcMethod("counter");
caller_room.reset();
receiver_room.reset();
}
// Integration test: Run for approximately 1 minute
TEST_F(RpcIntegrationTest, OneMinuteIntegration) {
if (!config_.available) {
GTEST_SKIP() << "LIVEKIT_URL, LIVEKIT_TOKEN_A, and LIVEKIT_TOKEN_B not set";
}
auto receiver_room = std::make_unique<Room>();
RoomOptions options;
options.auto_subscribe = true;
bool receiver_connected = receiver_room->connect(config_.url, config_.token_b, options);
ASSERT_TRUE(receiver_connected) << "Receiver failed to connect";
std::string receiver_identity = receiver_room->localParticipant()->identity();
std::atomic<int> total_received{0};
std::atomic<size_t> total_bytes_received{0};
receiver_room->localParticipant()->registerRpcMethod(
"integration-test", [&](const RpcInvocationData& data) -> std::optional<std::string> {
total_received++;
total_bytes_received += data.payload.size();
return "ack:" + std::to_string(data.payload.size());
});
auto caller_room = std::make_unique<Room>();
bool caller_connected = caller_room->connect(config_.url, config_.token_a, options);
ASSERT_TRUE(caller_connected) << "Caller failed to connect";
bool receiver_visible = waitForParticipant(caller_room.get(), receiver_identity, 10s);
ASSERT_TRUE(receiver_visible) << "Receiver not visible to caller";
// Run for 1 minute
const auto test_duration = 60s;
const auto start_time = std::chrono::steady_clock::now();
std::atomic<int> total_sent{0};
std::atomic<int> successful_calls{0};
std::atomic<int> failed_calls{0};
std::atomic<bool> running{true};
// Sender thread
std::thread sender([&]() {
std::vector<size_t> payload_sizes = {100, 1024, 5 * 1024, 10 * 1024, kMaxRpcPayloadSize};
int size_index = 0;
while (running.load()) {
size_t payload_size = payload_sizes[size_index % payload_sizes.size()];
std::string payload = generateRandomPayload(payload_size);
try {
std::string response =
caller_room->localParticipant()->performRpc(receiver_identity, "integration-test", payload, 30.0);
if (response == "ack:" + std::to_string(payload_size)) {
successful_calls++;
}
} catch (const std::exception& e) {
failed_calls++;
}
total_sent++;
size_index++;
std::this_thread::sleep_for(100ms); // Rate limit
}
});
// Wait for test duration
while (std::chrono::steady_clock::now() - start_time < test_duration) {
std::this_thread::sleep_for(1s);
std::cout << "Progress: sent=" << total_sent.load() << " successful=" << successful_calls.load()
<< " failed=" << failed_calls.load() << " received=" << total_received.load() << std::endl;
}
running.store(false);
sender.join();
std::cout << "\n=== Integration Test Results (1 minute) ===" << std::endl;
std::cout << "Total sent: " << total_sent.load() << std::endl;
std::cout << "Successful: " << successful_calls.load() << std::endl;
std::cout << "Failed: " << failed_calls.load() << std::endl;
std::cout << "Total received: " << total_received.load() << std::endl;
std::cout << "Total bytes received: " << total_bytes_received.load() << std::endl;
EXPECT_GT(successful_calls.load(), 0);
EXPECT_EQ(total_sent.load(), total_received.load());
receiver_room->localParticipant()->unregisterRpcMethod("integration-test");
caller_room.reset();
receiver_room.reset();
}
} // namespace livekit::test