Debian Codegraph @

tests/net_load_tests/clt.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <atomic>
32 #include <chrono>
33 #include <functional>
34 #include <numeric>
35 #include <boost/thread/thread.hpp>
36 #include <vector>
37
38 #include "gtest/gtest.h"
39
40 #include "include_base_utils.h"
41 #include "misc_language.h"
42 #include "misc_log_ex.h"
43 #include "storages/levin_abstract_invoke2.h"
44 #include "common/util.h"
45
46 #include "net_load_tests.h"
47
48 using namespace net_load_tests;
49
50 namespace
51 {
52 const size_t CONNECTION_COUNT = 100000;
53 const size_t CONNECTION_TIMEOUT = 10000;
54 const size_t DEFAULT_OPERATION_TIMEOUT = 30000;
55 const size_t RESERVED_CONN_CNT = 1;
56
57 template<typename t_predicate>
58 bool busy_wait_for(size_t timeout_ms, const t_predicate& predicate, size_t sleep_ms = 10)
59 {
60 for (size_t i = 0; i < timeout_ms / sleep_ms; ++i)
61 {
62 if (predicate())
63 return true;
64 //std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
65 epee::misc_utils::sleep_no_w(static_cast<long>(sleep_ms));
66 }
67 return false;
68 }
69
70 class t_connection_opener_1
71 {
72 public:
73 t_connection_opener_1(test_tcp_server& tcp_server, size_t open_request_target)
74 : m_tcp_server(tcp_server)
75 , m_open_request_target(open_request_target)
76 , m_next_id(0)
77 , m_error_count(0)
78 , m_connections(open_request_target)
79 {
80 for (auto& conn_id : m_connections)
81 conn_id = boost::uuids::nil_uuid();
82 }
83
84 bool open()
85 {
86 size_t id = m_next_id.fetch_add(1, std::memory_order_relaxed);
87 if (m_open_request_target <= id)
88 return false;
89
90 bool r = m_tcp_server.connect_async("127.0.0.1", srv_port, CONNECTION_TIMEOUT, [=](const test_connection_context& context, const boost::system::error_code& ec) {
91 if (!ec)
92 {
93 m_connections[id] = context.m_connection_id;
94 }
95 else
96 {
97 m_error_count.fetch_add(1, std::memory_order_relaxed);
98 }
99 });
100
101 if (!r)
102 {
103 m_error_count.fetch_add(1, std::memory_order_relaxed);
104 }
105
106 return true;
107 }
108
109 bool close(size_t id)
110 {
111 if (!m_connections[id].is_nil())
112 {
113 m_tcp_server.get_config_object().close(m_connections[id], true);
114 return true;
115 }
116 else
117 {
118 return false;
119 }
120 }
121
122 size_t error_count() const { return m_error_count.load(std::memory_order_relaxed); }
123
124 private:
125 test_tcp_server& m_tcp_server;
126 size_t m_open_request_target;
127 std::atomic<size_t> m_next_id;
128 std::atomic<size_t> m_error_count;
129 std::vector<boost::uuids::uuid> m_connections;
130 };
131
132 class t_connection_opener_2
133 {
134 public:
135 t_connection_opener_2(test_tcp_server& tcp_server, size_t open_request_target, size_t max_opened_connection_count)
136 : m_tcp_server(tcp_server)
137 , m_open_request_target(open_request_target)
138 , m_open_request_count(0)
139 , m_error_count(0)
140 , m_open_close_test_helper(tcp_server, open_request_target, max_opened_connection_count)
141 {
142 }
143
144 bool open_and_close()
145 {
146 size_t req_count = m_open_request_count.fetch_add(1, std::memory_order_relaxed);
147 if (m_open_request_target <= req_count)
148 return false;
149
150 bool r = m_tcp_server.connect_async("127.0.0.1", srv_port, CONNECTION_TIMEOUT, [=](const test_connection_context& context, const boost::system::error_code& ec) {
151 if (!ec)
152 {
153 m_open_close_test_helper.handle_new_connection(context.m_connection_id);
154 }
155 else
156 {
157 m_error_count.fetch_add(1, std::memory_order_relaxed);
158 }
159 });
160
161 if (!r)
162 {
163 m_error_count.fetch_add(1, std::memory_order_relaxed);
164 }
165
166 return true;
167 }
168
169 void close_remaining_connections()
170 {
171 m_open_close_test_helper.close_remaining_connections();
172 }
173
174 size_t opened_connection_count() const { return m_open_close_test_helper.opened_connection_count(); }
175 size_t error_count() const { return m_error_count.load(std::memory_order_relaxed); }
176
177 private:
178 test_tcp_server& m_tcp_server;
179 size_t m_open_request_target;
180 std::atomic<size_t> m_open_request_count;
181 std::atomic<size_t> m_error_count;
182 open_close_test_helper m_open_close_test_helper;
183 };
184
185 class net_load_test_clt : public ::testing::Test
186 {
187 public:
188 net_load_test_clt()
189 : m_tcp_server(epee::net_utils::e_connection_type_RPC) // RPC disables network limit for unit tests
190 {
191 }
192 protected:
193 virtual void SetUp()
194 {
195 m_thread_count = (std::max)(min_thread_count, boost::thread::hardware_concurrency() / 2);
196
197 m_tcp_server.get_config_object().set_handler(&m_commands_handler);
198 m_tcp_server.get_config_object().m_invoke_timeout = CONNECTION_TIMEOUT;
199
200 ASSERT_TRUE(m_tcp_server.init_server(clt_port, "127.0.0.1"));
201 ASSERT_TRUE(m_tcp_server.run_server(m_thread_count, false));
202
203 // Connect to server
204 std::atomic<int> conn_status(0);
205 m_context = {};
206 ASSERT_TRUE(m_tcp_server.connect_async("127.0.0.1", srv_port, CONNECTION_TIMEOUT, [&](const test_connection_context& context, const boost::system::error_code& ec) {
207 if (!ec)
208 {
209 m_context = context;
210 }
211 else
212 {
213 LOG_ERROR("Connection error: " << ec.message());
214 }
215 conn_status.store(1, std::memory_order_seq_cst);
216 }));
217
218 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return 0 != conn_status.load(std::memory_order_seq_cst); })) << "connect_async timed out";
219 ASSERT_EQ(1, conn_status.load(std::memory_order_seq_cst));
220 ASSERT_FALSE(m_context.m_connection_id.is_nil());
221
222 conn_status.store(0, std::memory_order_seq_cst);
223 CMD_RESET_STATISTICS::request req;
224 ASSERT_TRUE(epee::net_utils::async_invoke_remote_command2<CMD_RESET_STATISTICS::response>(m_context, CMD_RESET_STATISTICS::ID, req,
225 m_tcp_server.get_config_object(), [&](int code, const CMD_RESET_STATISTICS::response& rsp, const test_connection_context&) {
226 conn_status.store(code, std::memory_order_seq_cst);
227 }));
228
229 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return 0 != conn_status.load(std::memory_order_seq_cst); })) << "reset statistics timed out";
230 ASSERT_LT(0, conn_status.load(std::memory_order_seq_cst));
231 }
232
233 virtual void TearDown()
234 {
235 m_tcp_server.send_stop_signal();
236 ASSERT_TRUE(m_tcp_server.timed_wait_server_stop(DEFAULT_OPERATION_TIMEOUT));
237 }
238
239 static void TearDownTestCase()
240 {
241 // Stop server
242 test_levin_commands_handler *commands_handler_ptr = new test_levin_commands_handler();
243 test_levin_commands_handler &commands_handler = *commands_handler_ptr;
244 test_tcp_server tcp_server(epee::net_utils::e_connection_type_RPC);
245 tcp_server.get_config_object().set_handler(commands_handler_ptr, [](epee::levin::levin_commands_handler<test_connection_context> *handler)->void { delete handler; });
246 tcp_server.get_config_object().m_invoke_timeout = CONNECTION_TIMEOUT;
247
248 if (!tcp_server.init_server(clt_port, "127.0.0.1")) return;
249 if (!tcp_server.run_server(2, false)) return;
250
251 // Connect to server and invoke shutdown command
252 std::atomic<int> conn_status(0);
253 test_connection_context cmd_context;
254 tcp_server.connect_async("127.0.0.1", srv_port, CONNECTION_TIMEOUT, [&](const test_connection_context& context, const boost::system::error_code& ec) {
255 cmd_context = context;
256 conn_status.store(!ec ? 1 : -1, std::memory_order_seq_cst);
257 });
258
259 if (!busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return 0 != conn_status.load(std::memory_order_seq_cst); })) return;
260 if (1 != conn_status.load(std::memory_order_seq_cst)) return;
261
262 epee::net_utils::notify_remote_command2(cmd_context, CMD_SHUTDOWN::ID, CMD_SHUTDOWN::request(), tcp_server.get_config_object());
263
264 busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return 0 != commands_handler.close_connection_counter(); });
265 }
266
267 template<typename Func>
268 static auto call_func(size_t /*thread_index*/, const Func& func, int) -> decltype(func())
269 {
270 func();
271 }
272
273 template<typename Func>
274 static auto call_func(size_t thread_index, const Func& func, long) -> decltype(func(thread_index))
275 {
276 func(thread_index);
277 }
278
279 template<typename Func>
280 void parallel_exec(const Func& func)
281 {
282 unit_test::call_counter properly_finished_threads;
283 std::vector<boost::thread> threads(m_thread_count);
284 for (size_t i = 0; i < threads.size(); ++i)
285 {
286 threads[i] = boost::thread([&, i] {
287 call_func(i, func, 0);
288 properly_finished_threads.inc();
289 });
290 }
291
292 for (auto& th : threads)
293 th.join();
294
295 ASSERT_EQ(properly_finished_threads.get(), m_thread_count);
296 }
297
298 void get_server_statistics(CMD_GET_STATISTICS::response& statistics)
299 {
300 std::atomic<int> req_status(0);
301 CMD_GET_STATISTICS::request req;
302 ASSERT_TRUE(epee::net_utils::async_invoke_remote_command2<CMD_GET_STATISTICS::response>(m_context, CMD_GET_STATISTICS::ID, req,
303 m_tcp_server.get_config_object(), [&](int code, const CMD_GET_STATISTICS::response& rsp, const test_connection_context&) {
304 if (0 < code)
305 {
306 statistics = rsp;
307 }
308 else
309 {
310 LOG_ERROR("Get server statistics error: " << code);
311 }
312 req_status.store(0 < code ? 1 : -1, std::memory_order_seq_cst);
313 }));
314
315 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return 0 != req_status.load(std::memory_order_seq_cst); })) << "get_server_statistics timed out";
316 ASSERT_EQ(1, req_status.load(std::memory_order_seq_cst));
317 }
318
319 template <typename t_predicate>
320 bool busy_wait_for_server_statistics(CMD_GET_STATISTICS::response& statistics, const t_predicate& predicate)
321 {
322 for (size_t i = 0; i < 30; ++i)
323 {
324 get_server_statistics(statistics);
325 if (predicate(statistics))
326 {
327 return true;
328 }
329
330 //std::this_thread::sleep_for(std::chrono::seconds(1));
331 epee::misc_utils::sleep_no_w(1000);
332 }
333
334 return false;
335 }
336
337 void ask_for_data_requests(size_t request_size = 0)
338 {
339 CMD_SEND_DATA_REQUESTS::request req;
340 req.request_size = request_size;
341 epee::net_utils::notify_remote_command2(m_context, CMD_SEND_DATA_REQUESTS::ID, req, m_tcp_server.get_config_object());
342 }
343
344 protected:
345 test_tcp_server m_tcp_server;
346 test_levin_commands_handler m_commands_handler;
347 size_t m_thread_count;
348 test_connection_context m_context;
349 };
350 }
351
352 TEST_F(net_load_test_clt, a_lot_of_client_connections_and_connections_closed_by_client)
353 {
354 // Open connections
355 t_connection_opener_1 connection_opener(m_tcp_server, CONNECTION_COUNT);
356 parallel_exec([&] {
357 while (connection_opener.open());
358 });
359
360 // Wait for all open requests to complete
361 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return CONNECTION_COUNT + RESERVED_CONN_CNT <= m_commands_handler.new_connection_counter() + connection_opener.error_count(); }));
362 LOG_PRINT_L0("number of opened connections / fails (total): " << m_commands_handler.new_connection_counter() <<
363 " / " << connection_opener.error_count() << " (" << (m_commands_handler.new_connection_counter() + connection_opener.error_count()) << ")");
364
365 // Check
366 ASSERT_GT(m_commands_handler.new_connection_counter(), RESERVED_CONN_CNT);
367 ASSERT_EQ(m_commands_handler.new_connection_counter() + connection_opener.error_count(), CONNECTION_COUNT + RESERVED_CONN_CNT);
368 ASSERT_EQ(m_commands_handler.new_connection_counter() - m_commands_handler.close_connection_counter(), m_tcp_server.get_config_object().get_connections_count());
369
370 // Close connections
371 parallel_exec([&](size_t thread_idx) {
372 for (size_t i = thread_idx; i < CONNECTION_COUNT; i += m_thread_count)
373 {
374 connection_opener.close(i);
375 }
376 });
377
378 // Wait for all opened connections to close
379 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT <= m_commands_handler.close_connection_counter(); }));
380 LOG_PRINT_L0("number of opened / closed connections: " << m_tcp_server.get_config_object().get_connections_count() <<
381 " / " << m_commands_handler.close_connection_counter());
382
383 // Check all connections are closed
384 ASSERT_EQ(m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT, m_commands_handler.close_connection_counter());
385 ASSERT_EQ(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
386
387 // Wait for server to handle all open and close requests
388 CMD_GET_STATISTICS::response srv_stat;
389 busy_wait_for_server_statistics(srv_stat, [](const CMD_GET_STATISTICS::response& stat) { return stat.new_connection_counter - RESERVED_CONN_CNT <= stat.close_connection_counter; });
390 LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
391
392 // Check server status
393 // It's OK, if server didn't close all opened connections, because of it could receive not all FIN packets
394 ASSERT_LE(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
395 ASSERT_LE(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
396
397 // Request data from server, it causes to close rest connections
398 ask_for_data_requests();
399
400 // Wait for server to close rest connections
401 busy_wait_for_server_statistics(srv_stat, [](const CMD_GET_STATISTICS::response& stat) { return stat.new_connection_counter - RESERVED_CONN_CNT <= stat.close_connection_counter; });
402 LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
403
404 // Check server status. All connections should be closed
405 ASSERT_EQ(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
406 ASSERT_EQ(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
407 }
408
409 TEST_F(net_load_test_clt, a_lot_of_client_connections_and_connections_closed_by_server)
410 {
411 // Open connections
412 t_connection_opener_1 connection_opener(m_tcp_server, CONNECTION_COUNT);
413 parallel_exec([&] {
414 while (connection_opener.open());
415 });
416
417 // Wait for all open requests to complete
418 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return CONNECTION_COUNT + RESERVED_CONN_CNT <= m_commands_handler.new_connection_counter() + connection_opener.error_count(); }));
419 LOG_PRINT_L0("number of opened connections / fails (total): " << m_commands_handler.new_connection_counter() <<
420 " / " << connection_opener.error_count() << " (" << (m_commands_handler.new_connection_counter() + connection_opener.error_count()) << ")");
421
422 // Check
423 ASSERT_GT(m_commands_handler.new_connection_counter(), RESERVED_CONN_CNT);
424 ASSERT_EQ(m_commands_handler.new_connection_counter() + connection_opener.error_count(), CONNECTION_COUNT + RESERVED_CONN_CNT);
425 ASSERT_EQ(m_commands_handler.new_connection_counter() - m_commands_handler.close_connection_counter(), m_tcp_server.get_config_object().get_connections_count());
426
427 // Wait for server accepts all connections
428 CMD_GET_STATISTICS::response srv_stat;
429 int last_new_connection_counter = -1;
430 busy_wait_for_server_statistics(srv_stat, [&last_new_connection_counter](const CMD_GET_STATISTICS::response& stat) {
431 if (last_new_connection_counter == static_cast<int>(stat.new_connection_counter)) return true;
432 else { last_new_connection_counter = static_cast<int>(stat.new_connection_counter); return false; }
433 });
434
435 // Close connections
436 CMD_CLOSE_ALL_CONNECTIONS::request req;
437 ASSERT_TRUE(epee::net_utils::notify_remote_command2(m_context, CMD_CLOSE_ALL_CONNECTIONS::ID, req, m_tcp_server.get_config_object()));
438
439 // Wait for all opened connections to close
440 busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT <= m_commands_handler.close_connection_counter(); });
441 LOG_PRINT_L0("number of opened / closed connections: " << m_tcp_server.get_config_object().get_connections_count() <<
442 " / " << m_commands_handler.close_connection_counter());
443
444 // It's OK, if server didn't close all connections, because it could accept not all our connections
445 ASSERT_LE(m_commands_handler.close_connection_counter(), m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT);
446 ASSERT_LE(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
447
448 // Wait for server to handle all open and close requests
449 busy_wait_for_server_statistics(srv_stat, [](const CMD_GET_STATISTICS::response& stat) { return stat.new_connection_counter - RESERVED_CONN_CNT <= stat.close_connection_counter; });
450 LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
451
452 // Check server status
453 ASSERT_EQ(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
454 ASSERT_EQ(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
455
456 // Close rest connections
457 m_tcp_server.get_config_object().foreach_connection([&](test_connection_context& ctx) {
458 if (ctx.m_connection_id != m_context.m_connection_id)
459 {
460 CMD_DATA_REQUEST::request req;
461 bool r = epee::net_utils::async_invoke_remote_command2<CMD_DATA_REQUEST::response>(ctx, CMD_DATA_REQUEST::ID, req,
462 m_tcp_server.get_config_object(), [=](int code, const CMD_DATA_REQUEST::response& rsp, const test_connection_context&) {
463 if (code <= 0)
464 {
465 LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST. code = " << code);
466 }
467 });
468 if (!r)
469 LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST");
470 }
471 return true;
472 });
473
474 // Wait for all opened connections to close
475 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT <= m_commands_handler.close_connection_counter(); }));
476 LOG_PRINT_L0("number of opened / closed connections: " << m_tcp_server.get_config_object().get_connections_count() <<
477 " / " << m_commands_handler.close_connection_counter());
478
479 // Check
480 ASSERT_EQ(m_commands_handler.close_connection_counter(), m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT);
481 ASSERT_EQ(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
482 }
483
484 TEST_F(net_load_test_clt, permament_open_and_close_and_connections_closed_by_client)
485 {
486 static const size_t MAX_OPENED_CONN_COUNT = 100;
487
488 // Open/close connections
489 t_connection_opener_2 connection_opener(m_tcp_server, CONNECTION_COUNT, MAX_OPENED_CONN_COUNT);
490 parallel_exec([&] {
491 while (connection_opener.open_and_close());
492 });
493
494 // Wait for all open requests to complete
495 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return CONNECTION_COUNT + RESERVED_CONN_CNT <= m_commands_handler.new_connection_counter() + connection_opener.error_count(); }));
496 LOG_PRINT_L0("number of opened connections / fails (total): " << m_commands_handler.new_connection_counter() <<
497 " / " << connection_opener.error_count() << " (" << (m_commands_handler.new_connection_counter() + connection_opener.error_count()) << ")");
498
499 // Check
500 ASSERT_GT(m_commands_handler.new_connection_counter(), RESERVED_CONN_CNT);
501 ASSERT_EQ(m_commands_handler.new_connection_counter() + connection_opener.error_count(), CONNECTION_COUNT + RESERVED_CONN_CNT);
502
503 // Wait for all close requests to complete
504 EXPECT_TRUE(busy_wait_for(4 * DEFAULT_OPERATION_TIMEOUT, [&](){ return connection_opener.opened_connection_count() <= MAX_OPENED_CONN_COUNT; }));
505 LOG_PRINT_L0("actual number of opened connections: " << connection_opener.opened_connection_count());
506
507 // Check
508 ASSERT_EQ(MAX_OPENED_CONN_COUNT, connection_opener.opened_connection_count());
509
510 connection_opener.close_remaining_connections();
511
512 // Wait for all close requests to complete
513 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() <= m_commands_handler.close_connection_counter() + RESERVED_CONN_CNT; }));
514 LOG_PRINT_L0("actual number of opened connections: " << connection_opener.opened_connection_count());
515
516 ASSERT_EQ(m_commands_handler.new_connection_counter(), m_commands_handler.close_connection_counter() + RESERVED_CONN_CNT);
517 ASSERT_EQ(0, connection_opener.opened_connection_count());
518 ASSERT_EQ(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
519
520 // Wait for server to handle all open and close requests
521 CMD_GET_STATISTICS::response srv_stat;
522 busy_wait_for_server_statistics(srv_stat, [](const CMD_GET_STATISTICS::response& stat) { return stat.new_connection_counter - RESERVED_CONN_CNT <= stat.close_connection_counter; });
523 LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
524
525 // Check server status
526 // It's OK, if server didn't close all opened connections, because of it could receive not all FIN packets
527 ASSERT_LE(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
528 ASSERT_LE(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
529
530 // Request data from server, it causes to close rest connections
531 ask_for_data_requests();
532
533 // Wait for server to close rest connections
534 busy_wait_for_server_statistics(srv_stat, [](const CMD_GET_STATISTICS::response& stat) { return stat.new_connection_counter - RESERVED_CONN_CNT <= stat.close_connection_counter; });
535 LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
536
537 // Check server status. All connections should be closed
538 ASSERT_EQ(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
539 ASSERT_EQ(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
540 }
541
542 TEST_F(net_load_test_clt, permament_open_and_close_and_connections_closed_by_server)
543 {
544 static const size_t MAX_OPENED_CONN_COUNT = 100;
545
546 // Init test
547 std::atomic<int> test_state(0);
548 CMD_START_OPEN_CLOSE_TEST::request req_start;
549 req_start.open_request_target = CONNECTION_COUNT;
550 req_start.max_opened_conn_count = MAX_OPENED_CONN_COUNT;
551 ASSERT_TRUE(epee::net_utils::async_invoke_remote_command2<CMD_START_OPEN_CLOSE_TEST::response>(m_context, CMD_START_OPEN_CLOSE_TEST::ID, req_start,
552 m_tcp_server.get_config_object(), [&](int code, const CMD_START_OPEN_CLOSE_TEST::response&, const test_connection_context&) {
553 test_state.store(0 < code ? 1 : -1, std::memory_order_seq_cst);
554 }));
555
556 // Wait for server response
557 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return 1 == test_state.load(std::memory_order_seq_cst); }));
558 ASSERT_EQ(1, test_state.load(std::memory_order_seq_cst));
559
560 // Open connections
561 t_connection_opener_1 connection_opener(m_tcp_server, CONNECTION_COUNT);
562 parallel_exec([&] {
563 while (connection_opener.open());
564 });
565
566 // Wait for all open requests to complete
567 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return CONNECTION_COUNT + RESERVED_CONN_CNT <= m_commands_handler.new_connection_counter() + connection_opener.error_count(); }));
568 LOG_PRINT_L0("number of opened connections / fails (total): " << m_commands_handler.new_connection_counter() <<
569 " / " << connection_opener.error_count() << " (" << (m_commands_handler.new_connection_counter() + connection_opener.error_count()) << ")");
570 LOG_PRINT_L0("actual number of opened connections: " << m_tcp_server.get_config_object().get_connections_count());
571
572 ASSERT_GT(m_commands_handler.new_connection_counter(), RESERVED_CONN_CNT);
573 ASSERT_EQ(m_commands_handler.new_connection_counter() + connection_opener.error_count(), CONNECTION_COUNT + RESERVED_CONN_CNT);
574
575 // Wait for server accepts all connections
576 CMD_GET_STATISTICS::response srv_stat;
577 int last_new_connection_counter = -1;
578 busy_wait_for_server_statistics(srv_stat, [&last_new_connection_counter](const CMD_GET_STATISTICS::response& stat) {
579 if (last_new_connection_counter == static_cast<int>(stat.new_connection_counter)) return true;
580 else { last_new_connection_counter = static_cast<int>(stat.new_connection_counter); return false; }
581 });
582
583 // Ask server to close rest connections
584 CMD_CLOSE_ALL_CONNECTIONS::request req;
585 ASSERT_TRUE(epee::net_utils::notify_remote_command2(m_context, CMD_CLOSE_ALL_CONNECTIONS::ID, req, m_tcp_server.get_config_object()));
586
587 // Wait for almost all connections to be closed by server
588 busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() <= m_commands_handler.close_connection_counter() + RESERVED_CONN_CNT; });
589
590 // It's OK, if there are opened connections, because server could accept not all our connections
591 ASSERT_LE(m_commands_handler.close_connection_counter() + RESERVED_CONN_CNT, m_commands_handler.new_connection_counter());
592 ASSERT_LE(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
593
594 // Wait for server to handle all open and close requests
595 busy_wait_for_server_statistics(srv_stat, [](const CMD_GET_STATISTICS::response& stat) { return stat.new_connection_counter - RESERVED_CONN_CNT <= stat.close_connection_counter; });
596 LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
597
598 // Check server status
599 ASSERT_EQ(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
600 ASSERT_EQ(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
601
602 // Close rest connections
603 m_tcp_server.get_config_object().foreach_connection([&](test_connection_context& ctx) {
604 if (ctx.m_connection_id != m_context.m_connection_id)
605 {
606 CMD_DATA_REQUEST::request req;
607 bool r = epee::net_utils::async_invoke_remote_command2<CMD_DATA_REQUEST::response>(ctx, CMD_DATA_REQUEST::ID, req,
608 m_tcp_server.get_config_object(), [=](int code, const CMD_DATA_REQUEST::response& rsp, const test_connection_context&) {
609 if (code <= 0)
610 {
611 LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST. code = " << code);
612 }
613 });
614 if (!r)
615 LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST");
616 }
617 return true;
618 });
619
620 // Wait for all opened connections to close
621 EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT <= m_commands_handler.close_connection_counter(); }));
622 LOG_PRINT_L0("number of opened / closed connections: " << m_tcp_server.get_config_object().get_connections_count() <<
623 " / " << m_commands_handler.close_connection_counter());
624
625 // Check
626 ASSERT_EQ(m_commands_handler.close_connection_counter(), m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT);
627 ASSERT_EQ(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
628 }
629
630 int main(int argc, char** argv)
631 {
632 TRY_ENTRY();
633 tools::on_startup();
634 epee::debug::get_set_enable_assert(true, false);
635 //set up logging options
636 mlog_configure(mlog_get_default_log_path("net_load_tests_clt.log"), true);
637
638 ::testing::InitGoogleTest(&argc, argv);
639 return RUN_ALL_TESTS();
640 CATCH_ENTRY_L0("main", 1);
641 }