From 7c6ce24b028a8c201ac8eb1513a6424205c4d8d8 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Fri, 27 Dec 2019 15:57:34 -0800 Subject: fixes #1042 testutil.h could offer better test failure diagnosis --- tests/aio.c | 64 +++++++++++++++++++++++++++++--------------------------- tests/testutil.h | 18 ++++++++++++++++ 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/tests/aio.c b/tests/aio.c index fdc68cb8..0fe665b1 100644 --- a/tests/aio.c +++ b/tests/aio.c @@ -15,6 +15,7 @@ #include #include "acutest.h" +#include "testutil.h" void cbdone(void *p) @@ -42,11 +43,11 @@ test_sleep(void) nng_time end = 0; nng_aio *saio; - TEST_CHECK(nng_aio_alloc(&saio, sleepdone, &end) == 0); + TEST_NNG_PASS(nng_aio_alloc(&saio, sleepdone, &end)); start = nng_clock(); nng_sleep_aio(200, saio); nng_aio_wait(saio); - TEST_CHECK(nng_aio_result(saio) == 0); + TEST_NNG_PASS(nng_aio_result(saio)); TEST_CHECK(end != 0); TEST_CHECK((end - start) >= 200); TEST_CHECK((end - start) <= 1000); @@ -61,12 +62,13 @@ test_sleep_timeout(void) nng_time start = 0; nng_time end = 0; nng_aio *saio; + TEST_CHECK(nng_aio_alloc(&saio, sleepdone, &end) == 0); nng_aio_set_timeout(saio, 100); start = nng_clock(); nng_sleep_aio(2000, saio); nng_aio_wait(saio); - TEST_CHECK(nng_aio_result(saio) == NNG_ETIMEDOUT); + TEST_NNG_FAIL(nng_aio_result(saio), NNG_ETIMEDOUT); TEST_CHECK(end != 0); TEST_CHECK((end - start) >= 100); TEST_CHECK((end - start) <= 1000); @@ -81,8 +83,8 @@ test_insane_niov(void) nng_aio *aio; nng_iov iov; - TEST_CHECK(nng_aio_alloc(&aio, NULL, NULL) == 0); - TEST_CHECK(nng_aio_set_iov(aio, 1024, &iov) == NNG_EINVAL); + TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); + TEST_NNG_FAIL(nng_aio_set_iov(aio, 1024, &iov), NNG_EINVAL); nng_aio_free(aio); } @@ -92,7 +94,7 @@ test_provider_cancel(void) nng_aio *aio; int rv = 0; // We fake an empty provider that does not do anything. - TEST_CHECK(nng_aio_alloc(&aio, NULL, NULL) == 0); + TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); TEST_CHECK(nng_aio_begin(aio) == true); nng_aio_defer(aio, cancelfn, &rv); nng_aio_cancel(aio); @@ -134,20 +136,20 @@ test_traffic(void) nng_msg * m; char * addr = "inproc://traffic"; - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_pair1_open(&s2) == 0); + TEST_NNG_PASS(nng_pair1_open(&s1)); + TEST_NNG_PASS(nng_pair1_open(&s2)); - TEST_CHECK(nng_listen(s1, addr, NULL, 0) == 0); - TEST_CHECK(nng_dial(s2, addr, NULL, 0) == 0); + TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0)); + TEST_NNG_PASS(nng_dial(s2, addr, NULL, 0)); - TEST_CHECK(nng_aio_alloc(&rxaio, cbdone, &rxdone) == 0); - TEST_CHECK(nng_aio_alloc(&txaio, cbdone, &txdone) == 0); + TEST_NNG_PASS(nng_aio_alloc(&rxaio, cbdone, &rxdone)); + TEST_NNG_PASS(nng_aio_alloc(&txaio, cbdone, &txdone)); nng_aio_set_timeout(rxaio, 1000); nng_aio_set_timeout(txaio, 1000); - TEST_CHECK(nng_msg_alloc(&m, 0) == 0); - TEST_CHECK(nng_msg_append(m, "hello", strlen("hello")) == 0); + TEST_NNG_PASS(nng_msg_alloc(&m, 0)); + TEST_NNG_PASS(nng_msg_append(m, "hello", strlen("hello"))); nng_recv_aio(s2, rxaio); @@ -157,8 +159,8 @@ test_traffic(void) nng_aio_wait(txaio); nng_aio_wait(rxaio); - TEST_CHECK(nng_aio_result(rxaio) == 0); - TEST_CHECK(nng_aio_result(txaio) == 0); + TEST_NNG_PASS(nng_aio_result(rxaio)); + TEST_NNG_PASS(nng_aio_result(txaio)); TEST_CHECK((m = nng_aio_get_msg(rxaio)) != NULL); TEST_CHECK(nng_msg_len(m) == strlen("hello")); @@ -171,8 +173,8 @@ test_traffic(void) nng_aio_free(rxaio); nng_aio_free(txaio); - TEST_CHECK(nng_close(s1) == 0); - TEST_CHECK(nng_close(s2) == 0); + TEST_NNG_PASS(nng_close(s1)); + TEST_NNG_PASS(nng_close(s2)); } void @@ -182,15 +184,15 @@ test_explicit_timeout(void) nng_aio * a; int done = 0; - TEST_CHECK(nng_pair1_open(&s) == 0); - TEST_CHECK(nng_aio_alloc(&a, cbdone, &done) == 0); + TEST_NNG_PASS(nng_pair1_open(&s)); + TEST_NNG_PASS(nng_aio_alloc(&a, cbdone, &done)); nng_aio_set_timeout(a, 40); nng_recv_aio(s, a); nng_aio_wait(a); TEST_CHECK(done == 1); - TEST_CHECK(nng_aio_result(a) == NNG_ETIMEDOUT); + TEST_NNG_FAIL(nng_aio_result(a), NNG_ETIMEDOUT); nng_aio_free(a); - TEST_CHECK(nng_close(s) == 0); + TEST_NNG_PASS(nng_close(s)); } void @@ -200,15 +202,15 @@ test_inherited_timeout(void) nng_aio * a; int done = 0; - TEST_CHECK(nng_pair1_open(&s) == 0); - TEST_CHECK(nng_aio_alloc(&a, cbdone, &done) == 0); - TEST_CHECK(nng_setopt_ms(s, NNG_OPT_RECVTIMEO, 40) == 0); + TEST_NNG_PASS(nng_pair1_open(&s)); + TEST_NNG_PASS(nng_aio_alloc(&a, cbdone, &done)); + TEST_NNG_PASS(nng_setopt_ms(s, NNG_OPT_RECVTIMEO, 40)); nng_recv_aio(s, a); nng_aio_wait(a); TEST_CHECK(done == 1); - TEST_CHECK(nng_aio_result(a) == NNG_ETIMEDOUT); + TEST_NNG_FAIL(nng_aio_result(a), NNG_ETIMEDOUT); nng_aio_free(a); - TEST_CHECK(nng_close(s) == 0); + TEST_NNG_PASS(nng_close(s)); } void @@ -218,15 +220,15 @@ test_zero_timeout(void) nng_aio * a; int done = 0; - TEST_CHECK(nng_pair1_open(&s) == 0); - TEST_CHECK(nng_aio_alloc(&a, cbdone, &done) == 0); + TEST_NNG_PASS(nng_pair1_open(&s)); + TEST_NNG_PASS(nng_aio_alloc(&a, cbdone, &done)); nng_aio_set_timeout(a, NNG_DURATION_ZERO); nng_recv_aio(s, a); nng_aio_wait(a); TEST_CHECK(done == 1); - TEST_CHECK(nng_aio_result(a) == NNG_ETIMEDOUT); + TEST_NNG_FAIL(nng_aio_result(a), NNG_ETIMEDOUT); nng_aio_free(a); - TEST_CHECK(nng_close(s) == 0); + TEST_NNG_PASS(nng_close(s)); } TEST_LIST = { diff --git a/tests/testutil.h b/tests/testutil.h index 97540f36..b3c3bbd2 100644 --- a/tests/testutil.h +++ b/tests/testutil.h @@ -42,6 +42,24 @@ extern uint16_t testutil_next_port(void); // are fully connected. extern int testutil_marry(nng_socket, nng_socket); +// TEST_NNG_PASS tests for NNG success. It reports the failure if it +// did not. +#define TEST_NNG_PASS(cond) \ + do { \ + int result_ = (cond); \ + TEST_CHECK_(result_ == 0, "%s failed: %s (%d)", #cond, \ + nng_strerror(result_), result_); \ + } while (0) + +#define TEST_NNG_FAIL(cond, expect) \ + do { \ + int result_ = (cond); \ + TEST_CHECK_(result_ == expect, \ + "%s: expected %s (%d), got %s (%d)", #cond, \ + nng_strerror(expect), expect, nng_strerror(result_), \ + result_); \ + } while (0) + #ifdef __cplusplus }; #endif -- cgit v1.2.3-70-g09d2