| 1 |
/** |
| 2 |
* Add support for using the libuv loop in tests. |
| 3 |
*/ |
| 4 |
|
| 5 |
#ifndef TEST_UV_H |
| 6 |
#define TEST_UV_H |
| 7 |
|
| 8 |
#include <uv.h> |
| 9 |
|
| 10 |
#include "munit.h" |
| 11 |
|
| 12 |
/* Max n. of loop iterations ran by a single function call */ |
| 13 |
#define TEST_UV_MAX_LOOP_RUN 10 |
| 14 |
|
| 15 |
/** |
| 16 |
* Initialize the given libuv loop. |
| 17 |
*/ |
| 18 |
void test_uv_setup(const MunitParameter params[], struct uv_loop_s *l); |
| 19 |
|
| 20 |
/** |
| 21 |
* Run the loop until there are no pending active handles. |
| 22 |
* |
| 23 |
* If there are still pending active handles after 10 loop iterations, the test |
| 24 |
* will fail. |
| 25 |
* |
| 26 |
* This is meant to be used in tear down functions. |
| 27 |
*/ |
| 28 |
void test_uv_stop(struct uv_loop_s *l); |
| 29 |
|
| 30 |
/** |
| 31 |
* Tear down the loop making sure no active handles are left. |
| 32 |
*/ |
| 33 |
void test_uv_tear_down(struct uv_loop_s *l); |
| 34 |
|
| 35 |
/** |
| 36 |
* Run the loop until there are no pending active handles or the given amount of |
| 37 |
* iterations is reached. |
| 38 |
* |
| 39 |
* Return non-zero if there are pending handles. |
| 40 |
*/ |
| 41 |
int test_uv_run(struct uv_loop_s *l, unsigned n); |
| 42 |
|
| 43 |
/** |
| 44 |
* Run the loop until the given function returns true. |
| 45 |
* |
| 46 |
* If the loop exhausts all active handles or if #TEST_UV_MAX_LOOP_RUN is |
| 47 |
* reached without @f returning #true, the test fails. |
| 48 |
*/ |
| 49 |
#define test_uv_run_until(DATA, FUNC) \ |
| 50 |
{ \ |
| 51 |
unsigned i; \ |
| 52 |
int rv; \ |
| 53 |
for (i = 0; i < TEST_UV_MAX_LOOP_RUN; i++) { \ |
| 54 |
if (FUNC(DATA)) { \ |
| 55 |
break; \ |
| 56 |
} \ |
| 57 |
rv = uv_run(&f->loop, UV_RUN_ONCE); \ |
| 58 |
if (rv < 0) { \ |
| 59 |
munit_errorf("uv_run: %s", uv_strerror(rv)); \ |
| 60 |
} \ |
| 61 |
if (rv == 0) { \ |
| 62 |
if (FUNC(DATA)) { \ |
| 63 |
break; \ |
| 64 |
} \ |
| 65 |
munit_errorf( \ |
| 66 |
"uv_run: stopped after %u iterations", \ |
| 67 |
i + 1); \ |
| 68 |
} \ |
| 69 |
} \ |
| 70 |
if (i == TEST_UV_MAX_LOOP_RUN) { \ |
| 71 |
munit_errorf( \ |
| 72 |
"uv_run: condition not met in %d iterations", \ |
| 73 |
TEST_UV_MAX_LOOP_RUN); \ |
| 74 |
} \ |
| 75 |
} |
| 76 |
|
| 77 |
#endif /* TEST_UV_H */ |