summaryrefslogtreecommitdiff
path: root/auto_tests
diff options
context:
space:
mode:
authorzugz (tox) <mbays+tox@sdf.org>2018-08-17 20:34:25 +0200
committerzugz (tox) <mbays+tox@sdf.org>2018-08-31 17:08:35 +0200
commit9764285ab156c25050b9e9e46a8e8bc0be701951 (patch)
treee4e608c75a36382fe84dcdfc03d8135f48aea655 /auto_tests
parent2e4cae692e0895ce17e67fb2e1fd899c91fcb675 (diff)
Use test clock in run_auto_test tests and dht test
Diffstat (limited to 'auto_tests')
-rw-r--r--auto_tests/conference_double_invite_test.c10
-rw-r--r--auto_tests/conference_peer_nick_test.c11
-rw-r--r--auto_tests/dht_test.c11
-rw-r--r--auto_tests/friend_connection_test.c1
-rw-r--r--auto_tests/lossless_packet_test.c7
-rw-r--r--auto_tests/lossy_packet_test.c6
-rw-r--r--auto_tests/overflow_recvq_test.c5
-rw-r--r--auto_tests/overflow_sendq_test.c1
-rw-r--r--auto_tests/reconnect_test.c20
-rw-r--r--auto_tests/run_auto_test.h27
-rw-r--r--auto_tests/send_message_test.c7
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 @@
7 7
8typedef struct State { 8typedef struct State {
9 uint32_t index; 9 uint32_t index;
10 uint64_t clock;
11
10 bool self_online; 12 bool self_online;
11 bool friend_online; 13 bool friend_online;
12 14
@@ -64,10 +66,7 @@ static void conference_double_invite_test(Tox **toxes, State *state)
64 fprintf(stderr, "Waiting for invitation to arrive\n"); 66 fprintf(stderr, "Waiting for invitation to arrive\n");
65 67
66 do { 68 do {
67 tox_iterate(toxes[0], &state[0]); 69 iterate_all_wait(2, toxes, state, ITERATION_INTERVAL);
68 tox_iterate(toxes[1], &state[1]);
69
70 c_sleep(ITERATION_INTERVAL);
71 } while (!state[0].joined || !state[1].joined); 70 } while (!state[0].joined || !state[1].joined);
72 71
73 fprintf(stderr, "Invitations accepted\n"); 72 fprintf(stderr, "Invitations accepted\n");
@@ -75,8 +74,7 @@ static void conference_double_invite_test(Tox **toxes, State *state)
75 // Invite one more time, resulting in friend -1 inviting tox1 (toxes[1]). 74 // Invite one more time, resulting in friend -1 inviting tox1 (toxes[1]).
76 tox_conference_invite(toxes[0], 0, state[0].conference, nullptr); 75 tox_conference_invite(toxes[0], 0, state[0].conference, nullptr);
77 76
78 tox_iterate(toxes[0], &state[0]); 77 iterate_all_wait(2, toxes, state, ITERATION_INTERVAL);
79 tox_iterate(toxes[1], &state[1]);
80} 78}
81 79
82int main(void) 80int 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 @@
7 7
8typedef struct State { 8typedef struct State {
9 uint32_t index; 9 uint32_t index;
10 uint64_t clock;
11
10 bool self_online; 12 bool self_online;
11 bool friend_online; 13 bool friend_online;
12 bool friend_in_group; 14 bool friend_in_group;
@@ -108,21 +110,16 @@ static void conference_peer_nick_test(Tox **toxes, State *state)
108 fprintf(stderr, "Waiting for invitation to arrive and peers to be in the group\n"); 110 fprintf(stderr, "Waiting for invitation to arrive and peers to be in the group\n");
109 111
110 do { 112 do {
111 tox_iterate(toxes[0], &state[0]); 113 iterate_all_wait(2, toxes, state, ITERATION_INTERVAL);
112 tox_iterate(toxes[1], &state[1]);
113
114 c_sleep(ITERATION_INTERVAL);
115 } while (!state[0].joined || !state[1].joined || !state[0].friend_in_group || !state[1].friend_in_group); 114 } while (!state[0].joined || !state[1].joined || !state[0].friend_in_group || !state[1].friend_in_group);
116 115
117 fprintf(stderr, "Running tox0, but not tox1, waiting for tox1 to drop out\n"); 116 fprintf(stderr, "Running tox0, but not tox1, waiting for tox1 to drop out\n");
118 117
119 do { 118 do {
120 tox_iterate(toxes[0], &state[0]); 119 iterate_all_wait(1, toxes, state, 1000);
121 120
122 // Rebuild peer list after every iteration. 121 // Rebuild peer list after every iteration.
123 rebuild_peer_list(toxes[0]); 122 rebuild_peer_list(toxes[0]);
124
125 c_sleep(ITERATION_INTERVAL);
126 } while (state[0].friend_in_group); 123 } while (state[0].friend_in_group);
127 124
128 fprintf(stderr, "Invitations accepted\n"); 125 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)
600 600
601#define NUM_DHT_FRIENDS 20 601#define NUM_DHT_FRIENDS 20
602 602
603static uint64_t get_clock_callback(void *user_data)
604{
605 return *(uint64_t *)user_data;
606}
607
603static void test_DHT_test(void) 608static void test_DHT_test(void)
604{ 609{
605 uint32_t to_comp = 8394782; 610 uint32_t to_comp = 8394782;
606 DHT *dhts[NUM_DHT]; 611 DHT *dhts[NUM_DHT];
607 Logger *logs[NUM_DHT]; 612 Logger *logs[NUM_DHT];
608 Mono_Time *mono_times[NUM_DHT]; 613 Mono_Time *mono_times[NUM_DHT];
614 uint64_t clock[NUM_DHT];
609 uint32_t index[NUM_DHT]; 615 uint32_t index[NUM_DHT];
610 616
611 uint32_t i, j; 617 uint32_t i, j;
@@ -619,6 +625,8 @@ static void test_DHT_test(void)
619 logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]); 625 logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]);
620 626
621 mono_times[i] = mono_time_new(); 627 mono_times[i] = mono_time_new();
628 clock[i] = current_time_monotonic(mono_times[i]);
629 mono_time_set_current_time_callback(mono_times[i], get_clock_callback, &clock[i]);
622 630
623 dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true); 631 dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], ip, DHT_DEFAULT_PORT + i), true);
624 ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); 632 ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
@@ -674,9 +682,10 @@ loop_top:
674 mono_time_update(mono_times[i]); 682 mono_time_update(mono_times[i]);
675 networking_poll(dhts[i]->net, nullptr); 683 networking_poll(dhts[i]->net, nullptr);
676 do_dht(dhts[i]); 684 do_dht(dhts[i]);
685 clock[i] += 500;
677 } 686 }
678 687
679 c_sleep(500); 688 c_sleep(20);
680 } 689 }
681 690
682 for (i = 0; i < NUM_DHT; ++i) { 691 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 @@
13 13
14typedef struct State { 14typedef struct State {
15 uint32_t index; 15 uint32_t index;
16 uint64_t clock;
16} State; 17} State;
17 18
18#include "run_auto_test.h" 19#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 @@
18 18
19typedef struct State { 19typedef struct State {
20 uint32_t index; 20 uint32_t index;
21 uint64_t clock;
22
21 bool custom_packet_received; 23 bool custom_packet_received;
22} State; 24} State;
23 25
@@ -51,10 +53,7 @@ static void test_lossless_packet(Tox **toxes, State *state)
51 ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret); 53 ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret);
52 54
53 do { 55 do {
54 tox_iterate(toxes[0], nullptr); 56 iterate_all_wait(2, toxes, state, ITERATION_INTERVAL);
55 tox_iterate(toxes[1], &state[1]);
56
57 c_sleep(ITERATION_INTERVAL);
58 } while (!state[1].custom_packet_received); 57 } while (!state[1].custom_packet_received);
59} 58}
60 59
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 @@
16 16
17typedef struct State { 17typedef struct State {
18 uint32_t index; 18 uint32_t index;
19 uint64_t clock;
20
19 bool custom_packet_received; 21 bool custom_packet_received;
20} State; 22} State;
21 23
@@ -47,9 +49,7 @@ static void test_lossy_packet(Tox **toxes, State *state)
47 ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret); 49 ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);
48 50
49 do { 51 do {
50 tox_iterate(toxes[0], nullptr); 52 iterate_all_wait(2, toxes, state, ITERATION_INTERVAL);
51 tox_iterate(toxes[1], &state[1]);
52 c_sleep(ITERATION_INTERVAL);
53 } while (!state[1].custom_packet_received); 53 } while (!state[1].custom_packet_received);
54} 54}
55 55
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 @@
9 9
10typedef struct State { 10typedef struct State {
11 uint32_t index; 11 uint32_t index;
12 uint64_t clock;
13
12 uint32_t recv_count; 14 uint32_t recv_count;
13} State; 15} State;
14 16
@@ -50,8 +52,7 @@ static void net_crypto_overflow_test(Tox **toxes, State *state)
50 // TODO(iphydf): Wait until all messages have arrived. Currently, not all 52 // TODO(iphydf): Wait until all messages have arrived. Currently, not all
51 // messages arrive, so this test would always fail. 53 // messages arrive, so this test would always fail.
52 for (uint32_t i = 0; i < 200; i++) { 54 for (uint32_t i = 0; i < 200; i++) {
53 iterate_all(3, toxes, state); 55 iterate_all_wait(3, toxes, state, ITERATION_INTERVAL);
54 c_sleep(ITERATION_INTERVAL);
55 } 56 }
56 57
57 printf("tox%u received %u messages\n", state[0].index, state[0].recv_count); 58 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 @@
9 9
10typedef struct State { 10typedef struct State {
11 uint32_t index; 11 uint32_t index;
12 uint64_t clock;
12} State; 13} State;
13 14
14#include "run_auto_test.h" 15#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 @@
24 24
25typedef struct State { 25typedef struct State {
26 uint32_t index; 26 uint32_t index;
27 uint64_t clock;
27} State; 28} State;
28 29
29#include "run_auto_test.h" 30#include "run_auto_test.h"
@@ -64,9 +65,7 @@ static void test_reconnect(Tox **toxes, State *state)
64 printf("letting connections settle\n"); 65 printf("letting connections settle\n");
65 66
66 do { 67 do {
67 iterate_all(TOX_COUNT, toxes, state); 68 iterate_all_wait(TOX_COUNT, toxes, state, ITERATION_INTERVAL);
68
69 c_sleep(ITERATION_INTERVAL);
70 } while (time(nullptr) - test_start_time < 2); 69 } while (time(nullptr) - test_start_time < 2);
71 70
72 uint16_t disconnect = random_u16() % TOX_COUNT; 71 uint16_t disconnect = random_u16() % TOX_COUNT;
@@ -76,25 +75,24 @@ static void test_reconnect(Tox **toxes, State *state)
76 for (uint16_t i = 0; i < TOX_COUNT; ++i) { 75 for (uint16_t i = 0; i < TOX_COUNT; ++i) {
77 if (i != disconnect) { 76 if (i != disconnect) {
78 tox_iterate(toxes[i], &state[i]); 77 tox_iterate(toxes[i], &state[i]);
78 state[i].clock += 1000;
79 } 79 }
80 } 80 }
81 81
82 c_sleep(ITERATION_INTERVAL); 82 c_sleep(20);
83 } while (!all_disconnected_from(TOX_COUNT, toxes, state, disconnect)); 83 } while (!all_disconnected_from(TOX_COUNT, toxes, state, disconnect));
84 84
85 const time_t reconnect_start_time = time(nullptr); 85 const uint64_t reconnect_start_time = state[0].clock;
86 86
87 printf("reconnecting\n"); 87 printf("reconnecting\n");
88 88
89 do { 89 do {
90 iterate_all(TOX_COUNT, toxes, state); 90 iterate_all_wait(TOX_COUNT, toxes, state, ITERATION_INTERVAL);
91
92 c_sleep(ITERATION_INTERVAL);
93 } while (!all_friends_connected(TOX_COUNT, toxes)); 91 } while (!all_friends_connected(TOX_COUNT, toxes));
94 92
95 const int reconnect_time = (int)(time(nullptr) - reconnect_start_time); 93 const uint64_t reconnect_time = state[0].clock - reconnect_start_time;
96 ck_assert_msg(reconnect_time <= RECONNECT_TIME_MAX, "reconnection took %d seconds; expected at most %d seconds", 94 ck_assert_msg(reconnect_time <= RECONNECT_TIME_MAX * 1000, "reconnection took %d seconds; expected at most %d seconds",
97 reconnect_time, RECONNECT_TIME_MAX); 95 (int)(reconnect_time / 1000), RECONNECT_TIME_MAX);
98 96
99 printf("test_reconnect succeeded, took %d seconds\n", (int)(time(nullptr) - test_start_time)); 97 printf("test_reconnect succeeded, took %d seconds\n", (int)(time(nullptr) - test_start_time));
100} 98}
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 @@
2 2
3#include "check_compat.h" 3#include "check_compat.h"
4#include "../testing/misc_tools.h" 4#include "../testing/misc_tools.h"
5#include "../toxcore/Messenger.h"
6#include "../toxcore/mono_time.h"
5 7
6static bool all_connected(uint32_t tox_count, Tox **toxes) 8static bool all_connected(uint32_t tox_count, Tox **toxes)
7{ 9{
@@ -29,13 +31,20 @@ static bool all_friends_connected(uint32_t tox_count, Tox **toxes)
29 return true; 31 return true;
30} 32}
31 33
32static bool iterate_all(uint32_t tox_count, Tox **toxes, State *state) 34static void iterate_all_wait(uint32_t tox_count, Tox **toxes, State *state, uint32_t wait)
33{ 35{
34 for (uint32_t i = 0; i < tox_count; i++) { 36 for (uint32_t i = 0; i < tox_count; i++) {
35 tox_iterate(toxes[i], &state[i]); 37 tox_iterate(toxes[i], &state[i]);
38 state[i].clock += wait;
36 } 39 }
37 40
38 return true; 41 /* Also actually sleep a little, to allow for local network processing */
42 c_sleep(20);
43}
44
45static uint64_t get_state_clock_callback(void *user_data)
46{
47 return ((State *)user_data)->clock;
39} 48}
40 49
41static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *state)) 50static 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
48 state[i].index = i; 57 state[i].index = i;
49 toxes[i] = tox_new_log(nullptr, nullptr, &state[i].index); 58 toxes[i] = tox_new_log(nullptr, nullptr, &state[i].index);
50 ck_assert_msg(toxes[i], "failed to create %u tox instances", i + 1); 59 ck_assert_msg(toxes[i], "failed to create %u tox instances", i + 1);
60
61 // TODO(iphydf): Don't rely on toxcore internals.
62 Mono_Time *mono_time = (*(Messenger **)toxes[i])->mono_time;
63
64 state[i].clock = current_time_monotonic(mono_time);
65 mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &state[i]);
51 } 66 }
52 67
53 printf("toxes all add each other as friends\n"); 68 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
73 } 88 }
74 89
75 do { 90 do {
76 iterate_all(tox_count, toxes, state); 91 iterate_all_wait(tox_count, toxes, state, ITERATION_INTERVAL);
77
78 c_sleep(ITERATION_INTERVAL);
79 } while (!all_connected(tox_count, toxes)); 92 } while (!all_connected(tox_count, toxes));
80 93
81 printf("toxes are online\n"); 94 printf("toxes are online\n");
82 95
83 do { 96 do {
84 iterate_all(tox_count, toxes, state); 97 iterate_all_wait(tox_count, toxes, state, ITERATION_INTERVAL);
85
86 c_sleep(ITERATION_INTERVAL);
87 } while (!all_friends_connected(tox_count, toxes)); 98 } while (!all_friends_connected(tox_count, toxes));
88 99
89 printf("tox clients connected\n"); 100 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 @@
11 11
12typedef struct State { 12typedef struct State {
13 uint32_t index; 13 uint32_t index;
14 uint64_t clock;
15
14 bool message_received; 16 bool message_received;
15} State; 17} State;
16 18
@@ -51,10 +53,7 @@ static void send_message_test(Tox **toxes, State *state)
51 ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm); 53 ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm);
52 54
53 do { 55 do {
54 tox_iterate(toxes[0], &state[0]); 56 iterate_all_wait(2, toxes, state, ITERATION_INTERVAL);
55 tox_iterate(toxes[1], &state[1]);
56
57 c_sleep(ITERATION_INTERVAL);
58 } while (!state[1].message_received); 57 } while (!state[1].message_received);
59} 58}
60 59