From 5a6c4003663955744385ea1c7b36e0b908ffe2d1 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 5 Sep 2017 14:08:04 -0700 Subject: Add test for reconnection of disconnected client. --- tests/CMakeLists.txt | 1 + tests/reconnect.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/reconnect.c (limited to 'tests') 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 +// 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) + +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); + }); + }); + }); +}); -- cgit v1.2.3-70-g09d2