summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2015-11-15 17:19:31 -0800
committerGarrett D'Amore <garrett@damore.org>2015-11-16 15:56:38 -0800
commit10a567c89e564def7f5ae01f8c3bb1dab2acfed5 (patch)
treedd8d54aa6285c789b80cfe0572a390c5355a7e3e /tests
parentbe0b03df6cffa3674245f284edaa404790a20c29 (diff)
downloadnanomsg-10a567c89e564def7f5ae01f8c3bb1dab2acfed5.tar.gz
fixes #65 nn_close should abort blocking calls on socket
fixes #524 Sockets array is not thread-safe fixes #519 How to close a device without closing everything. fixes #523 Want a test to verify nn_close() aborts nn_recv() Portions contributed by Jack R. Dunaway <jack@wirebirdlabs.com>.
Diffstat (limited to 'tests')
-rw-r--r--tests/async_shutdown.c76
-rw-r--r--tests/device.c2
-rw-r--r--tests/device4.c2
-rw-r--r--tests/term.c1
-rw-r--r--tests/testutil.h2
5 files changed, 79 insertions, 4 deletions
diff --git a/tests/async_shutdown.c b/tests/async_shutdown.c
new file mode 100644
index 0000000..96cda89
--- /dev/null
+++ b/tests/async_shutdown.c
@@ -0,0 +1,76 @@
+/*
+ Copyright (c) 2012 Martin Sustrik All rights reserved.
+ Copyright (c) 2015 Jack R. Dunaway. All rights reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom
+ the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ IN THE SOFTWARE.
+*/
+
+#include "../src/nn.h"
+#include "../src/pair.h"
+#include "../src/pubsub.h"
+#include "../src/pipeline.h"
+#include "../src/tcp.h"
+
+#include "testutil.h"
+#include "../src/utils/attr.h"
+#include "../src/utils/thread.c"
+#include "../src/utils/atomic.c"
+
+/* Test condition of closing sockets that are blocking in another thread. */
+
+#define TEST_LOOPS 10
+#define SOCKET_ADDRESS "tcp://127.0.0.1:5557"
+
+struct nn_atomic active;
+
+static void routine (NN_UNUSED void *arg)
+{
+ int s;
+ int rc;
+ int msg;
+
+ nn_assert (arg);
+
+ s = *((int *) arg);
+
+ /* We don't expect to actually receive a message here;
+ therefore, the datatype of 'msg' is irrelevant. */
+ rc = nn_recv (s, &msg, sizeof(msg), 0);
+
+ errno_assert (nn_errno () == EBADF);
+}
+
+int main ()
+{
+ int sb;
+ int i;
+ struct nn_thread thread;
+
+ for (i = 0; i != TEST_LOOPS; ++i) {
+ sb = test_socket (AF_SP, NN_PULL);
+ test_bind (sb, SOCKET_ADDRESS);
+ nn_sleep (100);
+ nn_thread_init (&thread, routine, &sb);
+ nn_sleep (100);
+ test_close (sb);
+ nn_thread_term (&thread);
+ }
+
+ return 0;
+}
diff --git a/tests/device.c b/tests/device.c
index 30de089..97983c0 100644
--- a/tests/device.c
+++ b/tests/device.c
@@ -51,7 +51,7 @@ void device1 (NN_UNUSED void *arg)
/* Run the device. */
rc = nn_device (deva, devb);
- nn_assert (rc < 0 && nn_errno () == ETERM);
+ nn_assert (rc < 0 && (nn_errno () == ETERM || nn_errno () == EBADF));
/* Clean up. */
test_close (devb);
diff --git a/tests/device4.c b/tests/device4.c
index 1abcd07..a1ecbd7 100644
--- a/tests/device4.c
+++ b/tests/device4.c
@@ -47,7 +47,7 @@ void device4 (NN_UNUSED void *arg)
/* Run the device. */
rc = nn_device (devf, devg);
- nn_assert (rc < 0 && nn_errno () == ETERM);
+ nn_assert (rc < 0 && (nn_errno () == ETERM || nn_errno () == EBADF));
/* Clean up. */
test_close (devg);
diff --git a/tests/term.c b/tests/term.c
index ced753e..eeaea0b 100644
--- a/tests/term.c
+++ b/tests/term.c
@@ -43,7 +43,6 @@ static void worker (NN_UNUSED void *arg)
/* Check that all subsequent operations fail in synchronous manner. */
rc = nn_recv (s, buf, sizeof (buf), 0);
nn_assert (rc == -1 && nn_errno () == ETERM);
-
test_close (s);
}
diff --git a/tests/testutil.h b/tests/testutil.h
index f4ce51a..d6c1108 100644
--- a/tests/testutil.h
+++ b/tests/testutil.h
@@ -117,7 +117,7 @@ static void NN_UNUSED test_close_impl (char *file, int line, int sock)
int rc;
rc = nn_close (sock);
- if (rc != 0) {
+ if ((rc != 0) && (errno != EBADF && errno != ETERM)) {
fprintf (stderr, "Failed to close socket: %s [%d] (%s:%d)\n",
nn_err_strerror (errno),
(int) errno, file, line);