Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Improve the stability of the save_load test #2160

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions auto_tests/save_load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,34 @@ static void reload_tox(Tox **tox, struct Tox_Options *const in_opts, void *user_
free(buffer);
}

typedef struct Time_Data {
pthread_mutex_t lock;
uint64_t clock;
} Time_Data;

static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data)
{
Time_Data *time_data = (Time_Data *)user_data;
pthread_mutex_lock(&time_data->lock);
uint64_t clock = time_data->clock;
pthread_mutex_unlock(&time_data->lock);
return clock;
}

static void increment_clock(Time_Data *time_data, uint64_t count)
{
pthread_mutex_lock(&time_data->lock);
time_data->clock += count;
pthread_mutex_unlock(&time_data->lock);
}

static void set_current_time_callback(Tox *tox, Time_Data *time_data)
{
// TODO(iphydf): Don't rely on toxcore internals.
Mono_Time *mono_time = ((Messenger *)tox)->mono_time;
mono_time_set_current_time_callback(mono_time, get_state_clock_callback, time_data);
}

static void test_few_clients(void)
{
uint32_t index[] = { 1, 2, 3 };
Expand All @@ -129,6 +157,14 @@ static void test_few_clients(void)

ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances");


Time_Data time_data;
ck_assert_msg(pthread_mutex_init(&time_data.lock, nullptr) == 0, "Failed to init time_data mutex");
time_data.clock = current_time_monotonic(((Messenger *)tox1)->mono_time);
set_current_time_callback(tox1, &time_data);
set_current_time_callback(tox2, &time_data);
set_current_time_callback(tox3, &time_data);

uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(tox1, dht_key);
const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
Expand Down Expand Up @@ -169,12 +205,17 @@ static void test_few_clients(void)
}
}

c_sleep(ITERATION_INTERVAL);
increment_clock(&time_data, 200);
c_sleep(5);
}

ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1);
printf("tox clients connected took %lu seconds\n", (unsigned long)(time(nullptr) - con_time));

// We're done with this callback, so unset it to ensure we don't fail the
// test if tox1 goes offline while tox2 and 3 are reloaded.
tox_callback_self_connection_status(tox1, nullptr);

reload_tox(&tox2, opts2, &index[1]);

reload_tox(&tox3, opts3, &index[2]);
Expand Down Expand Up @@ -202,7 +243,8 @@ static void test_few_clients(void)
}
}

c_sleep(ITERATION_INTERVAL);
increment_clock(&time_data, 100);
c_sleep(5);
}

printf("tox clients connected took %lu seconds\n", (unsigned long)(time(nullptr) - con_time));
Expand Down