| 1 |
#include "uv.h" |
| 2 |
|
| 3 |
#define TEST_UV_MAX_LOOP_RUN 10 /* Max n. of loop iterations upon teardown */ |
| 4 |
|
| 5 |
void test_uv_setup(const MunitParameter params[], struct uv_loop_s *l) |
| 6 |
{ |
| 7 |
int rv; |
| 8 |
|
| 9 |
(void)params; |
| 10 |
|
| 11 |
rv = uv_loop_init(l); |
| 12 |
munit_assert_int(rv, ==, 0); |
| 13 |
} |
| 14 |
|
| 15 |
int test_uv_run(struct uv_loop_s *l, unsigned n) |
| 16 |
{ |
| 17 |
unsigned i; |
| 18 |
int rv; |
| 19 |
|
| 20 |
munit_assert_int(n, >, 0); |
| 21 |
|
| 22 |
for (i = 0; i < n; i++) { |
| 23 |
rv = uv_run(l, UV_RUN_ONCE); |
| 24 |
if (rv < 0) { |
| 25 |
munit_errorf("uv_run: %s (%d)", uv_strerror(rv), rv); |
| 26 |
} |
| 27 |
if (rv == 0) { |
| 28 |
break; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
return rv; |
| 33 |
} |
| 34 |
|
| 35 |
void test_uv_stop(struct uv_loop_s *l) |
| 36 |
{ |
| 37 |
unsigned n_handles; |
| 38 |
|
| 39 |
/* Spin a few times to trigger pending callbacks. */ |
| 40 |
n_handles = test_uv_run(l, TEST_UV_MAX_LOOP_RUN); |
| 41 |
if (n_handles > 0) { |
| 42 |
munit_errorf("loop has still %d pending active handles", |
| 43 |
n_handles); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
static void test_uv__walk_cb(uv_handle_t *handle, void *arg) |
| 48 |
{ |
| 49 |
(void)arg; |
| 50 |
|
| 51 |
munit_logf(MUNIT_LOG_INFO, "handle %d", handle->type); |
| 52 |
} |
| 53 |
|
| 54 |
void test_uv_tear_down(struct uv_loop_s *l) |
| 55 |
{ |
| 56 |
int rv; |
| 57 |
|
| 58 |
rv = uv_loop_close(l); |
| 59 |
if (rv != 0) { |
| 60 |
uv_walk(l, test_uv__walk_cb, NULL); |
| 61 |
munit_errorf("uv_loop_close: %s (%d)", uv_strerror(rv), rv); |
| 62 |
} |
| 63 |
|
| 64 |
rv = uv_replace_allocator(malloc, realloc, calloc, free); |
| 65 |
munit_assert_int(rv, ==, 0); |
| 66 |
} |