aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-10-25 15:00:52 -0700
committerGarrett D'Amore <garrett@damore.org>2017-10-25 18:29:47 -0700
commit9cbdeda1d0a9074bd65da2aaf9c87b79545a1590 (patch)
tree98254532f75a58cde92c837b4829bd2b3982db7a /tests
parentb28838f5cf3c5fed494d2684422099d26e8ab293 (diff)
downloadnng-9cbdeda1d0a9074bd65da2aaf9c87b79545a1590.tar.gz
nng-9cbdeda1d0a9074bd65da2aaf9c87b79545a1590.tar.bz2
nng-9cbdeda1d0a9074bd65da2aaf9c87b79545a1590.zip
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.
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/aio.c131
2 files changed, 132 insertions, 0 deletions
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 <garrett@damore.org>
+// Copyright 2017 Capitar IT Group BV <info@capitar.com>
+//
+// 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 <string.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)
+
+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();
+})