diff options
author | zugz (tox) <mbays+tox@sdf.org> | 2018-08-14 18:37:32 +0200 |
---|---|---|
committer | zugz (tox) <mbays+tox@sdf.org> | 2018-08-27 22:27:01 +0200 |
commit | 66ab386d6fb0200d544eb841a05b68fc7151101e (patch) | |
tree | 4d7d21d683ccbf9f1270ea036bb94f0d28c23bce /auto_tests | |
parent | 0075374f2bb3bb72ef1112471f1aacf12b6b6658 (diff) |
try ipv6 connections even after udp timeout
Also adds a test (auto_reconnect_test) which fails without this change.
Diffstat (limited to 'auto_tests')
-rw-r--r-- | auto_tests/Makefile.inc | 5 | ||||
-rw-r--r-- | auto_tests/reconnect_test.c | 108 |
2 files changed, 113 insertions, 0 deletions
diff --git a/auto_tests/Makefile.inc b/auto_tests/Makefile.inc index 6df92dcd..3a4b6c7e 100644 --- a/auto_tests/Makefile.inc +++ b/auto_tests/Makefile.inc | |||
@@ -24,6 +24,7 @@ TESTS = \ | |||
24 | onion_test \ | 24 | onion_test \ |
25 | overflow_recvq_test \ | 25 | overflow_recvq_test \ |
26 | overflow_sendq_test \ | 26 | overflow_sendq_test \ |
27 | reconnect_test \ | ||
27 | save_compatibility_test \ | 28 | save_compatibility_test \ |
28 | save_friend_test \ | 29 | save_friend_test \ |
29 | save_load_test \ | 30 | save_load_test \ |
@@ -154,6 +155,10 @@ overflow_sendq_test_SOURCES = ../auto_tests/overflow_sendq_test.c | |||
154 | overflow_sendq_test_CFLAGS = $(AUTOTEST_CFLAGS) | 155 | overflow_sendq_test_CFLAGS = $(AUTOTEST_CFLAGS) |
155 | overflow_sendq_test_LDADD = $(AUTOTEST_LDADD) | 156 | overflow_sendq_test_LDADD = $(AUTOTEST_LDADD) |
156 | 157 | ||
158 | reconnect_test_SOURCES = ../auto_tests/reconnect_test.c | ||
159 | reconnect_test_CFLAGS = $(AUTO_TEST_CFLAGS) | ||
160 | reconnect_test_LDADD = $(AUTOTEST_LDADD) | ||
161 | |||
157 | save_compatibility_test_SOURCES = ../auto_tests/save_compatibility_test.c | 162 | save_compatibility_test_SOURCES = ../auto_tests/save_compatibility_test.c |
158 | save_compatibility_test_CFLAGS = $(AUTOTEST_CFLAGS) | 163 | save_compatibility_test_CFLAGS = $(AUTOTEST_CFLAGS) |
159 | save_compatibility_test_LDADD = $(AUTOTEST_LDADD) | 164 | save_compatibility_test_LDADD = $(AUTOTEST_LDADD) |
diff --git a/auto_tests/reconnect_test.c b/auto_tests/reconnect_test.c new file mode 100644 index 00000000..76cabd17 --- /dev/null +++ b/auto_tests/reconnect_test.c | |||
@@ -0,0 +1,108 @@ | |||
1 | /* Auto Tests: Reconnection. | ||
2 | * | ||
3 | * This test checks that when a tox instance is suspended for long enough that | ||
4 | * its friend connections time out, those connections are promptly | ||
5 | * re-established when the instance is resumed. | ||
6 | */ | ||
7 | |||
8 | #ifdef HAVE_CONFIG_H | ||
9 | #include "config.h" | ||
10 | #endif | ||
11 | |||
12 | #include <stdlib.h> | ||
13 | #include <string.h> | ||
14 | #include <time.h> | ||
15 | |||
16 | #include "../testing/misc_tools.h" | ||
17 | #include "../toxcore/friend_connection.h" | ||
18 | #include "../toxcore/tox.h" | ||
19 | #include "../toxcore/util.h" | ||
20 | #include "check_compat.h" | ||
21 | |||
22 | #define TOX_COUNT 2 | ||
23 | #define RECONNECT_TIME_MAX (FRIEND_CONNECTION_TIMEOUT + 3) | ||
24 | |||
25 | typedef struct State { | ||
26 | uint32_t index; | ||
27 | } State; | ||
28 | |||
29 | #include "run_auto_test.h" | ||
30 | |||
31 | static uint32_t tox_connected_count(uint32_t tox_count, Tox **toxes, State *state, uint32_t index) | ||
32 | { | ||
33 | const size_t friend_count = tox_self_get_friend_list_size(toxes[index]); | ||
34 | uint32_t connected_count = 0; | ||
35 | |||
36 | for (size_t j = 0; j < friend_count; j++) { | ||
37 | if (tox_friend_get_connection_status(toxes[index], j, nullptr) != TOX_CONNECTION_NONE) { | ||
38 | ++connected_count; | ||
39 | } | ||
40 | } | ||
41 | |||
42 | return connected_count; | ||
43 | } | ||
44 | |||
45 | static bool all_disconnected_from(uint32_t tox_count, Tox **toxes, State *state, uint32_t index) | ||
46 | { | ||
47 | for (uint32_t i = 0; i < tox_count; i++) { | ||
48 | if (i == index) { | ||
49 | continue; | ||
50 | } | ||
51 | |||
52 | if (tox_connected_count(tox_count, toxes, state, i) >= tox_count - 1) { | ||
53 | return false; | ||
54 | } | ||
55 | } | ||
56 | |||
57 | return true; | ||
58 | } | ||
59 | |||
60 | static void test_reconnect(Tox **toxes, State *state) | ||
61 | { | ||
62 | const time_t test_start_time = time(nullptr); | ||
63 | |||
64 | printf("letting connections settle\n"); | ||
65 | |||
66 | do { | ||
67 | iterate_all(TOX_COUNT, toxes, state); | ||
68 | |||
69 | c_sleep(ITERATION_INTERVAL); | ||
70 | } while (time(nullptr) - test_start_time < 2); | ||
71 | |||
72 | uint16_t disconnect = random_u16() % TOX_COUNT; | ||
73 | printf("disconnecting #%u\n", state[disconnect].index); | ||
74 | |||
75 | do { | ||
76 | for (uint16_t i = 0; i < TOX_COUNT; ++i) { | ||
77 | if (i != disconnect) { | ||
78 | tox_iterate(toxes[i], &state[i]); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | c_sleep(ITERATION_INTERVAL); | ||
83 | } while (!all_disconnected_from(TOX_COUNT, toxes, state, disconnect)); | ||
84 | |||
85 | const time_t reconnect_start_time = time(nullptr); | ||
86 | |||
87 | printf("reconnecting\n"); | ||
88 | |||
89 | do { | ||
90 | iterate_all(TOX_COUNT, toxes, state); | ||
91 | |||
92 | c_sleep(ITERATION_INTERVAL); | ||
93 | } while (!all_friends_connected(TOX_COUNT, toxes)); | ||
94 | |||
95 | const int reconnect_time = (int)(time(nullptr) - reconnect_start_time); | ||
96 | ck_assert_msg(reconnect_time <= RECONNECT_TIME_MAX, "reconnection took %d seconds; expected at most %d seconds", | ||
97 | reconnect_time, RECONNECT_TIME_MAX); | ||
98 | |||
99 | printf("test_reconnect succeeded, took %d seconds\n", (int)(time(nullptr) - test_start_time)); | ||
100 | } | ||
101 | |||
102 | int main(void) | ||
103 | { | ||
104 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
105 | |||
106 | run_auto_test(TOX_COUNT, test_reconnect); | ||
107 | return 0; | ||
108 | } | ||