aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-01-12 16:11:19 -0800
committerGarrett D'Amore <garrett@damore.org>2020-01-12 23:03:06 -0800
commit7b4a0a996aa6ed3e8fbbd9fd0e28811725707605 (patch)
treec06211c9972d2b311116c8b4fee536896f38b394
parent4299a5b4edc59753a6ec857fabedadf1504c4243 (diff)
downloadnng-7b4a0a996aa6ed3e8fbbd9fd0e28811725707605.tar.gz
nng-7b4a0a996aa6ed3e8fbbd9fd0e28811725707605.tar.bz2
nng-7b4a0a996aa6ed3e8fbbd9fd0e28811725707605.zip
Add PUB/SUB test suite.
This gets near 100% coverage of the PUB/SUB protocols. The remaining uncovered bits will need to have a mock protocol that runs slower, so that we can inject both back pressure, and also so that we can inject "erroroneous" messages.
-rw-r--r--docs/man/nng_options.5.adoc5
-rw-r--r--src/protocol/pubsub0/CMakeLists.txt6
-rw-r--r--src/protocol/pubsub0/pub.c32
-rw-r--r--src/protocol/pubsub0/pub_test.c343
-rw-r--r--src/protocol/pubsub0/sub.c35
-rw-r--r--src/protocol/pubsub0/sub_test.c633
-rw-r--r--src/protocol/pubsub0/xsub.c42
-rw-r--r--src/protocol/pubsub0/xsub_test.c387
-rw-r--r--tests/pubsub.c7
-rw-r--r--tests/testutil.h22
10 files changed, 1421 insertions, 91 deletions
diff --git a/docs/man/nng_options.5.adoc b/docs/man/nng_options.5.adoc
index 278055c5..915096ec 100644
--- a/docs/man/nng_options.5.adoc
+++ b/docs/man/nng_options.5.adoc
@@ -1,6 +1,6 @@
= nng_options(5)
//
-// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2019 Devolutions <info@devolutions.net>
//
@@ -145,6 +145,9 @@ The option is irrelevant for listeners.
This is the depth of the socket's receive buffer as a number of messages.
Messages received by a transport may be buffered until the application
has accepted them for delivery.
+This value must be an integer between 0 and 8192, inclusive.
+NOTE: Not all protocols support buffering received messages.
+For example xref:nng_req.7.adoc[_req_] can only deal with a single reply at a time.
[[NNG_OPT_RECVFD]]
((`NNG_OPT_RECVFD`))::
diff --git a/src/protocol/pubsub0/CMakeLists.txt b/src/protocol/pubsub0/CMakeLists.txt
index 23874662..8687de4e 100644
--- a/src/protocol/pubsub0/CMakeLists.txt
+++ b/src/protocol/pubsub0/CMakeLists.txt
@@ -21,4 +21,8 @@ nng_defines_if(NNG_PROTO_PUB0 NNG_HAVE_PUB0)
nng_sources_if(NNG_PROTO_SUB0 sub.c xsub.c)
nng_headers_if(NNG_PROTO_SUB0 nng/protocol/pubsub0/sub.h)
-nng_defines_if(NNG_PROTO_SUB0 NNG_HAVE_SUB0) \ No newline at end of file
+nng_defines_if(NNG_PROTO_SUB0 NNG_HAVE_SUB0)
+
+nng_test(pub_test)
+nng_test(sub_test)
+nng_test(xsub_test)
diff --git a/src/protocol/pubsub0/pub.c b/src/protocol/pubsub0/pub.c
index 9b995c33..c6959148 100644
--- a/src/protocol/pubsub0/pub.c
+++ b/src/protocol/pubsub0/pub.c
@@ -9,6 +9,7 @@
//
#include <stdlib.h>
+#include <stdio.h>
#include <string.h>
#include "core/nng_impl.h"
@@ -90,11 +91,7 @@ pub0_sock_open(void *arg)
static void
pub0_sock_close(void *arg)
{
- pub0_sock *sock = arg;
-
- nni_mtx_lock(&sock->mtx);
- sock->closed = true;
- nni_mtx_unlock(&sock->mtx);
+ NNI_ARG_UNUSED(arg);
}
static void
@@ -186,15 +183,12 @@ pub0_pipe_recv_cb(void *arg)
{
pub0_pipe *p = arg;
- if (nni_aio_result(p->aio_recv) != 0) {
- nni_pipe_close(p->pipe);
- return;
+ // We should never receive a message -- the only valid reason for us to
+ // be here is on pipe close.
+ if (nni_aio_result(p->aio_recv) == 0) {
+ nni_msg_free(nni_aio_get_msg(p->aio_recv));
}
-
- // We should never get any messages. If we do we just dicard them.
- nni_msg_free(nni_aio_get_msg(p->aio_recv));
- nni_aio_set_msg(p->aio_recv, NULL);
- nni_pipe_recv(p->pipe, p->aio_recv);
+ nni_pipe_close(p->pipe);
}
static void
@@ -212,7 +206,7 @@ pub0_pipe_send_cb(void *arg)
}
nni_mtx_lock(&sock->mtx);
- if (sock->closed || p->closed) {
+ if (p->closed) {
nni_mtx_unlock(&sock->mtx);
return;
}
@@ -246,15 +240,7 @@ pub0_sock_send(void *arg, nni_aio *aio)
msg = nni_aio_get_msg(aio);
len = nni_msg_len(msg);
nni_mtx_lock(&sock->mtx);
- if (sock->closed) {
- nni_mtx_unlock(&sock->mtx);
- nni_aio_finish_error(aio, NNG_ECLOSED);
- return;
- }
NNI_LIST_FOREACH (&sock->pipes, p) {
- if (p->closed) {
- continue;
- }
if (p == nni_list_last(&sock->pipes)) {
dup = msg;
msg = NULL;
@@ -319,7 +305,7 @@ pub0_sock_set_sendbuf(void *arg, const void *buf, size_t sz, nni_type t)
// stop short. The others would likely fail for ENOMEM as
// well anyway. There is a weird effect here where the
// buffers may have been set for *some* of the pipes, but
- // we have no way to correct, or even report, partial failure.
+ // we have no way to correct partial failure.
if ((rv = nni_lmq_resize(&p->sendq, (size_t) val)) != 0) {
break;
}
diff --git a/src/protocol/pubsub0/pub_test.c b/src/protocol/pubsub0/pub_test.c
new file mode 100644
index 00000000..94d5c333
--- /dev/null
+++ b/src/protocol/pubsub0/pub_test.c
@@ -0,0 +1,343 @@
+//
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
+//
+// 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 <string.h>
+
+#include <nng/nng.h>
+#include <nng/protocol/pubsub0/pub.h>
+#include <nng/protocol/pubsub0/sub.h>
+
+#include <acutest.h>
+#include <testutil.h>
+
+#ifndef NNI_PROTO
+#define NNI_PROTO(x, y) (((x) << 4u) | (y))
+#endif
+
+static void
+test_pub_identity(void)
+{
+ nng_socket s;
+ int p;
+ char * n;
+
+ TEST_NNG_PASS(nng_pub0_open(&s));
+ TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PROTO, &p));
+ TEST_CHECK(p == NNI_PROTO(2u, 0u)); // 32
+ TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PEER, &p));
+ TEST_CHECK(p == NNI_PROTO(2u, 1u)); // 33
+ TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PROTONAME, &n));
+ TEST_CHECK(strcmp(n, "pub") == 0);
+ nng_strfree(n);
+ TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PEERNAME, &n));
+ TEST_CHECK(strcmp(n, "sub") == 0);
+ nng_strfree(n);
+ TEST_NNG_PASS(nng_close(s));
+}
+
+static void
+test_pub_cannot_recv(void)
+{
+ nng_socket pub;
+
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_FAIL(nng_recv(pub, "", 0, 0), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(pub));
+}
+
+static void
+test_pub_no_context(void)
+{
+ nng_socket pub;
+ nng_ctx ctx;
+
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_FAIL(nng_ctx_open(&ctx, pub), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(pub));
+}
+
+static void
+test_pub_not_readable(void)
+{
+ int fd;
+ nng_socket pub;
+
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_FAIL(nng_getopt_int(pub, NNG_OPT_RECVFD, &fd), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(pub));
+}
+
+static void
+test_pub_poll_writeable(void)
+{
+ int fd;
+ nng_socket pub;
+ nng_socket sub;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_getopt_int(pub, NNG_OPT_SENDFD, &fd));
+ TEST_CHECK(fd >= 0);
+
+ // Pub is *always* writeable
+ TEST_CHECK(testutil_pollfd(fd) == true);
+
+ // Even after connect (no message yet)
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+ TEST_CHECK(testutil_pollfd(fd) == true);
+
+ // But once we send messages, it is.
+ // We have to send a request, in order to send a reply.
+ TEST_NNG_SEND_STR(pub, "abc");
+ TEST_CHECK(testutil_pollfd(fd) == true);
+
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_pub_send_no_pipes(void)
+{
+ nng_socket pub;
+
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_SEND_STR(pub, "DROP1");
+ TEST_NNG_SEND_STR(pub, "DROP2");
+ TEST_NNG_PASS(nng_close(pub));
+}
+
+void
+test_pub_validate_peer(void)
+{
+ nng_socket s1, s2;
+ nng_stat * stats;
+ nng_stat * reject;
+ char addr[64];
+
+ testutil_scratch_addr("inproc", sizeof(addr), addr);
+
+ TEST_NNG_PASS(nng_pub0_open(&s1));
+ TEST_NNG_PASS(nng_pub0_open(&s2));
+
+ TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0));
+ TEST_NNG_PASS(nng_dial(s2, addr, NULL, NNG_FLAG_NONBLOCK));
+
+ testutil_sleep(100);
+ TEST_NNG_PASS(nng_stats_get(&stats));
+
+ TEST_CHECK(stats != NULL);
+ TEST_CHECK((reject = nng_stat_find_socket(stats, s1)) != NULL);
+ TEST_CHECK((reject = nng_stat_find(reject, "reject")) != NULL);
+
+ TEST_CHECK(nng_stat_type(reject) == NNG_STAT_COUNTER);
+ TEST_CHECK(nng_stat_value(reject) > 0);
+
+ TEST_NNG_PASS(nng_close(s1));
+ TEST_NNG_PASS(nng_close(s2));
+ nng_stats_free(stats);
+}
+
+static void
+test_pub_send_queued(void)
+{
+ nng_socket pub;
+ nng_socket sub;
+
+ // MB: What we really need is a mock so that we can send harder
+ // than we receive -- we need a way to apply back-pressure for this
+ // test to be really meaningful.
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, "", 0));
+ TEST_NNG_PASS(nng_setopt_int(pub, NNG_OPT_SENDBUF, 10));
+ TEST_NNG_PASS(nng_setopt_int(sub, NNG_OPT_RECVBUF, 10));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 1000));
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+ TEST_NNG_SEND_STR(pub, "first");
+ TEST_NNG_SEND_STR(pub, "second");
+ TEST_NNG_SEND_STR(pub, "three musketeers");
+ TEST_NNG_SEND_STR(pub, "four");
+ testutil_sleep(50);
+ TEST_NNG_RECV_STR(sub, "first");
+ TEST_NNG_RECV_STR(sub, "second");
+ TEST_NNG_RECV_STR(sub, "three musketeers");
+ TEST_NNG_RECV_STR(sub, "four");
+
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+static void
+test_sub_recv_ctx_closed(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nng_ctx_close(ctx);
+ nng_ctx_recv(ctx, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECLOSED);
+ nng_aio_free(aio);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_ctx_recv_aio_stopped(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+
+ nng_aio_stop(aio);
+ nng_ctx_recv(ctx, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECANCELED);
+ TEST_NNG_PASS(nng_ctx_close(ctx));
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_sub_close_context_recv(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nng_aio_set_timeout(aio, 1000);
+ nng_ctx_recv(ctx, aio);
+ TEST_NNG_PASS(nng_ctx_close(ctx));
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECLOSED);
+
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_sub_ctx_recv_nonblock(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+
+ nng_aio_set_timeout(aio, 0); // Instant timeout
+ nng_ctx_recv(ctx, aio);
+
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ETIMEDOUT);
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_sub_ctx_recv_cancel(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+
+ nng_aio_set_timeout(aio, 1000);
+ nng_ctx_recv(ctx, aio);
+ nng_aio_abort(aio, NNG_ECANCELED);
+
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECANCELED);
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_pub_send_buf_option(void)
+{
+ nng_socket pub;
+ int v;
+ bool b;
+ size_t sz;
+ const char *opt = NNG_OPT_SENDBUF;
+
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+
+ TEST_NNG_PASS(nng_setopt_int(pub, opt, 1));
+ TEST_NNG_FAIL(nng_setopt_int(pub, opt, 0), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_int(pub, opt, -1), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_int(pub, opt, 1000000), NNG_EINVAL);
+ TEST_NNG_PASS(nng_setopt_int(pub, opt, 3));
+ TEST_NNG_PASS(nng_getopt_int(pub, opt, &v));
+ TEST_CHECK(v == 3);
+ v = 0;
+ sz = sizeof(v);
+ TEST_NNG_PASS(nng_getopt(pub, opt, &v, &sz));
+ TEST_CHECK(v == 3);
+ TEST_CHECK(sz == sizeof(v));
+
+ TEST_NNG_FAIL(nng_setopt(pub, opt, "", 1), NNG_EINVAL);
+ sz = 1;
+ TEST_NNG_FAIL(nng_getopt(pub, opt, &v, &sz), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_bool(pub, opt, true), NNG_EBADTYPE);
+ TEST_NNG_FAIL(nng_getopt_bool(pub, opt, &b), NNG_EBADTYPE);
+
+ TEST_NNG_PASS(nng_close(pub));
+}
+
+static void
+test_pub_cooked(void)
+{
+ nng_socket s;
+ bool b;
+
+ TEST_NNG_PASS(nng_pub0_open(&s));
+ TEST_NNG_PASS(nng_getopt_bool(s, NNG_OPT_RAW, &b));
+ TEST_CHECK(!b);
+ TEST_NNG_FAIL(nng_setopt_bool(s, NNG_OPT_RAW, true), NNG_EREADONLY);
+ TEST_NNG_PASS(nng_close(s));
+
+ // raw pub only differs in the option setting
+ TEST_NNG_PASS(nng_pub0_open_raw(&s));
+ TEST_NNG_PASS(nng_getopt_bool(s, NNG_OPT_RAW, &b));
+ TEST_CHECK(b);
+ TEST_NNG_PASS(nng_close(s));
+}
+
+TEST_LIST = {
+ { "pub identity", test_pub_identity },
+ { "pub cannot recv", test_pub_cannot_recv },
+ { "put no context", test_pub_no_context },
+ { "pub not readable", test_pub_not_readable },
+ { "pub poll writeable", test_pub_poll_writeable },
+ { "pub validate peer", test_pub_validate_peer },
+ { "pub send queued", test_pub_send_queued },
+ { "pub send no pipes", test_pub_send_no_pipes },
+ { "sub recv ctx closed", test_sub_recv_ctx_closed },
+ { "sub recv aio ctx stopped", test_sub_ctx_recv_aio_stopped },
+ { "sub close context recv", test_sub_close_context_recv },
+ { "sub context recv nonblock", test_sub_ctx_recv_nonblock },
+ { "sub context recv cancel", test_sub_ctx_recv_cancel },
+ { "pub send buf option", test_pub_send_buf_option },
+ { "pub cooked", test_pub_cooked },
+ { NULL, NULL },
+};
diff --git a/src/protocol/pubsub0/sub.c b/src/protocol/pubsub0/sub.c
index c5b84313..b5dd7834 100644
--- a/src/protocol/pubsub0/sub.c
+++ b/src/protocol/pubsub0/sub.c
@@ -10,7 +10,6 @@
//
#include <stdbool.h>
-#include <stdlib.h>
#include <string.h>
#include "core/nng_impl.h"
@@ -55,7 +54,6 @@ struct sub0_ctx {
sub0_sock * sock;
nni_list topics; // TODO: Consider patricia trie
nni_list recv_queue; // can have multiple pending receives
- bool closed;
nni_lmq lmq;
bool prefer_new;
};
@@ -74,7 +72,7 @@ struct sub0_sock {
struct sub0_pipe {
nni_pipe * pipe;
sub0_sock *sub;
- nni_aio * aio_recv;
+ nni_aio aio_recv;
};
static void
@@ -103,12 +101,6 @@ sub0_ctx_recv(void *arg, nni_aio *aio)
nni_mtx_lock(&sock->lk);
- if (ctx->closed) {
- nni_mtx_unlock(&sock->lk);
- nni_aio_finish_error(aio, NNG_ECLOSED);
- return;
- }
-
if (nni_lmq_empty(&ctx->lmq)) {
int rv;
if ((rv = nni_aio_schedule(aio, sub0_ctx_cancel, ctx)) != 0) {
@@ -148,7 +140,6 @@ sub0_ctx_close(void *arg)
nni_aio * aio;
nni_mtx_lock(&sock->lk);
- ctx->closed = true;
while ((aio = nni_list_first(&ctx->recv_queue)) != NULL) {
nni_list_remove(&ctx->recv_queue, aio);
nni_aio_finish_error(aio, NNG_ECLOSED);
@@ -213,7 +204,7 @@ sub0_sock_fini(void *arg)
sub0_sock *sock = arg;
sub0_ctx_fini(&sock->master);
- nni_pollable_fini(&sock->readable);
+ nni_pollable_fini(&sock->readable);
nni_mtx_fini(&sock->lk);
}
@@ -257,7 +248,7 @@ sub0_pipe_stop(void *arg)
{
sub0_pipe *p = arg;
- nni_aio_stop(p->aio_recv);
+ nni_aio_stop(&p->aio_recv);
}
static void
@@ -265,19 +256,15 @@ sub0_pipe_fini(void *arg)
{
sub0_pipe *p = arg;
- nni_aio_free(p->aio_recv);
+ nni_aio_fini(&p->aio_recv);
}
static int
sub0_pipe_init(void *arg, nni_pipe *pipe, void *s)
{
sub0_pipe *p = arg;
- int rv;
- if ((rv = nni_aio_alloc(&p->aio_recv, sub0_recv_cb, p)) != 0) {
- sub0_pipe_fini(p);
- return (rv);
- }
+ nni_aio_init(&p->aio_recv, sub0_recv_cb, p);
p->pipe = pipe;
p->sub = s;
@@ -294,7 +281,7 @@ sub0_pipe_start(void *arg)
return (NNG_EPROTO);
}
- nni_pipe_recv(p->pipe, p->aio_recv);
+ nni_pipe_recv(p->pipe, &p->aio_recv);
return (0);
}
@@ -303,7 +290,7 @@ sub0_pipe_close(void *arg)
{
sub0_pipe *p = arg;
- nni_aio_close(p->aio_recv);
+ nni_aio_close(&p->aio_recv);
}
static bool
@@ -338,15 +325,15 @@ sub0_recv_cb(void *arg)
nng_aio * aio;
bool submatch;
- if (nni_aio_result(p->aio_recv) != 0) {
+ if (nni_aio_result(&p->aio_recv) != 0) {
nni_pipe_close(p->pipe);
return;
}
nni_aio_list_init(&finish);
- msg = nni_aio_get_msg(p->aio_recv);
- nni_aio_set_msg(p->aio_recv, NULL);
+ msg = nni_aio_get_msg(&p->aio_recv);
+ nni_aio_set_msg(&p->aio_recv, NULL);
nni_msg_set_pipe(msg, nni_pipe_id(p->pipe));
body = nni_msg_body(msg);
@@ -415,7 +402,7 @@ sub0_recv_cb(void *arg)
nni_pollable_raise(&sock->readable);
}
- nni_pipe_recv(p->pipe, p->aio_recv);
+ nni_pipe_recv(p->pipe, &p->aio_recv);
}
static int
diff --git a/src/protocol/pubsub0/sub_test.c b/src/protocol/pubsub0/sub_test.c
new file mode 100644
index 00000000..4690ad23
--- /dev/null
+++ b/src/protocol/pubsub0/sub_test.c
@@ -0,0 +1,633 @@
+//
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
+//
+// 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 <string.h>
+
+#include <nng/nng.h>
+#include <nng/protocol/pubsub0/pub.h>
+#include <nng/protocol/pubsub0/sub.h>
+
+#include <acutest.h>
+#include <testutil.h>
+
+#ifndef NNI_PROTO
+#define NNI_PROTO(x, y) (((x) << 4u) | (y))
+#endif
+
+static void
+test_sub_identity(void)
+{
+ nng_socket s;
+ int p;
+ char * n;
+
+ TEST_NNG_PASS(nng_sub0_open(&s));
+ TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PROTO, &p));
+ TEST_CHECK(p == NNI_PROTO(2u, 1u)); // 33
+ TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PEER, &p));
+ TEST_CHECK(p == NNI_PROTO(2u, 0u)); // 32
+ TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PROTONAME, &n));
+ TEST_CHECK(strcmp(n, "sub") == 0);
+ nng_strfree(n);
+ TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PEERNAME, &n));
+ TEST_CHECK(strcmp(n, "pub") == 0);
+ nng_strfree(n);
+ TEST_NNG_PASS(nng_close(s));
+}
+
+static void
+test_sub_cannot_send(void)
+{
+ nng_socket sub;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_FAIL(nng_send(sub, "", 0, 0), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_context_cannot_send(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_msg * m;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_msg_alloc(&m, 0));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nng_aio_set_msg(aio, m);
+ nng_aio_set_timeout(aio, 1000);
+ nng_ctx_send(ctx, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_ctx_close(ctx));
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+ nng_msg_free(m);
+}
+
+static void
+test_sub_not_writeable(void)
+{
+ int fd;
+ nng_socket sub;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_FAIL(nng_getopt_int(sub, NNG_OPT_SENDFD, &fd), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_poll_readable(void)
+{
+ int fd;
+ nng_socket pub;
+ nng_socket sub;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, "", 0));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 1000));
+ TEST_NNG_PASS(nng_getopt_int(sub, NNG_OPT_RECVFD, &fd));
+ TEST_CHECK(fd >= 0);
+
+ // Not readable if not connected!
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ // Even after connect (no message yet)
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ // But once we send messages, it is.
+ // We have to send a request, in order to send a reply.
+ TEST_NNG_SEND_STR(pub, "abc");
+ testutil_sleep(200);
+
+ TEST_CHECK(testutil_pollfd(fd) == true);
+
+ // and receiving makes it no longer ready
+ TEST_NNG_RECV_STR(sub, "abc");
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_recv_late(void)
+{
+ int fd;
+ nng_socket pub;
+ nng_socket sub;
+ nng_aio * aio;
+ nng_msg * msg;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, "", 0));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 1000));
+ TEST_NNG_PASS(nng_getopt_int(sub, NNG_OPT_RECVFD, &fd));
+ TEST_CHECK(fd >= 0);
+
+ // Not readable if not connected!
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ // Even after connect (no message yet)
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ nng_recv_aio(sub, aio);
+
+ // But once we send messages, it is.
+ // We have to send a request, in order to send a reply.
+ TEST_NNG_SEND_STR(pub, "abc");
+ testutil_sleep(200);
+
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ msg = nng_aio_get_msg(aio);
+ nng_aio_set_msg(aio, NULL);
+ TEST_CHECK(nng_msg_len(msg) == 4);
+ TEST_CHECK(strcmp(nng_msg_body(msg), "abc") == 0);
+
+ nng_msg_free(msg);
+ nng_aio_free(aio);
+
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+void
+test_sub_context_no_poll(void)
+{
+ int fd;
+ nng_socket sub;
+ nng_ctx ctx;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_FAIL(
+ nng_ctx_getopt_int(ctx, NNG_OPT_SENDFD, &fd), NNG_ENOTSUP);
+ TEST_NNG_FAIL(
+ nng_ctx_getopt_int(ctx, NNG_OPT_RECVFD, &fd), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_ctx_close(ctx));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+void
+test_sub_validate_peer(void)
+{
+ nng_socket s1, s2;
+ nng_stat * stats;
+ nng_stat * reject;
+ char addr[64];
+
+ testutil_scratch_addr("inproc", sizeof(addr), addr);
+
+ TEST_NNG_PASS(nng_sub0_open(&s1));
+ TEST_NNG_PASS(nng_sub0_open(&s2));
+
+ TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0));
+ TEST_NNG_PASS(nng_dial(s2, addr, NULL, NNG_FLAG_NONBLOCK));
+
+ testutil_sleep(100);
+ TEST_NNG_PASS(nng_stats_get(&stats));
+
+ TEST_CHECK(stats != NULL);
+ TEST_CHECK((reject = nng_stat_find_socket(stats, s1)) != NULL);
+ TEST_CHECK((reject = nng_stat_find(reject, "reject")) != NULL);
+
+ TEST_CHECK(nng_stat_type(reject) == NNG_STAT_COUNTER);
+ TEST_CHECK(nng_stat_value(reject) > 0);
+
+ TEST_NNG_PASS(nng_close(s1));
+ TEST_NNG_PASS(nng_close(s2));
+ nng_stats_free(stats);
+}
+
+static void
+test_sub_recv_ctx_closed(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nng_ctx_close(ctx);
+ nng_ctx_recv(ctx, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECLOSED);
+ nng_aio_free(aio);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_ctx_recv_aio_stopped(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+
+ nng_aio_stop(aio);
+ nng_ctx_recv(ctx, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECANCELED);
+ TEST_NNG_PASS(nng_ctx_close(ctx));
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_sub_close_context_recv(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nng_aio_set_timeout(aio, 1000);
+ nng_ctx_recv(ctx, aio);
+ TEST_NNG_PASS(nng_ctx_close(ctx));
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECLOSED);
+
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_sub_ctx_recv_nonblock(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+
+ nng_aio_set_timeout(aio, 0); // Instant timeout
+ nng_ctx_recv(ctx, aio);
+
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ETIMEDOUT);
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_sub_ctx_recv_cancel(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_ctx_open(&ctx, sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+
+ nng_aio_set_timeout(aio, 1000);
+ nng_ctx_recv(ctx, aio);
+ nng_aio_abort(aio, NNG_ECANCELED);
+
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECANCELED);
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_sub_recv_buf_option(void)
+{
+ nng_socket sub;
+ int v;
+ bool b;
+ size_t sz;
+ const char *opt = NNG_OPT_RECVBUF;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+
+ TEST_NNG_PASS(nng_setopt_int(sub, opt, 1));
+ TEST_NNG_FAIL(nng_setopt_int(sub, opt, 0), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_int(sub, opt, -1), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_int(sub, opt, 1000000), NNG_EINVAL);
+ TEST_NNG_PASS(nng_setopt_int(sub, opt, 3));
+ TEST_NNG_PASS(nng_getopt_int(sub, opt, &v));
+ TEST_CHECK(v == 3);
+ v = 0;
+ sz = sizeof(v);
+ TEST_NNG_PASS(nng_getopt(sub, opt, &v, &sz));
+ TEST_CHECK(v == 3);
+ TEST_CHECK(sz == sizeof(v));
+
+ TEST_NNG_FAIL(nng_setopt(sub, opt, "", 1), NNG_EINVAL);
+ sz = 1;
+ TEST_NNG_FAIL(nng_getopt(sub, opt, &v, &sz), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_bool(sub, opt, true), NNG_EBADTYPE);
+ TEST_NNG_FAIL(nng_getopt_bool(sub, opt, &b), NNG_EBADTYPE);
+
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_subscribe_option(void)
+{
+ nng_socket sub;
+ size_t sz;
+ int v;
+ const char *opt = NNG_OPT_SUB_SUBSCRIBE;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+
+ TEST_NNG_PASS(nng_setopt(sub, opt, "abc", 3));
+ TEST_NNG_PASS(nng_setopt(sub, opt, "abc", 3)); // duplicate
+ TEST_NNG_PASS(nng_setopt_bool(sub, opt, false));
+ TEST_NNG_PASS(nng_setopt_int(sub, opt, 32));
+ sz = sizeof(v);
+ TEST_NNG_FAIL(nng_getopt(sub, opt, &v, &sz), NNG_EWRITEONLY);
+
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_unsubscribe_option(void)
+{
+ nng_socket sub;
+ size_t sz;
+ int v;
+ const char *opt1 = NNG_OPT_SUB_SUBSCRIBE;
+ const char *opt2 = NNG_OPT_SUB_UNSUBSCRIBE;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+
+ TEST_NNG_PASS(nng_setopt(sub, opt1, "abc", 3));
+ TEST_NNG_FAIL(nng_setopt(sub, opt2, "abcdef", 6), NNG_ENOENT);
+ TEST_NNG_PASS(nng_setopt(sub, opt2, "abc", 3));
+ TEST_NNG_FAIL(nng_setopt(sub, opt2, "abc", 3), NNG_ENOENT);
+ TEST_NNG_PASS(nng_setopt_int(sub, opt1, 32));
+ TEST_NNG_FAIL(nng_setopt_int(sub, opt2, 23), NNG_ENOENT);
+ TEST_NNG_PASS(nng_setopt_int(sub, opt2, 32));
+ sz = sizeof(v);
+ TEST_NNG_FAIL(nng_getopt(sub, opt2, &v, &sz), NNG_EWRITEONLY);
+
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_prefer_new_option(void)
+{
+ nng_socket sub;
+ bool b;
+ size_t sz;
+ const char *opt = NNG_OPT_SUB_PREFNEW;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+
+ TEST_NNG_PASS(nng_setopt_bool(sub, opt, true));
+ TEST_NNG_PASS(nng_setopt_bool(sub, opt, false));
+ TEST_NNG_PASS(nng_getopt_bool(sub, opt, &b));
+ TEST_CHECK(b == false);
+ sz = sizeof(b);
+ b = true;
+ TEST_NNG_PASS(nng_getopt(sub, opt, &b, &sz));
+ TEST_CHECK(b == false);
+ TEST_CHECK(sz == sizeof(bool));
+
+ TEST_NNG_FAIL(nng_setopt(sub, opt, "abc", 3), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_int(sub, opt, 1), NNG_EBADTYPE);
+
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+void
+test_sub_drop_new(void)
+{
+ nng_socket sub;
+ nng_socket pub;
+ nng_msg * msg;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_setopt_int(sub, NNG_OPT_RECVBUF, 2));
+ TEST_NNG_PASS(nng_setopt_bool(sub, NNG_OPT_SUB_PREFNEW, false));
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, NULL, 0));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 200));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 1000));
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+ TEST_NNG_SEND_STR(pub, "one");
+ TEST_NNG_SEND_STR(pub, "two");
+ TEST_NNG_SEND_STR(pub, "three");
+ testutil_sleep(100);
+ TEST_NNG_RECV_STR(sub, "one");
+ TEST_NNG_RECV_STR(sub, "two");
+ TEST_NNG_FAIL(nng_recvmsg(sub, &msg, 0), NNG_ETIMEDOUT);
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+void
+test_sub_drop_old(void)
+{
+ nng_socket sub;
+ nng_socket pub;
+ nng_msg * msg;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_setopt_int(sub, NNG_OPT_RECVBUF, 2));
+ TEST_NNG_PASS(nng_setopt_bool(sub, NNG_OPT_SUB_PREFNEW, true));
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, NULL, 0));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 200));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 1000));
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+ TEST_NNG_SEND_STR(pub, "one");
+ TEST_NNG_SEND_STR(pub, "two");
+ TEST_NNG_SEND_STR(pub, "three");
+ testutil_sleep(100);
+ TEST_NNG_RECV_STR(sub, "two");
+ TEST_NNG_RECV_STR(sub, "three");
+ TEST_NNG_FAIL(nng_recvmsg(sub, &msg, 0), NNG_ETIMEDOUT);
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_sub_filter(void)
+{
+ nng_socket sub;
+ nng_socket pub;
+ char buf[32];
+ size_t sz;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_int(sub, NNG_OPT_RECVBUF, 10));
+
+ // Set up some default filters
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, "abc", 3));
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, "def", 3));
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, "ghi", 3));
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, "jkl", 3));
+
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+
+ TEST_NNG_PASS(nng_send(pub, "def", 3, 0));
+ TEST_NNG_PASS(nng_send(pub, "de", 2, 0)); // will not go through
+ TEST_NNG_PASS(nng_send(pub, "abc123", 6, 0));
+ TEST_NNG_PASS(nng_send(pub, "xzy", 3, 0)); // does not match
+ TEST_NNG_PASS(nng_send(pub, "ghidrop", 7, 0)); // dropped by unsub
+ TEST_NNG_PASS(nng_send(pub, "jklmno", 6, 0));
+
+ testutil_sleep(100);
+ TEST_NNG_PASS(nng_setopt(sub, NNG_OPT_SUB_UNSUBSCRIBE, "ghi", 3));
+ sz = sizeof(buf);
+ TEST_NNG_PASS(nng_recv(sub, buf, &sz, 0));
+ TEST_CHECK(sz == 3);
+ TEST_CHECK(memcmp(buf, "def", 3) == 0);
+
+ sz = sizeof(buf);
+ TEST_NNG_PASS(nng_recv(sub, buf, &sz, 0));
+ TEST_CHECK(sz == 6);
+ TEST_CHECK(memcmp(buf, "abc123", 6) == 0);
+
+ sz = sizeof(buf);
+ TEST_NNG_PASS(nng_recv(sub, buf, &sz, 0));
+ TEST_CHECK(sz == 6);
+ TEST_CHECK(memcmp(buf, "jklmno", 6) == 0);
+
+ TEST_NNG_PASS(nng_close(sub));
+ TEST_NNG_PASS(nng_close(pub));
+}
+
+static void
+test_sub_multi_context(void)
+{
+ nng_socket sub;
+ nng_socket pub;
+ nng_ctx c1;
+ nng_ctx c2;
+ nng_aio * aio1;
+ nng_aio * aio2;
+ nng_msg * m;
+
+ TEST_NNG_PASS(nng_sub0_open(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio1, NULL, NULL));
+ TEST_NNG_PASS(nng_aio_alloc(&aio2, NULL, NULL));
+ TEST_NNG_PASS(nng_ctx_open(&c1, sub));
+ TEST_NNG_PASS(nng_ctx_open(&c2, sub));
+
+ TEST_NNG_PASS(nng_ctx_setopt(c1, NNG_OPT_SUB_SUBSCRIBE, "one", 3));
+ TEST_NNG_PASS(nng_ctx_setopt(c1, NNG_OPT_SUB_SUBSCRIBE, "all", 3));
+
+ TEST_NNG_PASS(nng_ctx_setopt(c2, NNG_OPT_SUB_SUBSCRIBE, "two", 3));
+ TEST_NNG_PASS(nng_ctx_setopt(c2, NNG_OPT_SUB_SUBSCRIBE, "all", 3));
+
+ nng_aio_set_timeout(aio1, 100);
+ nng_aio_set_timeout(aio2, 100);
+
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+
+ TEST_NNG_SEND_STR(pub, "one for the money");
+ TEST_NNG_SEND_STR(pub, "all dogs go to heaven");
+ TEST_NNG_SEND_STR(pub, "nobody likes a snitch");
+ TEST_NNG_SEND_STR(pub, "two for the show");
+
+ nng_ctx_recv(c1, aio1);
+ nng_aio_wait(aio1);
+ TEST_NNG_PASS(nng_aio_result(aio1));
+ m = nng_aio_get_msg(aio1);
+ TEST_CHECK(strcmp(nng_msg_body(m), "one for the money") == 0);
+ nng_msg_free(m);
+
+ nng_ctx_recv(c1, aio1);
+ nng_aio_wait(aio1);
+ TEST_NNG_PASS(nng_aio_result(aio1));
+ m = nng_aio_get_msg(aio1);
+ TEST_CHECK(strcmp(nng_msg_body(m), "all dogs go to heaven") == 0);
+ nng_msg_free(m);
+
+ nng_ctx_recv(c2, aio1);
+ nng_aio_wait(aio1);
+ TEST_NNG_PASS(nng_aio_result(aio1));
+ m = nng_aio_get_msg(aio1);
+ TEST_CHECK(strcmp(nng_msg_body(m), "all dogs go to heaven") == 0);
+ nng_msg_free(m);
+
+ nng_ctx_recv(c2, aio1);
+ nng_aio_wait(aio1);
+ TEST_NNG_PASS(nng_aio_result(aio1));
+ m = nng_aio_get_msg(aio1);
+ TEST_CHECK(strcmp(nng_msg_body(m), "two for the show") == 0);
+ nng_msg_free(m);
+
+ nng_ctx_recv(c1, aio1);
+ nng_ctx_recv(c2, aio2);
+
+ nng_aio_wait(aio1);
+ nng_aio_wait(aio2);
+ TEST_NNG_FAIL(nng_aio_result(aio1), NNG_ETIMEDOUT);
+ TEST_NNG_FAIL(nng_aio_result(aio2), NNG_ETIMEDOUT);
+ TEST_NNG_PASS(nng_close(sub));
+ TEST_NNG_PASS(nng_close(pub));
+ nng_aio_free(aio1);
+ nng_aio_free(aio2);
+}
+
+static void
+test_sub_cooked(void)
+{
+ nng_socket s;
+ bool b;
+
+ TEST_NNG_PASS(nng_sub0_open(&s));
+ TEST_NNG_PASS(nng_getopt_bool(s, NNG_OPT_RAW, &b));
+ TEST_CHECK(!b);
+ TEST_NNG_PASS(nng_close(s));
+}
+
+TEST_LIST = {
+ { "sub identity", test_sub_identity },
+ { "sub cannot send", test_sub_cannot_send },
+ { "sub context cannot send", test_sub_context_cannot_send },
+ { "sub not writeable", test_sub_not_writeable },
+ { "sub poll readable", test_sub_poll_readable },
+ { "sub context does not poll", test_sub_context_no_poll },
+ { "sub validate peer", test_sub_validate_peer },
+ { "sub recv late", test_sub_recv_late },
+ { "sub recv ctx closed", test_sub_recv_ctx_closed },
+ { "sub recv aio ctx stopped", test_sub_ctx_recv_aio_stopped },
+ { "sub close context recv", test_sub_close_context_recv },
+ { "sub context recv nonblock", test_sub_ctx_recv_nonblock },
+ { "sub context recv cancel", test_sub_ctx_recv_cancel },
+ { "sub recv buf option", test_sub_recv_buf_option },
+ { "sub subscribe option", test_sub_subscribe_option },
+ { "sub unsubscribe option", test_sub_unsubscribe_option },
+ { "sub prefer new option", test_sub_prefer_new_option },
+ { "sub drop new", test_sub_drop_new },
+ { "sub drop old", test_sub_drop_old },
+ { "sub filter", test_sub_filter },
+ { "sub multi context", test_sub_multi_context },
+ { "sub cooked", test_sub_cooked },
+ { NULL, NULL },
+};
diff --git a/src/protocol/pubsub0/xsub.c b/src/protocol/pubsub0/xsub.c
index baa4f8eb..0013b8b3 100644
--- a/src/protocol/pubsub0/xsub.c
+++ b/src/protocol/pubsub0/xsub.c
@@ -41,7 +41,7 @@ struct xsub0_sock {
struct xsub0_pipe {
nni_pipe * pipe;
xsub0_sock *sub;
- nni_aio * aio_recv;
+ nni_aio aio_recv;
};
static int
@@ -77,7 +77,7 @@ xsub0_pipe_stop(void *arg)
{
xsub0_pipe *p = arg;
- nni_aio_stop(p->aio_recv);
+ nni_aio_stop(&p->aio_recv);
}
static void
@@ -85,19 +85,15 @@ xsub0_pipe_fini(void *arg)
{
xsub0_pipe *p = arg;
- nni_aio_free(p->aio_recv);
+ nni_aio_fini(&p->aio_recv);
}
static int
xsub0_pipe_init(void *arg, nni_pipe *pipe, void *s)
{
xsub0_pipe *p = arg;
- int rv;
- if ((rv = nni_aio_alloc(&p->aio_recv, xsub0_recv_cb, p)) != 0) {
- xsub0_pipe_fini(p);
- return (rv);
- }
+ nni_aio_init(&p->aio_recv, xsub0_recv_cb, p);
p->pipe = pipe;
p->sub = s;
@@ -114,7 +110,7 @@ xsub0_pipe_start(void *arg)
return (NNG_EPROTO);
}
- nni_pipe_recv(p->pipe, p->aio_recv);
+ nni_pipe_recv(p->pipe, &p->aio_recv);
return (0);
}
@@ -123,7 +119,7 @@ xsub0_pipe_close(void *arg)
{
xsub0_pipe *p = arg;
- nni_aio_close(p->aio_recv);
+ nni_aio_close(&p->aio_recv);
}
static void
@@ -134,29 +130,25 @@ xsub0_recv_cb(void *arg)
nni_msgq * urq = s->urq;
nni_msg * msg;
- if (nni_aio_result(p->aio_recv) != 0) {
+ if (nni_aio_result(&p->aio_recv) != 0) {
nni_pipe_close(p->pipe);
return;
}
- msg = nni_aio_get_msg(p->aio_recv);
- nni_aio_set_msg(p->aio_recv, NULL);
+ msg = nni_aio_get_msg(&p->aio_recv);
+ nni_aio_set_msg(&p->aio_recv, NULL);
nni_msg_set_pipe(msg, nni_pipe_id(p->pipe));
- switch (nni_msgq_tryput(urq, msg)) {
- case 0:
- break;
- case NNG_EAGAIN:
+ if (nni_msgq_tryput(urq, msg) != 0) {
+ // This only happens for two reasons. For flow control,
+ // in which case we just want to discard the message and
+ // carry on, and for a close of the socket (which is very
+ // hard to achieve, since we close the pipes.) In either
+ // case the easiest thing to do is just free the message
+ // and try again.
nni_msg_free(msg);
- break;
- default:
- // Any other error we stop the pipe for. It's probably
- // NNG_ECLOSED anyway.
- nng_msg_free(msg);
- nni_pipe_close(p->pipe);
- return;
}
- nni_pipe_recv(p->pipe, p->aio_recv);
+ nni_pipe_recv(p->pipe, &p->aio_recv);
}
static void
diff --git a/src/protocol/pubsub0/xsub_test.c b/src/protocol/pubsub0/xsub_test.c
new file mode 100644
index 00000000..a05b893c
--- /dev/null
+++ b/src/protocol/pubsub0/xsub_test.c
@@ -0,0 +1,387 @@
+//
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
+//
+// 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 <string.h>
+
+#include <nng/nng.h>
+#include <nng/protocol/pubsub0/pub.h>
+#include <nng/protocol/pubsub0/sub.h>
+
+#include <acutest.h>
+#include <testutil.h>
+
+#ifndef NNI_PROTO
+#define NNI_PROTO(x, y) (((x) << 4u) | (y))
+#endif
+
+static void
+test_xsub_identity(void)
+{
+ nng_socket s;
+ int p;
+ char * n;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&s));
+ TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PROTO, &p));
+ TEST_CHECK(p == NNI_PROTO(2u, 1u)); // 33
+ TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PEER, &p));
+ TEST_CHECK(p == NNI_PROTO(2u, 0u)); // 32
+ TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PROTONAME, &n));
+ TEST_CHECK(strcmp(n, "sub") == 0);
+ nng_strfree(n);
+ TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PEERNAME, &n));
+ TEST_CHECK(strcmp(n, "pub") == 0);
+ nng_strfree(n);
+ TEST_NNG_PASS(nng_close(s));
+}
+
+static void
+test_xsub_cannot_send(void)
+{
+ nng_socket sub;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_FAIL(nng_send(sub, "", 0, 0), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_xsub_not_writeable(void)
+{
+ int fd;
+ nng_socket sub;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_FAIL(nng_getopt_int(sub, NNG_OPT_SENDFD, &fd), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_xsub_poll_readable(void)
+{
+ int fd;
+ nng_socket pub;
+ nng_socket sub;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 1000));
+ TEST_NNG_PASS(nng_getopt_int(sub, NNG_OPT_RECVFD, &fd));
+ TEST_CHECK(fd >= 0);
+
+ // Not readable if not connected!
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ // Even after connect (no message yet)
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ // But once we send messages, it is.
+ // We have to send a request, in order to send a reply.
+ TEST_NNG_SEND_STR(pub, "abc");
+ testutil_sleep(200);
+
+ TEST_CHECK(testutil_pollfd(fd) == true);
+
+ // and receiving makes it no longer ready
+ TEST_NNG_RECV_STR(sub, "abc");
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_xsub_recv_late(void)
+{
+ int fd;
+ nng_socket pub;
+ nng_socket sub;
+ nng_aio * aio;
+ nng_msg * msg;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 1000));
+ TEST_NNG_PASS(nng_getopt_int(sub, NNG_OPT_RECVFD, &fd));
+ TEST_CHECK(fd >= 0);
+
+ // Not readable if not connected!
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ // Even after connect (no message yet)
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+ TEST_CHECK(testutil_pollfd(fd) == false);
+
+ nng_recv_aio(sub, aio);
+
+ // But once we send messages, it is.
+ // We have to send a request, in order to send a reply.
+ TEST_NNG_SEND_STR(pub, "abc");
+ testutil_sleep(200);
+
+ nng_aio_wait(aio);
+ TEST_NNG_PASS(nng_aio_result(aio));
+ msg = nng_aio_get_msg(aio);
+ nng_aio_set_msg(aio, NULL);
+ TEST_CHECK(nng_msg_len(msg) == 4);
+ TEST_CHECK(strcmp(nng_msg_body(msg), "abc") == 0);
+
+ nng_msg_free(msg);
+ nng_aio_free(aio);
+
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+void
+test_xsub_no_context(void)
+{
+ nng_socket sub;
+ nng_ctx ctx;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_FAIL(nng_ctx_open(&ctx, sub), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+void
+test_xsub_validate_peer(void)
+{
+ nng_socket s1, s2;
+ nng_stat * stats;
+ nng_stat * reject;
+ char addr[64];
+
+ testutil_scratch_addr("inproc", sizeof(addr), addr);
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&s1));
+ TEST_NNG_PASS(nng_sub0_open_raw(&s2));
+
+ TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0));
+ TEST_NNG_PASS(nng_dial(s2, addr, NULL, NNG_FLAG_NONBLOCK));
+
+ testutil_sleep(100);
+ TEST_NNG_PASS(nng_stats_get(&stats));
+
+ TEST_CHECK(stats != NULL);
+ TEST_CHECK((reject = nng_stat_find_socket(stats, s1)) != NULL);
+ TEST_CHECK((reject = nng_stat_find(reject, "reject")) != NULL);
+
+ TEST_CHECK(nng_stat_type(reject) == NNG_STAT_COUNTER);
+ TEST_CHECK(nng_stat_value(reject) > 0);
+
+ TEST_NNG_PASS(nng_close(s1));
+ TEST_NNG_PASS(nng_close(s2));
+ nng_stats_free(stats);
+}
+
+static void
+test_xsub_recv_closed(void)
+{
+ nng_socket sub;
+ nng_aio * aio;
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nng_close(sub);
+ nng_recv_aio(sub, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECLOSED);
+ nng_aio_free(aio);
+}
+
+static void
+test_xsub_close_recv(void)
+{
+ nng_socket sub;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+ nng_aio_set_timeout(aio, 1000);
+ nng_recv_aio(sub, aio);
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECLOSED);
+
+ nng_aio_free(aio);
+}
+
+static void
+test_xsub_recv_nonblock(void)
+{
+ nng_socket sub;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+
+ nng_aio_set_timeout(aio, 0); // Instant timeout
+ nng_recv_aio(sub, aio);
+
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ETIMEDOUT);
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+static void
+test_xsub_recv_buf_option(void)
+{
+ nng_socket sub;
+ int v;
+ bool b;
+ size_t sz;
+ const char *opt = NNG_OPT_RECVBUF;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+
+ TEST_NNG_PASS(nng_setopt_int(sub, opt, 1));
+ TEST_NNG_FAIL(nng_setopt_int(sub, opt, -1), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_int(sub, opt, 1000000), NNG_EINVAL);
+ TEST_NNG_PASS(nng_setopt_int(sub, opt, 3));
+ TEST_NNG_PASS(nng_getopt_int(sub, opt, &v));
+ TEST_CHECK(v == 3);
+ v = 0;
+ sz = sizeof(v);
+ TEST_NNG_PASS(nng_getopt(sub, opt, &v, &sz));
+ TEST_CHECK(v == 3);
+ TEST_CHECK(sz == sizeof(v));
+
+ TEST_NNG_FAIL(nng_setopt(sub, opt, "", 1), NNG_EINVAL);
+ sz = 1;
+ TEST_NNG_FAIL(nng_getopt(sub, opt, &v, &sz), NNG_EINVAL);
+ TEST_NNG_FAIL(nng_setopt_bool(sub, opt, true), NNG_EBADTYPE);
+ TEST_NNG_FAIL(nng_getopt_bool(sub, opt, &b), NNG_EBADTYPE);
+
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_xsub_subscribe_option(void)
+{
+ nng_socket sub;
+ const char *opt = NNG_OPT_SUB_SUBSCRIBE;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_FAIL(nng_setopt(sub, opt, "abc", 3), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_xsub_unsubscribe_option(void)
+{
+ nng_socket sub;
+ const char *opt = NNG_OPT_SUB_UNSUBSCRIBE;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_FAIL(nng_setopt(sub, opt, "abc", 3), NNG_ENOTSUP);
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_xsub_raw(void)
+{
+ nng_socket s;
+ bool b;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&s));
+ TEST_NNG_PASS(nng_getopt_bool(s, NNG_OPT_RAW, &b));
+ TEST_CHECK(b);
+ TEST_NNG_PASS(nng_close(s));
+}
+
+static void
+test_xsub_close_during_recv(void)
+{
+ nng_socket sub;
+ nng_socket pub;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 100));
+ TEST_NNG_PASS(nng_setopt_int(sub, NNG_OPT_RECVBUF, 5));
+ TEST_NNG_PASS(nng_setopt_int(pub, NNG_OPT_SENDBUF, 20));
+
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+
+ for (unsigned i = 0; i < 100; i++) {
+ TEST_NNG_PASS(nng_send(pub, "abc", 3, 0));
+ }
+ TEST_NNG_PASS(nng_close(pub));
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_xsub_close_during_pipe_recv(void)
+{
+ nng_socket sub;
+ nng_socket pub;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_PASS(nng_pub0_open(&pub));
+ TEST_NNG_PASS(nng_setopt_ms(sub, NNG_OPT_RECVTIMEO, 1000));
+ TEST_NNG_PASS(nng_setopt_ms(pub, NNG_OPT_SENDTIMEO, 100));
+ TEST_NNG_PASS(nng_setopt_int(sub, NNG_OPT_RECVBUF, 5));
+ TEST_NNG_PASS(nng_setopt_int(pub, NNG_OPT_SENDBUF, 20));
+
+ TEST_NNG_PASS(testutil_marry(pub, sub));
+
+ for (unsigned i = 0; i < 100; i++) {
+ int rv;
+ rv = nng_send(pub, "abc", 3, 0);
+ if (rv == NNG_ETIMEDOUT) {
+ break;
+ }
+ testutil_sleep(1);
+ }
+ TEST_NNG_PASS(nng_close(sub));
+}
+
+static void
+test_xsub_recv_aio_stopped(void)
+{
+ nng_socket sub;
+ nng_aio * aio;
+
+ TEST_NNG_PASS(nng_sub0_open_raw(&sub));
+ TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+
+ nng_aio_stop(aio);
+ nng_recv_aio(sub, aio);
+ nng_aio_wait(aio);
+ TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECANCELED);
+ TEST_NNG_PASS(nng_close(sub));
+ nng_aio_free(aio);
+}
+
+TEST_LIST = {
+ { "xsub identity", test_xsub_identity },
+ { "xsub cannot send", test_xsub_cannot_send },
+ { "xsub not writeable", test_xsub_not_writeable },
+ { "xsub poll readable", test_xsub_poll_readable },
+ { "xsub validate peer", test_xsub_validate_peer },
+ { "xsub recv late", test_xsub_recv_late },
+ { "xsub recv closed", test_xsub_recv_closed },
+ { "xsub close recv", test_xsub_close_recv },
+ { "xsub recv nonblock", test_xsub_recv_nonblock },
+ { "xsub recv buf option", test_xsub_recv_buf_option },
+ { "xsub subscribe option", test_xsub_subscribe_option },
+ { "xsub unsubscribe option", test_xsub_unsubscribe_option },
+ { "xsub no context", test_xsub_no_context },
+ { "xsub raw", test_xsub_raw },
+ { "xsub recv aio stopped", test_xsub_recv_aio_stopped },
+ { "xsub close during recv ", test_xsub_close_during_recv },
+ { "xsub close during pipe recv", test_xsub_close_during_pipe_recv },
+ { NULL, NULL },
+};
diff --git a/tests/pubsub.c b/tests/pubsub.c
index 68b7e3a4..14bd20fc 100644
--- a/tests/pubsub.c
+++ b/tests/pubsub.c
@@ -57,13 +57,6 @@ TestMain("PUB/SUB pattern", {
Reset({ nng_close(sub); });
- Convey("Send fails", {
- nng_msg *msg;
- So(nng_msg_alloc(&msg, 0) == 0);
- So(nng_sendmsg(sub, msg, 0) == NNG_ENOTSUP);
- nng_msg_free(msg);
- });
-
Convey("It can subscribe", {
So(nng_setopt(sub, NNG_OPT_SUB_SUBSCRIBE, "ABC", 3) ==
0);
diff --git a/tests/testutil.h b/tests/testutil.h
index 16e665f2..f28fed5c 100644
--- a/tests/testutil.h
+++ b/tests/testutil.h
@@ -77,16 +77,18 @@ extern int testutil_marry_ex(nng_socket, nng_socket, nng_pipe *, nng_pipe *);
#define TEST_NNG_SEND_STR(sock, string) \
TEST_NNG_PASS(nng_send(sock, string, strlen(string) + 1, 0))
-#define TEST_NNG_RECV_STR(sock, string) \
- do { \
- char * buf_; \
- size_t sz_; \
- int rv_ = nng_recv(sock, &buf_, &sz_, NNG_FLAG_ALLOC); \
- TEST_CHECK_( \
- rv_ == 0, "nng_recv (%d %s)", rv_, nng_strerror(rv_)); \
- TEST_CHECK(sz_ == strlen(string) + 1); \
- TEST_CHECK(strcmp(string, buf_) == 0); \
- nng_free(buf_, sz_); \
+#define TEST_NNG_RECV_STR(sock, string) \
+ do { \
+ char buf_[64]; \
+ size_t sz_ = sizeof(buf_); \
+ int rv_ = nng_recv(sock, &buf_, &sz_, 0); \
+ TEST_CHECK_( \
+ rv_ == 0, "nng_recv (%d %s)", rv_, nng_strerror(rv_)); \
+ TEST_CHECK_(sz_ == strlen(string) + 1, "length %d want %d", \
+ sz_, strlen(string) + 1); \
+ buf_[sizeof(buf_) - 1] = '\0'; \
+ TEST_CHECK_( \
+ strcmp(string, buf_) == 0, "%s == %s", string, buf_); \
} while (0)
#ifdef __cplusplus