From 9764285ab156c25050b9e9e46a8e8bc0be701951 Mon Sep 17 00:00:00 2001 From: "zugz (tox)" Date: Fri, 17 Aug 2018 20:34:25 +0200 Subject: [PATCH] Use test clock in run_auto_test tests and dht test --- auto_tests/conference_double_invite_test.c | 10 ++++---- auto_tests/conference_peer_nick_test.c | 11 ++++----- auto_tests/dht_test.c | 11 ++++++++- auto_tests/friend_connection_test.c | 1 + auto_tests/lossless_packet_test.c | 7 +++--- auto_tests/lossy_packet_test.c | 6 ++--- auto_tests/overflow_recvq_test.c | 5 ++-- auto_tests/overflow_sendq_test.c | 1 + auto_tests/reconnect_test.c | 20 ++++++++-------- auto_tests/run_auto_test.h | 27 +++++++++++++++------- auto_tests/send_message_test.c | 7 +++--- 11 files changed, 60 insertions(+), 46 deletions(-) diff --git a/auto_tests/conference_double_invite_test.c b/auto_tests/conference_double_invite_test.c index 33026986..36e93bc0 100644 --- a/auto_tests/conference_double_invite_test.c +++ b/auto_tests/conference_double_invite_test.c @@ -7,6 +7,8 @@ typedef struct State { uint32_t index; + uint64_t clock; + bool self_online; bool friend_online; @@ -64,10 +66,7 @@ static void conference_double_invite_test(Tox **toxes, State *state) fprintf(stderr, "Waiting for invitation to arrive\n"); do { - tox_iterate(toxes[0], &state[0]); - tox_iterate(toxes[1], &state[1]); - - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(2, toxes, state, ITERATION_INTERVAL); } while (!state[0].joined || !state[1].joined); fprintf(stderr, "Invitations accepted\n"); @@ -75,8 +74,7 @@ static void conference_double_invite_test(Tox **toxes, State *state) // Invite one more time, resulting in friend -1 inviting tox1 (toxes[1]). tox_conference_invite(toxes[0], 0, state[0].conference, nullptr); - tox_iterate(toxes[0], &state[0]); - tox_iterate(toxes[1], &state[1]); + iterate_all_wait(2, toxes, state, ITERATION_INTERVAL); } int main(void) diff --git a/auto_tests/conference_peer_nick_test.c b/auto_tests/conference_peer_nick_test.c index 14676b76..eb9bee71 100644 --- a/auto_tests/conference_peer_nick_test.c +++ b/auto_tests/conference_peer_nick_test.c @@ -7,6 +7,8 @@ typedef struct State { uint32_t index; + uint64_t clock; + bool self_online; bool friend_online; bool friend_in_group; @@ -108,21 +110,16 @@ static void conference_peer_nick_test(Tox **toxes, State *state) fprintf(stderr, "Waiting for invitation to arrive and peers to be in the group\n"); do { - tox_iterate(toxes[0], &state[0]); - tox_iterate(toxes[1], &state[1]); - - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(2, toxes, state, ITERATION_INTERVAL); } while (!state[0].joined || !state[1].joined || !state[0].friend_in_group || !state[1].friend_in_group); fprintf(stderr, "Running tox0, but not tox1, waiting for tox1 to drop out\n"); do { - tox_iterate(toxes[0], &state[0]); + iterate_all_wait(1, toxes, state, 1000); // Rebuild peer list after every iteration. rebuild_peer_list(toxes[0]); - - c_sleep(ITERATION_INTERVAL); } while (state[0].friend_in_group); fprintf(stderr, "Invitations accepted\n"); diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 25316496..232b7d95 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -600,12 +600,18 @@ static void ip_callback(void *data, int32_t number, IP_Port ip_port) #define NUM_DHT_FRIENDS 20 +static uint64_t get_clock_callback(void *user_data) +{ + return *(uint64_t *)user_data; +} + static void test_DHT_test(void) { uint32_t to_comp = 8394782; DHT *dhts[NUM_DHT]; Logger *logs[NUM_DHT]; Mono_Time *mono_times[NUM_DHT]; + uint64_t clock[NUM_DHT]; uint32_t index[NUM_DHT]; uint32_t i, j; @@ -619,6 +625,8 @@ static void test_DHT_test(void) logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]); mono_times[i] = mono_time_new(); + clock[i] = current_time_monotonic(mono_times[i]); + mono_time_set_current_time_callback(mono_times[i], get_clock_callback, &clock[i]); dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true); ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); @@ -674,9 +682,10 @@ loop_top: mono_time_update(mono_times[i]); networking_poll(dhts[i]->net, nullptr); do_dht(dhts[i]); + clock[i] += 500; } - c_sleep(500); + c_sleep(20); } for (i = 0; i < NUM_DHT; ++i) { diff --git a/auto_tests/friend_connection_test.c b/auto_tests/friend_connection_test.c index a165678c..ab91e0f0 100644 --- a/auto_tests/friend_connection_test.c +++ b/auto_tests/friend_connection_test.c @@ -13,6 +13,7 @@ typedef struct State { uint32_t index; + uint64_t clock; } State; #include "run_auto_test.h" diff --git a/auto_tests/lossless_packet_test.c b/auto_tests/lossless_packet_test.c index 9d9fe71b..5b8ee947 100644 --- a/auto_tests/lossless_packet_test.c +++ b/auto_tests/lossless_packet_test.c @@ -18,6 +18,8 @@ typedef struct State { uint32_t index; + uint64_t clock; + bool custom_packet_received; } State; @@ -51,10 +53,7 @@ static void test_lossless_packet(Tox **toxes, State *state) ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret); do { - tox_iterate(toxes[0], nullptr); - tox_iterate(toxes[1], &state[1]); - - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(2, toxes, state, ITERATION_INTERVAL); } while (!state[1].custom_packet_received); } diff --git a/auto_tests/lossy_packet_test.c b/auto_tests/lossy_packet_test.c index 51081a39..10d4e505 100644 --- a/auto_tests/lossy_packet_test.c +++ b/auto_tests/lossy_packet_test.c @@ -16,6 +16,8 @@ typedef struct State { uint32_t index; + uint64_t clock; + bool custom_packet_received; } State; @@ -47,9 +49,7 @@ static void test_lossy_packet(Tox **toxes, State *state) ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret); do { - tox_iterate(toxes[0], nullptr); - tox_iterate(toxes[1], &state[1]); - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(2, toxes, state, ITERATION_INTERVAL); } while (!state[1].custom_packet_received); } diff --git a/auto_tests/overflow_recvq_test.c b/auto_tests/overflow_recvq_test.c index 55f9e459..f70618c4 100644 --- a/auto_tests/overflow_recvq_test.c +++ b/auto_tests/overflow_recvq_test.c @@ -9,6 +9,8 @@ typedef struct State { uint32_t index; + uint64_t clock; + uint32_t recv_count; } State; @@ -50,8 +52,7 @@ static void net_crypto_overflow_test(Tox **toxes, State *state) // TODO(iphydf): Wait until all messages have arrived. Currently, not all // messages arrive, so this test would always fail. for (uint32_t i = 0; i < 200; i++) { - iterate_all(3, toxes, state); - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(3, toxes, state, ITERATION_INTERVAL); } printf("tox%u received %u messages\n", state[0].index, state[0].recv_count); diff --git a/auto_tests/overflow_sendq_test.c b/auto_tests/overflow_sendq_test.c index 0bdc6073..ce132d63 100644 --- a/auto_tests/overflow_sendq_test.c +++ b/auto_tests/overflow_sendq_test.c @@ -9,6 +9,7 @@ typedef struct State { uint32_t index; + uint64_t clock; } State; #include "run_auto_test.h" diff --git a/auto_tests/reconnect_test.c b/auto_tests/reconnect_test.c index 76cabd17..e56d818c 100644 --- a/auto_tests/reconnect_test.c +++ b/auto_tests/reconnect_test.c @@ -24,6 +24,7 @@ typedef struct State { uint32_t index; + uint64_t clock; } State; #include "run_auto_test.h" @@ -64,9 +65,7 @@ static void test_reconnect(Tox **toxes, State *state) printf("letting connections settle\n"); do { - iterate_all(TOX_COUNT, toxes, state); - - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(TOX_COUNT, toxes, state, ITERATION_INTERVAL); } while (time(nullptr) - test_start_time < 2); uint16_t disconnect = random_u16() % TOX_COUNT; @@ -76,25 +75,24 @@ static void test_reconnect(Tox **toxes, State *state) for (uint16_t i = 0; i < TOX_COUNT; ++i) { if (i != disconnect) { tox_iterate(toxes[i], &state[i]); + state[i].clock += 1000; } } - c_sleep(ITERATION_INTERVAL); + c_sleep(20); } while (!all_disconnected_from(TOX_COUNT, toxes, state, disconnect)); - const time_t reconnect_start_time = time(nullptr); + const uint64_t reconnect_start_time = state[0].clock; printf("reconnecting\n"); do { - iterate_all(TOX_COUNT, toxes, state); - - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(TOX_COUNT, toxes, state, ITERATION_INTERVAL); } while (!all_friends_connected(TOX_COUNT, toxes)); - 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); + const uint64_t reconnect_time = state[0].clock - reconnect_start_time; + ck_assert_msg(reconnect_time <= RECONNECT_TIME_MAX * 1000, "reconnection took %d seconds; expected at most %d seconds", + (int)(reconnect_time / 1000), RECONNECT_TIME_MAX); printf("test_reconnect succeeded, took %d seconds\n", (int)(time(nullptr) - test_start_time)); } diff --git a/auto_tests/run_auto_test.h b/auto_tests/run_auto_test.h index ad89992a..602ad524 100644 --- a/auto_tests/run_auto_test.h +++ b/auto_tests/run_auto_test.h @@ -2,6 +2,8 @@ #include "check_compat.h" #include "../testing/misc_tools.h" +#include "../toxcore/Messenger.h" +#include "../toxcore/mono_time.h" static bool all_connected(uint32_t tox_count, Tox **toxes) { @@ -29,13 +31,20 @@ static bool all_friends_connected(uint32_t tox_count, Tox **toxes) return true; } -static bool iterate_all(uint32_t tox_count, Tox **toxes, State *state) +static void iterate_all_wait(uint32_t tox_count, Tox **toxes, State *state, uint32_t wait) { for (uint32_t i = 0; i < tox_count; i++) { tox_iterate(toxes[i], &state[i]); + state[i].clock += wait; } - return true; + /* Also actually sleep a little, to allow for local network processing */ + c_sleep(20); +} + +static uint64_t get_state_clock_callback(void *user_data) +{ + return ((State *)user_data)->clock; } static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *state)) @@ -48,6 +57,12 @@ static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *stat state[i].index = i; toxes[i] = tox_new_log(nullptr, nullptr, &state[i].index); ck_assert_msg(toxes[i], "failed to create %u tox instances", i + 1); + + // TODO(iphydf): Don't rely on toxcore internals. + Mono_Time *mono_time = (*(Messenger **)toxes[i])->mono_time; + + state[i].clock = current_time_monotonic(mono_time); + mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &state[i]); } printf("toxes all add each other as friends\n"); @@ -73,17 +88,13 @@ static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *stat } do { - iterate_all(tox_count, toxes, state); - - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(tox_count, toxes, state, ITERATION_INTERVAL); } while (!all_connected(tox_count, toxes)); printf("toxes are online\n"); do { - iterate_all(tox_count, toxes, state); - - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(tox_count, toxes, state, ITERATION_INTERVAL); } while (!all_friends_connected(tox_count, toxes)); printf("tox clients connected\n"); diff --git a/auto_tests/send_message_test.c b/auto_tests/send_message_test.c index 8857d232..81969d59 100644 --- a/auto_tests/send_message_test.c +++ b/auto_tests/send_message_test.c @@ -11,6 +11,8 @@ typedef struct State { uint32_t index; + uint64_t clock; + bool message_received; } State; @@ -51,10 +53,7 @@ static void send_message_test(Tox **toxes, State *state) ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm); do { - tox_iterate(toxes[0], &state[0]); - tox_iterate(toxes[1], &state[1]); - - c_sleep(ITERATION_INTERVAL); + iterate_all_wait(2, toxes, state, ITERATION_INTERVAL); } while (!state[1].message_received); }