diff options
| author | Garrett D'Amore <garrett@damore.org> | 2019-12-21 10:20:55 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2019-12-24 00:34:29 -0800 |
| commit | 3f7561417bec08226bcfeb107d94be0dbf71b09e (patch) | |
| tree | 409901d7929df5eeb7295ab971b34c2e1040f507 /tests/aio.c | |
| parent | 9e7a4aff25139703bbc375b6dda263d6d42341a8 (diff) | |
| download | nng-3f7561417bec08226bcfeb107d94be0dbf71b09e.tar.gz nng-3f7561417bec08226bcfeb107d94be0dbf71b09e.tar.bz2 nng-3f7561417bec08226bcfeb107d94be0dbf71b09e.zip | |
fixes #1032 Figure out Darwin bustedness
fixes #1035 Convey is awkward -- consider acutest.h
This represents a rather large effort towards cleaning up our
testing and optional configuration infrastructure.
A separate test library is built by default, which is static, and
includes some useful utilities design to make it easier to write
shorter and more robust (not timing dependent) tests. This also means
that we can cover pretty nearly all the tests (protocols etc.) in
every case, even if the shipped image will be minimized.
Subsystems which are optional can now use a few new macros to configure
what they need see nng_sources_if, nng_headers_if, and nng_defines_if.
This goes a long way to making the distributed CMakefiles a lot simpler.
Additionally, tests for different parts of the tree can now be located
outside of the tests/ tree, so that they can be placed next to the code
that they are testing.
Beyond the enabling work, the work has only begun, but these changes
have resolved the most often failing tests for Darwin in the cloud.
Diffstat (limited to 'tests/aio.c')
| -rw-r--r-- | tests/aio.c | 377 |
1 files changed, 208 insertions, 169 deletions
diff --git a/tests/aio.c b/tests/aio.c index 9bb7de19..fdc68cb8 100644 --- a/tests/aio.c +++ b/tests/aio.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a @@ -14,13 +14,7 @@ #include <nng/protocol/pair1/pair.h> #include <nng/supplemental/util/platform.h> -#include "convey.h" -#include "stubs.h" - -#define APPENDSTR(m, s) nng_msg_append(m, s, strlen(s)) -#define CHECKSTR(m, s) \ - So(nng_msg_len(m) == strlen(s)); \ - So(memcmp(nng_msg_body(m), s, strlen(s)) == 0) +#include "acutest.h" void cbdone(void *p) @@ -41,164 +35,209 @@ cancelfn(nng_aio *aio, void *arg, int rv) nng_aio_finish(aio, rv); } -Main({ - Test("AIO operations", { - const char *addr = "inproc://aio"; - - Convey("Sleep works", { - nng_time start = 0; - nng_time end = 0; - nng_aio *saio; - So(nng_aio_alloc(&saio, sleepdone, &end) == 0); - start = nng_clock(); - nng_sleep_aio(200, saio); - nng_aio_wait(saio); - So(nng_aio_result(saio) == 0); - So(end != 0); - So((end - start) >= 200); - So((end - start) <= 1000); - So((nng_clock() - start) >= 200); - So((nng_clock() - start) <= 1000); - nng_aio_free(saio); - }); - - Convey("Sleep timeout works", { - nng_time start = 0; - nng_time end = 0; - nng_aio *saio; - So(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); - So(nng_aio_result(saio) == NNG_ETIMEDOUT); - So(end != 0); - So((end - start) >= 100); - So((end - start) <= 1000); - So((nng_clock() - start) >= 100); - So((nng_clock() - start) <= 1000); - nng_aio_free(saio); - }); - - Convey("Given a connected pair of sockets", { - nng_socket s1; - nng_socket s2; - nng_aio * txaio; - nng_aio * rxaio; - int txdone = 0; - int rxdone = 0; - nng_msg * m; - - So(nng_pair1_open(&s1) == 0); - So(nng_pair1_open(&s2) == 0); - - So(nng_listen(s1, addr, NULL, 0) == 0); - So(nng_dial(s2, addr, NULL, 0) == 0); - - So(nng_aio_alloc(&rxaio, cbdone, &rxdone) == 0); - So(nng_aio_alloc(&txaio, cbdone, &txdone) == 0); - - Reset({ - nng_aio_free(rxaio); - nng_aio_free(txaio); - nng_close(s1); - nng_close(s2); - }); - - nng_aio_set_timeout(rxaio, 100); - nng_aio_set_timeout(txaio, 100); - - So(nng_msg_alloc(&m, 0) == 0); - APPENDSTR(m, "hello"); - - nng_recv_aio(s2, rxaio); - - nng_aio_set_msg(txaio, m); - nng_send_aio(s1, txaio); - - nng_aio_wait(txaio); - nng_aio_wait(rxaio); - - So(nng_aio_result(rxaio) == 0); - So(nng_aio_result(txaio) == 0); - - So((m = nng_aio_get_msg(rxaio)) != NULL); - CHECKSTR(m, "hello"); - - nng_msg_free(m); - - So(rxdone == 1); - So(txdone == 1); - }); - - Convey("Failure modes work", { - nng_socket s; - nng_aio * a; - int done = 0; - - So(nng_pair1_open(&s) == 0); - - So(nng_aio_alloc(&a, cbdone, &done) == 0); - - Reset({ - nng_aio_free(a); - nng_close(s); - }); - - Convey("Explicit timeout works", { - nng_aio_set_timeout(a, 40); - nng_recv_aio(s, a); - nng_aio_wait(a); - So(done == 1); - So(nng_aio_result(a) == NNG_ETIMEDOUT); - }); - Convey("Default timeout works", { - So(nng_setopt_ms(s, NNG_OPT_RECVTIMEO, 40) == - 0); - nng_recv_aio(s, a); - nng_aio_wait(a); - So(done == 1); - So(nng_aio_result(a) == NNG_ETIMEDOUT); - }); - Convey("Zero timeout works", { - nng_aio_set_timeout(a, NNG_DURATION_ZERO); - nng_recv_aio(s, a); - nng_aio_wait(a); - So(done == 1); - So(nng_aio_result(a) == NNG_ETIMEDOUT); - }); - Convey("Cancellation works", { - nng_aio_set_timeout(a, NNG_DURATION_INFINITE); - nng_recv_aio(s, a); - nng_aio_cancel(a); - nng_aio_wait(a); - So(done == 1); - So(nng_aio_result(a) == NNG_ECANCELED); - }) - }); - - Convey("We cannot set insane IOVs", { - nng_aio *aio; - nng_iov iov; - - So(nng_aio_alloc(&aio, NULL, NULL) == 0); - So(nng_aio_set_iov(aio, 1024, &iov) == NNG_EINVAL); - nng_aio_free(aio); - }); - - Convey("Provider cancellation works", { - nng_aio *aio; - int rv = 0; - // We fake an empty provider that does not do anything. - So(nng_aio_alloc(&aio, NULL, NULL) == 0); - So(nng_aio_begin(aio) == true); - nng_aio_defer(aio, cancelfn, &rv); - nng_aio_cancel(aio); - nng_aio_wait(aio); - So(rv == NNG_ECANCELED); - nng_aio_free(aio); - }); - }); - - nng_fini(); -}) +void +test_sleep(void) +{ + nng_time start = 0; + nng_time end = 0; + nng_aio *saio; + + TEST_CHECK(nng_aio_alloc(&saio, sleepdone, &end) == 0); + start = nng_clock(); + nng_sleep_aio(200, saio); + nng_aio_wait(saio); + TEST_CHECK(nng_aio_result(saio) == 0); + TEST_CHECK(end != 0); + TEST_CHECK((end - start) >= 200); + TEST_CHECK((end - start) <= 1000); + TEST_CHECK((nng_clock() - start) >= 200); + TEST_CHECK((nng_clock() - start) <= 1000); + nng_aio_free(saio); +} + +void +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_CHECK(end != 0); + TEST_CHECK((end - start) >= 100); + TEST_CHECK((end - start) <= 1000); + TEST_CHECK((nng_clock() - start) >= 100); + TEST_CHECK((nng_clock() - start) <= 1000); + nng_aio_free(saio); +} + +void +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); + nng_aio_free(aio); +} + +void +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_CHECK(nng_aio_begin(aio) == true); + nng_aio_defer(aio, cancelfn, &rv); + nng_aio_cancel(aio); + nng_aio_wait(aio); + TEST_CHECK(rv == NNG_ECANCELED); + nng_aio_free(aio); +} + +void +test_consumer_cancel(void) +{ + nng_aio * a; + nng_socket s1; + int done = 0; + + TEST_CHECK(nng_pair1_open(&s1) == 0); + TEST_CHECK(nng_aio_alloc(&a, cbdone, &done) == 0); + + nng_aio_set_timeout(a, NNG_DURATION_INFINITE); + nng_recv_aio(s1, a); + nng_aio_cancel(a); + nng_aio_wait(a); + TEST_CHECK(done == 1); + TEST_CHECK(nng_aio_result(a) == NNG_ECANCELED); + + nng_aio_free(a); + TEST_CHECK(nng_close(s1) == 0); +} + +void +test_traffic(void) +{ + nng_socket s1; + nng_socket s2; + nng_aio * txaio; + nng_aio * rxaio; + int txdone = 0; + int rxdone = 0; + nng_msg * m; + char * addr = "inproc://traffic"; + + TEST_CHECK(nng_pair1_open(&s1) == 0); + TEST_CHECK(nng_pair1_open(&s2) == 0); + + TEST_CHECK(nng_listen(s1, addr, NULL, 0) == 0); + TEST_CHECK(nng_dial(s2, addr, NULL, 0) == 0); + + TEST_CHECK(nng_aio_alloc(&rxaio, cbdone, &rxdone) == 0); + TEST_CHECK(nng_aio_alloc(&txaio, cbdone, &txdone) == 0); + + 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); + + nng_recv_aio(s2, rxaio); + + nng_aio_set_msg(txaio, m); + nng_send_aio(s1, txaio); + + nng_aio_wait(txaio); + nng_aio_wait(rxaio); + + TEST_CHECK(nng_aio_result(rxaio) == 0); + TEST_CHECK(nng_aio_result(txaio) == 0); + + TEST_CHECK((m = nng_aio_get_msg(rxaio)) != NULL); + TEST_CHECK(nng_msg_len(m) == strlen("hello")); + TEST_CHECK(memcmp(nng_msg_body(m), "hello", strlen("hello")) == 0); + + nng_msg_free(m); + + TEST_CHECK(rxdone == 1); + TEST_CHECK(txdone == 1); + + nng_aio_free(rxaio); + nng_aio_free(txaio); + TEST_CHECK(nng_close(s1) == 0); + TEST_CHECK(nng_close(s2) == 0); +} + +void +test_explicit_timeout(void) +{ + nng_socket s; + nng_aio * a; + int done = 0; + + TEST_CHECK(nng_pair1_open(&s) == 0); + TEST_CHECK(nng_aio_alloc(&a, cbdone, &done) == 0); + 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); + nng_aio_free(a); + TEST_CHECK(nng_close(s) == 0); +} + +void +test_inherited_timeout(void) +{ + nng_socket s; + 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); + nng_recv_aio(s, a); + nng_aio_wait(a); + TEST_CHECK(done == 1); + TEST_CHECK(nng_aio_result(a) == NNG_ETIMEDOUT); + nng_aio_free(a); + TEST_CHECK(nng_close(s) == 0); +} + +void +test_zero_timeout(void) +{ + nng_socket s; + nng_aio * a; + int done = 0; + + TEST_CHECK(nng_pair1_open(&s) == 0); + TEST_CHECK(nng_aio_alloc(&a, cbdone, &done) == 0); + 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); + nng_aio_free(a); + TEST_CHECK(nng_close(s) == 0); +} + +TEST_LIST = { + { "sleep", test_sleep }, + { "sleep timeout", test_sleep_timeout }, + { "insane niov", test_insane_niov }, + { "provider cancel", test_provider_cancel }, + { "consumer cancel", test_consumer_cancel }, + { "traffic", test_traffic }, + { "explicit timeout", test_explicit_timeout }, + { "inherited timeout", test_inherited_timeout }, + { "zero timeout", test_zero_timeout }, + { NULL, NULL }, +};
\ No newline at end of file |
