Electroneum
clt.cpp File Reference
#include <atomic>
#include <chrono>
#include <functional>
#include <numeric>
#include <boost/thread/thread.hpp>
#include <vector>
#include "gtest/gtest.h"
#include "include_base_utils.h"
#include "misc_language.h"
#include "misc_log_ex.h"
#include "storages/levin_abstract_invoke2.h"
#include "common/util.h"
#include "net_load_tests.h"
Include dependency graph for clt.cpp:

Go to the source code of this file.

Functions

 TEST_F (net_load_test_clt, a_lot_of_client_connections_and_connections_closed_by_client)
 
 TEST_F (net_load_test_clt, a_lot_of_client_connections_and_connections_closed_by_server)
 
 TEST_F (net_load_test_clt, permament_open_and_close_and_connections_closed_by_client)
 
 TEST_F (net_load_test_clt, permament_open_and_close_and_connections_closed_by_server)
 
int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 631 of file clt.cpp.

632 {
633  TRY_ENTRY();
636  //set up logging options
637  mlog_configure(mlog_get_default_log_path("net_load_tests_clt.log"), true);
638 
639  ::testing::InitGoogleTest(&argc, argv);
640  return RUN_ALL_TESTS();
641  CATCH_ENTRY_L0("main", 1);
642 }
bool get_set_enable_assert(bool set=false, bool v=false)
Definition: misc_log_ex.h:138
std::string mlog_get_default_log_path(const char *default_filename)
Definition: mlog.cpp:72
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
Definition: mlog.cpp:148
bool on_startup()
Definition: util.cpp:778
#define TRY_ENTRY()
Definition: misc_log_ex.h:151
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
#define CATCH_ENTRY_L0(lacation, return_val)
Definition: misc_log_ex.h:165
Here is the call graph for this function:

◆ TEST_F() [1/4]

TEST_F ( net_load_test_clt  ,
a_lot_of_client_connections_and_connections_closed_by_client   
)

Definition at line 353 of file clt.cpp.

354 {
355  // Open connections
356  t_connection_opener_1 connection_opener(m_tcp_server, CONNECTION_COUNT);
357  parallel_exec([&] {
358  while (connection_opener.open());
359  });
360 
361  // Wait for all open requests to complete
362  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return CONNECTION_COUNT + RESERVED_CONN_CNT <= m_commands_handler.new_connection_counter() + connection_opener.error_count(); }));
363  LOG_PRINT_L0("number of opened connections / fails (total): " << m_commands_handler.new_connection_counter() <<
364  " / " << connection_opener.error_count() << " (" << (m_commands_handler.new_connection_counter() + connection_opener.error_count()) << ")");
365 
366  // Check
367  ASSERT_GT(m_commands_handler.new_connection_counter(), RESERVED_CONN_CNT);
368  ASSERT_EQ(m_commands_handler.new_connection_counter() + connection_opener.error_count(), CONNECTION_COUNT + RESERVED_CONN_CNT);
369  ASSERT_EQ(m_commands_handler.new_connection_counter() - m_commands_handler.close_connection_counter(), m_tcp_server.get_config_object().get_connections_count());
370 
371  // Close connections
372  parallel_exec([&](size_t thread_idx) {
373  for (size_t i = thread_idx; i < CONNECTION_COUNT; i += m_thread_count)
374  {
375  connection_opener.close(i);
376  }
377  });
378 
379  // Wait for all opened connections to close
380  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT <= m_commands_handler.close_connection_counter(); }));
381  LOG_PRINT_L0("number of opened / closed connections: " << m_tcp_server.get_config_object().get_connections_count() <<
382  " / " << m_commands_handler.close_connection_counter());
383 
384  // Check all connections are closed
385  ASSERT_EQ(m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT, m_commands_handler.close_connection_counter());
386  ASSERT_EQ(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
387 
388  // Wait for server to handle all open and close requests
390  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; });
391  LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
392 
393  // Check server status
394  // It's OK, if server didn't close all opened connections, because of it could receive not all FIN packets
395  ASSERT_LE(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
396  ASSERT_LE(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
397 
398  // Request data from server, it causes to close rest connections
399  ask_for_data_requests();
400 
401  // Wait for server to close rest connections
402  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; });
403  LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
404 
405  // Check server status. All connections should be closed
406  ASSERT_EQ(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
407  ASSERT_EQ(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
408 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
#define ASSERT_LE(val1, val2)
Definition: gtest.h:1964

◆ TEST_F() [2/4]

TEST_F ( net_load_test_clt  ,
a_lot_of_client_connections_and_connections_closed_by_server   
)

Definition at line 410 of file clt.cpp.

411 {
412  // Open connections
413  t_connection_opener_1 connection_opener(m_tcp_server, CONNECTION_COUNT);
414  parallel_exec([&] {
415  while (connection_opener.open());
416  });
417 
418  // Wait for all open requests to complete
419  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return CONNECTION_COUNT + RESERVED_CONN_CNT <= m_commands_handler.new_connection_counter() + connection_opener.error_count(); }));
420  LOG_PRINT_L0("number of opened connections / fails (total): " << m_commands_handler.new_connection_counter() <<
421  " / " << connection_opener.error_count() << " (" << (m_commands_handler.new_connection_counter() + connection_opener.error_count()) << ")");
422 
423  // Check
424  ASSERT_GT(m_commands_handler.new_connection_counter(), RESERVED_CONN_CNT);
425  ASSERT_EQ(m_commands_handler.new_connection_counter() + connection_opener.error_count(), CONNECTION_COUNT + RESERVED_CONN_CNT);
426  ASSERT_EQ(m_commands_handler.new_connection_counter() - m_commands_handler.close_connection_counter(), m_tcp_server.get_config_object().get_connections_count());
427 
428  // Wait for server accepts all connections
430  int last_new_connection_counter = -1;
431  busy_wait_for_server_statistics(srv_stat, [&last_new_connection_counter](const CMD_GET_STATISTICS::response& stat) {
432  if (last_new_connection_counter == static_cast<int>(stat.new_connection_counter)) return true;
433  else { last_new_connection_counter = static_cast<int>(stat.new_connection_counter); return false; }
434  });
435 
436  // Close connections
438  ASSERT_TRUE(epee::net_utils::notify_remote_command2(m_cmd_conn_id, CMD_CLOSE_ALL_CONNECTIONS::ID, req, m_tcp_server.get_config_object()));
439 
440  // Wait for all opened connections to close
441  busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT <= m_commands_handler.close_connection_counter(); });
442  LOG_PRINT_L0("number of opened / closed connections: " << m_tcp_server.get_config_object().get_connections_count() <<
443  " / " << m_commands_handler.close_connection_counter());
444 
445  // It's OK, if server didn't close all connections, because it could accept not all our connections
446  ASSERT_LE(m_commands_handler.close_connection_counter(), m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT);
447  ASSERT_LE(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
448 
449  // Wait for server to handle all open and close requests
450  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; });
451  LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
452 
453  // Check server status
454  ASSERT_EQ(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
455  ASSERT_EQ(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
456 
457  // Close rest connections
458  m_tcp_server.get_config_object().foreach_connection([&](test_connection_context& ctx) {
459  if (ctx.m_connection_id != m_cmd_conn_id)
460  {
461  CMD_DATA_REQUEST::request req;
462  bool r = epee::net_utils::async_invoke_remote_command2<CMD_DATA_REQUEST::response>(ctx.m_connection_id, CMD_DATA_REQUEST::ID, req,
463  m_tcp_server.get_config_object(), [=](int code, const CMD_DATA_REQUEST::response& rsp, const test_connection_context&) {
464  if (code <= 0)
465  {
466  LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST. code = " << code);
467  }
468  });
469  if (!r)
470  LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST");
471  }
472  return true;
473  });
474 
475  // Wait for all opened connections to close
476  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT <= m_commands_handler.close_connection_counter(); }));
477  LOG_PRINT_L0("number of opened / closed connections: " << m_tcp_server.get_config_object().get_connections_count() <<
478  " / " << m_commands_handler.close_connection_counter());
479 
480  // Check
481  ASSERT_EQ(m_commands_handler.close_connection_counter(), m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT);
482  ASSERT_EQ(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
483 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool notify_remote_command2(int command, const t_arg &out_struct, t_transport &transport)
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
#define ASSERT_LE(val1, val2)
Definition: gtest.h:1964
const boost::uuids::uuid m_connection_id
Here is the call graph for this function:

◆ TEST_F() [3/4]

TEST_F ( net_load_test_clt  ,
permament_open_and_close_and_connections_closed_by_client   
)

Definition at line 485 of file clt.cpp.

486 {
487  static const size_t MAX_OPENED_CONN_COUNT = 100;
488 
489  // Open/close connections
490  t_connection_opener_2 connection_opener(m_tcp_server, CONNECTION_COUNT, MAX_OPENED_CONN_COUNT);
491  parallel_exec([&] {
492  while (connection_opener.open_and_close());
493  });
494 
495  // Wait for all open requests to complete
496  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return CONNECTION_COUNT + RESERVED_CONN_CNT <= m_commands_handler.new_connection_counter() + connection_opener.error_count(); }));
497  LOG_PRINT_L0("number of opened connections / fails (total): " << m_commands_handler.new_connection_counter() <<
498  " / " << connection_opener.error_count() << " (" << (m_commands_handler.new_connection_counter() + connection_opener.error_count()) << ")");
499 
500  // Check
501  ASSERT_GT(m_commands_handler.new_connection_counter(), RESERVED_CONN_CNT);
502  ASSERT_EQ(m_commands_handler.new_connection_counter() + connection_opener.error_count(), CONNECTION_COUNT + RESERVED_CONN_CNT);
503 
504  // Wait for all close requests to complete
505  EXPECT_TRUE(busy_wait_for(4 * DEFAULT_OPERATION_TIMEOUT, [&](){ return connection_opener.opened_connection_count() <= MAX_OPENED_CONN_COUNT; }));
506  LOG_PRINT_L0("actual number of opened connections: " << connection_opener.opened_connection_count());
507 
508  // Check
509  ASSERT_EQ(MAX_OPENED_CONN_COUNT, connection_opener.opened_connection_count());
510 
511  connection_opener.close_remaining_connections();
512 
513  // Wait for all close requests to complete
514  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() <= m_commands_handler.close_connection_counter() + RESERVED_CONN_CNT; }));
515  LOG_PRINT_L0("actual number of opened connections: " << connection_opener.opened_connection_count());
516 
517  ASSERT_EQ(m_commands_handler.new_connection_counter(), m_commands_handler.close_connection_counter() + RESERVED_CONN_CNT);
518  ASSERT_EQ(0, connection_opener.opened_connection_count());
519  ASSERT_EQ(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
520 
521  // Wait for server to handle all open and close requests
523  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; });
524  LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
525 
526  // Check server status
527  // It's OK, if server didn't close all opened connections, because of it could receive not all FIN packets
528  ASSERT_LE(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
529  ASSERT_LE(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
530 
531  // Request data from server, it causes to close rest connections
532  ask_for_data_requests();
533 
534  // Wait for server to close rest connections
535  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; });
536  LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
537 
538  // Check server status. All connections should be closed
539  ASSERT_EQ(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
540  ASSERT_EQ(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
541 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
#define ASSERT_LE(val1, val2)
Definition: gtest.h:1964

◆ TEST_F() [4/4]

TEST_F ( net_load_test_clt  ,
permament_open_and_close_and_connections_closed_by_server   
)

Definition at line 543 of file clt.cpp.

544 {
545  static const size_t MAX_OPENED_CONN_COUNT = 100;
546 
547  // Init test
548  std::atomic<int> test_state(0);
550  req_start.open_request_target = CONNECTION_COUNT;
551  req_start.max_opened_conn_count = MAX_OPENED_CONN_COUNT;
552  ASSERT_TRUE(epee::net_utils::async_invoke_remote_command2<CMD_START_OPEN_CLOSE_TEST::response>(m_cmd_conn_id, CMD_START_OPEN_CLOSE_TEST::ID, req_start,
553  m_tcp_server.get_config_object(), [&](int code, const CMD_START_OPEN_CLOSE_TEST::response&, const test_connection_context&) {
554  test_state.store(0 < code ? 1 : -1, std::memory_order_seq_cst);
555  }));
556 
557  // Wait for server response
558  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&]{ return 1 == test_state.load(std::memory_order_seq_cst); }));
559  ASSERT_EQ(1, test_state.load(std::memory_order_seq_cst));
560 
561  // Open connections
562  t_connection_opener_1 connection_opener(m_tcp_server, CONNECTION_COUNT);
563  parallel_exec([&] {
564  while (connection_opener.open());
565  });
566 
567  // Wait for all open requests to complete
568  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return CONNECTION_COUNT + RESERVED_CONN_CNT <= m_commands_handler.new_connection_counter() + connection_opener.error_count(); }));
569  LOG_PRINT_L0("number of opened connections / fails (total): " << m_commands_handler.new_connection_counter() <<
570  " / " << connection_opener.error_count() << " (" << (m_commands_handler.new_connection_counter() + connection_opener.error_count()) << ")");
571  LOG_PRINT_L0("actual number of opened connections: " << m_tcp_server.get_config_object().get_connections_count());
572 
573  ASSERT_GT(m_commands_handler.new_connection_counter(), RESERVED_CONN_CNT);
574  ASSERT_EQ(m_commands_handler.new_connection_counter() + connection_opener.error_count(), CONNECTION_COUNT + RESERVED_CONN_CNT);
575 
576  // Wait for server accepts all connections
578  int last_new_connection_counter = -1;
579  busy_wait_for_server_statistics(srv_stat, [&last_new_connection_counter](const CMD_GET_STATISTICS::response& stat) {
580  if (last_new_connection_counter == static_cast<int>(stat.new_connection_counter)) return true;
581  else { last_new_connection_counter = static_cast<int>(stat.new_connection_counter); return false; }
582  });
583 
584  // Ask server to close rest connections
586  ASSERT_TRUE(epee::net_utils::notify_remote_command2(m_cmd_conn_id, CMD_CLOSE_ALL_CONNECTIONS::ID, req, m_tcp_server.get_config_object()));
587 
588  // Wait for almost all connections to be closed by server
589  busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() <= m_commands_handler.close_connection_counter() + RESERVED_CONN_CNT; });
590 
591  // It's OK, if there are opened connections, because server could accept not all our connections
592  ASSERT_LE(m_commands_handler.close_connection_counter() + RESERVED_CONN_CNT, m_commands_handler.new_connection_counter());
593  ASSERT_LE(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
594 
595  // Wait for server to handle all open and close requests
596  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; });
597  LOG_PRINT_L0("server statistics: " << srv_stat.to_string());
598 
599  // Check server status
600  ASSERT_EQ(srv_stat.close_connection_counter, srv_stat.new_connection_counter - RESERVED_CONN_CNT);
601  ASSERT_EQ(RESERVED_CONN_CNT, srv_stat.opened_connections_count);
602 
603  // Close rest connections
604  m_tcp_server.get_config_object().foreach_connection([&](test_connection_context& ctx) {
605  if (ctx.m_connection_id != m_cmd_conn_id)
606  {
607  CMD_DATA_REQUEST::request req;
608  bool r = epee::net_utils::async_invoke_remote_command2<CMD_DATA_REQUEST::response>(ctx.m_connection_id, CMD_DATA_REQUEST::ID, req,
609  m_tcp_server.get_config_object(), [=](int code, const CMD_DATA_REQUEST::response& rsp, const test_connection_context&) {
610  if (code <= 0)
611  {
612  LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST. code = " << code);
613  }
614  });
615  if (!r)
616  LOG_PRINT_L0("Failed to invoke CMD_DATA_REQUEST");
617  }
618  return true;
619  });
620 
621  // Wait for all opened connections to close
622  EXPECT_TRUE(busy_wait_for(DEFAULT_OPERATION_TIMEOUT, [&](){ return m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT <= m_commands_handler.close_connection_counter(); }));
623  LOG_PRINT_L0("number of opened / closed connections: " << m_tcp_server.get_config_object().get_connections_count() <<
624  " / " << m_commands_handler.close_connection_counter());
625 
626  // Check
627  ASSERT_EQ(m_commands_handler.close_connection_counter(), m_commands_handler.new_connection_counter() - RESERVED_CONN_CNT);
628  ASSERT_EQ(RESERVED_CONN_CNT, m_tcp_server.get_config_object().get_connections_count());
629 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool notify_remote_command2(int command, const t_arg &out_struct, t_transport &transport)
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_GT(val1, val2)
Definition: gtest.h:1976
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
#define ASSERT_LE(val1, val2)
Definition: gtest.h:1964
const boost::uuids::uuid m_connection_id
Here is the call graph for this function: