aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-04-27 10:36:24 -0700
committerGarrett D'Amore <garrett@damore.org>2018-04-28 20:43:02 -0700
commit605f53bdc1dcfbe980c37300b4c5bf0335f8a8ee (patch)
treedc0ba32faf859064ba992afbb85d52460559c38a /tests
parentfd06aba05381055ab56e1ec81d56055b66462f0b (diff)
downloadnng-605f53bdc1dcfbe980c37300b4c5bf0335f8a8ee.tar.gz
nng-605f53bdc1dcfbe980c37300b4c5bf0335f8a8ee.tar.bz2
nng-605f53bdc1dcfbe980c37300b4c5bf0335f8a8ee.zip
Add nonblocking test (test case for bug 346).
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/nonblock.c123
2 files changed, 124 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index d658dd65..6db01e82 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -139,6 +139,7 @@ add_nng_test(ipc 5 NNG_TRANSPORT_IPC)
add_nng_test(list 5 ON)
add_nng_test(message 5 ON)
add_nng_test(multistress 60 ON)
+add_nng_test(nonblock 60 ON)
add_nng_test(options 5 ON)
add_nng_test(platform 5 ON)
add_nng_test(pollfd 5 ON)
diff --git a/tests/nonblock.c b/tests/nonblock.c
new file mode 100644
index 00000000..1ad3c2f8
--- /dev/null
+++ b/tests/nonblock.c
@@ -0,0 +1,123 @@
+//
+// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 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 "nng.h"
+#include "protocol/reqrep0/rep.h"
+#include "protocol/reqrep0/req.h"
+#include "supplemental/util/platform.h"
+
+#include "convey.h"
+#include "stubs.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef _WIN32
+#include <sys/select.h>
+#include <sys/time.h>
+#endif
+
+const char *addr = "inproc://bug346";
+
+void
+repthr(void *arg)
+{
+ nng_socket rep = *(nng_socket *) arg;
+ nng_listener l;
+ int ifd;
+ PLATFD fd;
+
+ nng_listen(rep, addr, &l, NNG_FLAG_NONBLOCK);
+
+ nng_getopt_int(rep, NNG_OPT_RECVFD, &ifd);
+ fd = ifd;
+
+ for (;;) {
+ fd_set fset;
+ struct timeval tmo;
+ char * msgbuf;
+ size_t msglen;
+
+ FD_ZERO(&fset);
+ FD_SET(fd, &fset);
+
+ tmo.tv_sec = 0;
+ tmo.tv_usec = 20 * 1000; // 20 msec
+
+ select(1, &fset, NULL, NULL, &tmo);
+
+ for (;;) {
+ int rv;
+ rv = nng_recv(rep, &msgbuf, &msglen,
+ NNG_FLAG_NONBLOCK | NNG_FLAG_ALLOC);
+ if (rv != 0) {
+ return;
+ }
+ nng_free(msgbuf, msglen);
+ int ok = 0;
+ rv = nng_send(rep, &ok, 4, NNG_FLAG_NONBLOCK);
+ if (rv == NNG_ECLOSED) {
+ return;
+ }
+ }
+ }
+}
+
+void
+reqthr(void *arg)
+{
+ nng_socket req = *(nng_socket *) arg;
+
+ nng_dial(req, addr, NULL, NNG_FLAG_NONBLOCK);
+
+ int query = 0;
+ // We just keep pounding out requests, no wait for response.
+ for (;;) {
+ int rv;
+ rv = nng_send(req, &query, sizeof(query), 0);
+ if (rv == NNG_ECLOSED) {
+ return;
+ }
+ nng_msleep(50);
+ }
+}
+
+#define NCLIENTS 10
+nng_socket reqs[NCLIENTS];
+nng_socket rep;
+
+TestMain("Nonblocking Works", {
+
+ atexit(nng_fini);
+
+ Convey("Running for 15 sec", {
+
+ nng_thread *server;
+ nng_thread *clients[NCLIENTS];
+
+ So(nng_rep0_open(&rep) == 0);
+ for (int i = 0; i < NCLIENTS; i++) {
+ So(nng_req0_open(&reqs[i]) == 0);
+ }
+
+ nng_thread_create(&server, repthr, &rep);
+ for (int i = 0; i < NCLIENTS; i++) {
+ nng_thread_create(&clients[i], reqthr, &reqs[i]);
+ }
+
+ nng_msleep(15000);
+ nng_close(rep);
+ nng_thread_destroy(server);
+ for (int i = 0; i < NCLIENTS; i++) {
+ nng_close(reqs[i]);
+ nng_thread_destroy(clients[i]);
+ }
+ });
+})