aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
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();
+})