From 04055f71126c6008f04ce70c8e7c8b1f9aa72e92 Mon Sep 17 00:00:00 2001 From: "zugz (tox)" Date: Sun, 12 Aug 2018 10:53:11 +0200 Subject: [PATCH] add reconnect test --- CMakeLists.txt | 1 + auto_tests/Makefile.inc | 8 ++- auto_tests/reconnect_test.c | 104 ++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 auto_tests/reconnect_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index b7bbf282ee0..bdd732e1042 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -426,6 +426,7 @@ auto_test(network) auto_test(onion) auto_test(overflow_recvq) auto_test(overflow_sendq) +auto_test(reconnect) auto_test(save_friend) auto_test(save_load) auto_test(send_message) diff --git a/auto_tests/Makefile.inc b/auto_tests/Makefile.inc index 34388c51103..decb8ba97bb 100644 --- a/auto_tests/Makefile.inc +++ b/auto_tests/Makefile.inc @@ -1,13 +1,13 @@ if BUILD_TESTS -TESTS = bootstrap_test conference_double_invite_test conference_peer_nick_test conference_simple_test conference_test \ +TESTS = disc_test bootstrap_test conference_double_invite_test conference_peer_nick_test conference_simple_test conference_test \ conference_two_test crypto_test dht_test encryptsave_test file_saving_test file_transfer_test friend_connection_test \ friend_request_test invalid_tcp_proxy_test invalid_udp_proxy_test lan_discovery_test lossless_packet_test \ lossy_packet_test messenger_test network_test onion_test overflow_recvq_test overflow_sendq_test save_friend_test \ save_load_test send_message_test set_name_test set_status_message_test skeleton_test tcp_relay_test TCP_test \ tox_many_tcp_test tox_many_test tox_one_test tox_strncasecmp_test typing_test version_test -check_PROGRAMS = bootstrap_test conference_double_invite_test conference_peer_nick_test conference_simple_test \ +check_PROGRAMS = disc_test bootstrap_test conference_double_invite_test conference_peer_nick_test conference_simple_test \ conference_test conference_two_test crypto_test dht_test encryptsave_test file_saving_test file_transfer_test \ friend_connection_test friend_request_test invalid_tcp_proxy_test invalid_udp_proxy_test lan_discovery_test \ lossless_packet_test lossy_packet_test messenger_test network_test onion_test overflow_recvq_test \ @@ -57,6 +57,10 @@ conference_test_SOURCES = ../auto_tests/bootstrap_test.c conference_test_CFLAGS = $(AUTO_TEST_CFLAGS) conference_test_LDADD = $(AUTOTEST_LDADD) +disc_test_SOURCES = ../auto_tests/bootstrap_test.c +disc_test_CFLAGS = $(AUTO_TEST_CFLAGS) +disc_test_LDADD = $(AUTOTEST_LDADD) + conference_two_test_SOURCES = ../auto_tests/bootstrap_test.c conference_two_test_CFLAGS = $(AUTO_TEST_CFLAGS) conference_two_test_LDADD = $(AUTOTEST_LDADD) diff --git a/auto_tests/reconnect_test.c b/auto_tests/reconnect_test.c new file mode 100644 index 00000000000..3ae07844ba7 --- /dev/null +++ b/auto_tests/reconnect_test.c @@ -0,0 +1,104 @@ +/* Auto Tests: Conferences. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "../testing/misc_tools.h" +#include "../toxcore/friend_connection.h" +#include "../toxcore/tox.h" +#include "../toxcore/util.h" +#include "check_compat.h" + +#define NUM_TOXES 2 +#define RECONNECT_TIME_MAX (FRIEND_CONNECTION_TIMEOUT + 3) + +typedef struct State { + uint32_t index; +} State; + +#include "run_auto_test.h" + +static uint32_t tox_connected_count(uint32_t tox_count, Tox **toxes, State *state, uint32_t index) +{ + const size_t friend_count = tox_self_get_friend_list_size(toxes[index]); + uint32_t connected_count = 0; + + for (size_t j = 0; j < friend_count; j++) { + if (tox_friend_get_connection_status(toxes[index], j, nullptr) != TOX_CONNECTION_NONE) { + ++connected_count; + } + } + + return connected_count; +} + +static bool all_disconnected_from(uint32_t tox_count, Tox **toxes, State *state, uint32_t index) +{ + for (uint32_t i = 0; i < tox_count; i++) { + if (i == index) { + continue; + } + + if (tox_connected_count(tox_count, toxes, state, i) > tox_count - 1 - 1) { + return false; + } + } + + return true; +} + +static void test_reconnect(Tox **toxes, State *state) +{ + const time_t test_start_time = time(nullptr); + + printf("letting connections settle\n"); + + while ((int)(time(nullptr) - test_start_time) < 2) { + iterate_all(NUM_TOXES, toxes, state); + + c_sleep(ITERATION_INTERVAL); + } + + uint16_t disconnect = random_u16() % NUM_TOXES; + printf("disconnecting #%u\n", state[disconnect].index); + + while (!all_disconnected_from(NUM_TOXES, toxes, state, disconnect)) { + for (uint16_t i = 0; i < NUM_TOXES; ++i) { + if (i != disconnect) { + tox_iterate(toxes[i], &state[i]); + } + } + + c_sleep(ITERATION_INTERVAL); + } + + const time_t reconnect_start_time = time(nullptr); + + printf("reconnecting\n"); + + while (!all_friends_connected(NUM_TOXES, toxes)) { + iterate_all(NUM_TOXES, toxes, state); + + c_sleep(ITERATION_INTERVAL); + } + + const int reconnect_time = (int)(time(nullptr) - reconnect_start_time); + ck_assert_msg(reconnect_time <= RECONNECT_TIME_MAX, "reconnection took %d seconds; expected at most %d seconds", + reconnect_time, RECONNECT_TIME_MAX); + + printf("test_reconnect succeeded, took %d seconds\n", (int)(time(nullptr) - test_start_time)); +} + +int main(void) +{ + setvbuf(stdout, nullptr, _IONBF, 0); + + run_auto_test(NUM_TOXES, test_reconnect); + return 0; +}