forked from irungentoo/toxcore
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* Auto Tests: Conferences. | ||
*/ | ||
|
||
#ifdef HAVE_CONFIG_H | ||
#include "config.h" | ||
#endif | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
#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; | ||
} |