aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-09-05 14:08:04 -0700
committerGarrett D'Amore <garrett@damore.org>2017-09-05 14:08:04 -0700
commit5a6c4003663955744385ea1c7b36e0b908ffe2d1 (patch)
treef56be683b5a899f137317e142eb22b542a679718
parent8d67f64a975db7439bf42baf0d0835a32ce88069 (diff)
downloadnng-5a6c4003663955744385ea1c7b36e0b908ffe2d1.tar.gz
nng-5a6c4003663955744385ea1c7b36e0b908ffe2d1.tar.bz2
nng-5a6c4003663955744385ea1c7b36e0b908ffe2d1.zip
Add test for reconnection of disconnected client.
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/reconnect.c80
2 files changed, 81 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 357ea402..c6f6872c 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -92,6 +92,7 @@ add_nng_test(reqrep 5)
add_nng_test(pipeline 5)
add_nng_test(pollfd 5)
add_nng_test(pubsub 5)
+add_nng_test(reconnect 5)
add_nng_test(resolv 10)
add_nng_test(sock 5)
add_nng_test(survey 5)
diff --git a/tests/reconnect.c b/tests/reconnect.c
new file mode 100644
index 00000000..292f5d90
--- /dev/null
+++ b/tests/reconnect.c
@@ -0,0 +1,80 @@
+//
+// 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)
+
+TestMain("Reconnect works", {
+ atexit(nng_fini);
+ const char *addr = "inproc://test";
+ Convey("We can create a pipeline", {
+ nng_socket push;
+ nng_socket pull;
+
+ So(nng_push_open(&push) == 0);
+ So(nng_pull_open(&pull) == 0);
+
+ Reset({
+ nng_close(push);
+ nng_close(pull);
+ });
+
+ So(nng_setopt_usec(pull, nng_optid_reconnmint, 10000) == 0);
+ So(nng_setopt_usec(pull, nng_optid_reconnmaxt, 10000) == 0);
+
+ Convey("Dialing before listening works", {
+ So(nng_dial(push, addr, NULL, NNG_FLAG_NONBLOCK) == 0);
+ nng_usleep(100000);
+ So(nng_listen(pull, addr, NULL, 0) == 0);
+
+ Convey("We can send a frame", {
+ nng_msg *msg;
+
+ nng_usleep(100000);
+ So(nng_msg_alloc(&msg, 0) == 0);
+ APPENDSTR(msg, "hello");
+ So(nng_sendmsg(push, msg, 0) == 0);
+ msg = NULL;
+ So(nng_recvmsg(pull, &msg, 0) == 0);
+ So(msg != NULL);
+ CHECKSTR(msg, "hello");
+ nng_msg_free(msg);
+ });
+ });
+ Convey("Reconnection works", {
+ nng_listener l;
+ So(nng_dial(push, addr, NULL, NNG_FLAG_NONBLOCK) == 0);
+ So(nng_listen(pull, addr, &l, 0) == 0);
+ nng_usleep(100000);
+ nng_listener_close(l);
+ So(nng_listen(pull, addr, NULL, 0) == 0);
+
+ Convey("They still exchange frames", {
+ nng_msg *msg;
+
+ nng_usleep(100000);
+ So(nng_msg_alloc(&msg, 0) == 0);
+ APPENDSTR(msg, "hello");
+ So(nng_sendmsg(push, msg, 0) == 0);
+ msg = NULL;
+ So(nng_recvmsg(pull, &msg, 0) == 0);
+ So(msg != NULL);
+ CHECKSTR(msg, "hello");
+ nng_msg_free(msg);
+ });
+ });
+ });
+});