From 9cbdeda1d0a9074bd65da2aaf9c87b79545a1590 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 25 Oct 2017 15:00:52 -0700 Subject: fixes #45 expose aio to applications While here we added a test for the aio stuff, and cleaned up some dead code for the old fd notifications. There were a few improvements to shorten & clean code elsewhere, such as short-circuiting task wait when the task has no callback. The legacy sendmsg() and recvmsg() APIs are still in the socket core until we convert the device code to use the aios. --- tests/CMakeLists.txt | 1 + tests/aio.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 tests/aio.c (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 456f67e3..7cb4786f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -104,6 +104,7 @@ else () endmacro (add_nng_cpp_test) endif () +add_nng_test(aio 5) add_nng_test(bus 5) add_nng_test(files 5) add_nng_test(idhash 5) diff --git a/tests/aio.c b/tests/aio.c new file mode 100644 index 00000000..2a31319d --- /dev/null +++ b/tests/aio.c @@ -0,0 +1,131 @@ +// +// Copyright 2017 Garrett D'Amore +// Copyright 2017 Capitar IT Group BV +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include "convey.h" +#include "nng.h" + +#include + +#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) + +void +cbdone(void *p) +{ + (*(int *) p)++; +} + +Main({ + + Test("AIO operations", { + const char *addr = "inproc://aio"; + + 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(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); + }) + + }); + }); + + nng_fini(); +}) -- cgit v1.2.3-70-g09d2