diff options
author | Nick Mathewson <nickm@torproject.org> | 2016-04-15 09:31:18 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2016-05-09 14:04:54 -0400 |
commit | 11a09778d67cba7f118a7425bda313316ac7ecc2 (patch) | |
tree | 192fc3002c00522ecb77ab3a7c7b2d4fd3e6cd47 /src/test/test-timers.c | |
parent | 10fd4535c2109c9532585aca2c429f741937364c (diff) | |
download | tor-11a09778d67cba7f118a7425bda313316ac7ecc2.tar.gz tor-11a09778d67cba7f118a7425bda313316ac7ecc2.zip |
Test coverage for timers.
Diffstat (limited to 'src/test/test-timers.c')
-rw-r--r-- | src/test/test-timers.c | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/src/test/test-timers.c b/src/test/test-timers.c index 83a5e1ba41..10db20fbfc 100644 --- a/src/test/test-timers.c +++ b/src/test/test-timers.c @@ -21,12 +21,15 @@ #define N_TIMERS 1000 #define MAX_DURATION 30 +#define N_DISABLE 5 static struct timeval fire_at[N_TIMERS] = {{0,0}}; +static int is_disabled[N_TIMERS] = {0}; static int fired[N_TIMERS] = {0}; static struct timeval difference[N_TIMERS] = {{0,0}}; static tor_timer_t *timers[N_TIMERS] = {NULL}; +static int n_active_timers = 0; static int n_fired = 0; static void @@ -39,7 +42,7 @@ timer_cb(tor_timer_t *t, void *arg, const struct timeval *now) timersub(now, &fire_at[idx], &difference[idx]); ++n_fired; // printf("%d / %d\n",n_fired, N_TIMERS); - if (n_fired == N_TIMERS) { + if (n_fired == n_active_timers) { event_base_loopbreak(tor_libevent_get_base()); } } @@ -55,6 +58,7 @@ main(int argc, char **argv) timers_initialize(); int i; + int ret; struct timeval now; tor_gettimeofday(&now); for (i = 0; i < N_TIMERS; ++i) { @@ -62,26 +66,41 @@ main(int argc, char **argv) delay.tv_sec = crypto_rand_int_range(0,MAX_DURATION); delay.tv_usec = crypto_rand_int_range(0,1000000); timeradd(&now, &delay, &fire_at[i]); - timers[i] = timer_new(timer_cb, &timers[i], 0); + timers[i] = timer_new(timer_cb, &timers[i]); timer_schedule(timers[i], &delay); + ++n_active_timers; + } + + /* Disable some; we'll make sure they don't trigger. */ + for (i = 0; i < N_DISABLE; ++i) { + int idx = crypto_rand_int_range(0, N_TIMERS); + if (is_disabled[idx]) + continue; + is_disabled[idx] = 1; + timer_disable(timers[idx]); + --n_active_timers; } event_base_loop(tor_libevent_get_base(), 0); uint64_t total_difference = 0; uint64_t total_square_difference = 0; - tor_assert(n_fired == N_TIMERS); + tor_assert(n_fired == n_active_timers); for (i = 0; i < N_TIMERS; ++i) { + if (is_disabled[i]) { + tor_assert(fired[i] == 0); + continue; + } tor_assert(fired[i] == 1); uint64_t diff = difference[i].tv_usec + difference[i].tv_sec * 1000000; total_difference += diff; total_square_difference += diff*diff; } - const uint64_t mean_diff = total_difference / N_TIMERS; + const uint64_t mean_diff = total_difference / n_active_timers; printf("mean difference: "U64_FORMAT" usec\n", U64_PRINTF_ARG(mean_diff)); - const double mean_sq = ((double)total_square_difference) / N_TIMERS; + const double mean_sq = ((double)total_square_difference)/ n_active_timers; const double sq_mean = mean_diff * mean_diff; const double stddev = sqrt(mean_sq - sq_mean); printf("standard deviation: %lf usec\n", stddev); @@ -89,13 +108,21 @@ main(int argc, char **argv) if (mean_diff > 500*1000 || stddev > 500*1000) { printf("Either your system is under ridiculous load, or the " "timer backend is broken.\n"); - return 1; + ret = 1; } else if (mean_diff > 2000 || stddev > 2000) { printf("Either your system is a bit slow or the " "timer backend is odd.\n"); - return 0; + ret = 0; } else { printf("Looks good enough.\n"); + ret = 0; + } + + timer_free(NULL); + + for (i = 0; i < N_TIMERS; ++i) { + timer_free(timers[i]); } - return 0; + timers_shutdown(); + return ret; } |