diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-01-13 19:24:40 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-01-13 19:24:40 -0800 |
| commit | 49ea4dd5ec717303caeaef7f6f4efd10c90c94e1 (patch) | |
| tree | 85bceea6e6c1e8a9c82ec0be3a4d7a30229619fa | |
| parent | 859bc56d1a64cd37500b3291634be416e3fbb0e7 (diff) | |
| download | nng-49ea4dd5ec717303caeaef7f6f4efd10c90c94e1.tar.gz nng-49ea4dd5ec717303caeaef7f6f4efd10c90c94e1.tar.bz2 nng-49ea4dd5ec717303caeaef7f6f4efd10c90c94e1.zip | |
XSURVEY test suite added.
| -rw-r--r-- | src/protocol/survey0/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | src/protocol/survey0/xsurvey.c | 144 | ||||
| -rw-r--r-- | src/protocol/survey0/xsurvey_test.c | 368 |
3 files changed, 444 insertions, 74 deletions
diff --git a/src/protocol/survey0/CMakeLists.txt b/src/protocol/survey0/CMakeLists.txt index 271afabf..57d236f2 100644 --- a/src/protocol/survey0/CMakeLists.txt +++ b/src/protocol/survey0/CMakeLists.txt @@ -1,5 +1,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> # # This software is supplied under the terms of the MIT License, a @@ -21,4 +21,6 @@ nng_defines_if(NNG_PROTO_SURVEYOR0 NNG_HAVE_SURVEYOR0) nng_sources_if(NNG_PROTO_RESPONDENT0 respond.c xrespond.c) nng_headers_if(NNG_PROTO_RESPONDENT0 nng/protocol/survey0/respond.h) -nng_defines_if(NNG_PROTO_RESPONDENT0 NNG_HAVE_RESPONDENT0)
\ No newline at end of file +nng_defines_if(NNG_PROTO_RESPONDENT0 NNG_HAVE_RESPONDENT0) + +nng_test(xsurvey_test)
\ No newline at end of file diff --git a/src/protocol/survey0/xsurvey.c b/src/protocol/survey0/xsurvey.c index 86f912a2..13976a85 100644 --- a/src/protocol/survey0/xsurvey.c +++ b/src/protocol/survey0/xsurvey.c @@ -8,7 +8,6 @@ // found online at https://opensource.org/licenses/MIT. // -#include <stdlib.h> #include "core/nng_impl.h" #include "nng/protocol/survey0/survey.h" @@ -35,12 +34,12 @@ static void xsurv0_recv_cb(void *); // surv0_sock is our per-socket protocol private structure. struct xsurv0_sock { - int ttl; - nni_list pipes; - nni_aio * aio_getq; - nni_msgq *uwq; - nni_msgq *urq; - nni_mtx mtx; + nni_list pipes; + nni_aio aio_getq; + nni_msgq * uwq; + nni_msgq * urq; + nni_mtx mtx; + nni_atomic_int ttl; }; // surv0_pipe is our per-pipe protocol private structure. @@ -49,10 +48,10 @@ struct xsurv0_pipe { xsurv0_sock * psock; nni_msgq * sendq; nni_list_node node; - nni_aio * aio_getq; - nni_aio * aio_putq; - nni_aio * aio_send; - nni_aio * aio_recv; + nni_aio aio_getq; + nni_aio aio_putq; + nni_aio aio_send; + nni_aio aio_recv; }; static void @@ -60,7 +59,7 @@ xsurv0_sock_fini(void *arg) { xsurv0_sock *s = arg; - nni_aio_free(s->aio_getq); + nni_aio_fini(&s->aio_getq); nni_mtx_fini(&s->mtx); } @@ -68,18 +67,15 @@ static int xsurv0_sock_init(void *arg, nni_sock *nsock) { xsurv0_sock *s = arg; - int rv; - if ((rv = nni_aio_alloc(&s->aio_getq, xsurv0_sock_getq_cb, s)) != 0) { - xsurv0_sock_fini(s); - return (rv); - } + nni_aio_init(&s->aio_getq, xsurv0_sock_getq_cb, s); NNI_LIST_INIT(&s->pipes, xsurv0_pipe, node); nni_mtx_init(&s->mtx); s->uwq = nni_sock_sendq(nsock); s->urq = nni_sock_recvq(nsock); - s->ttl = 8; + nni_atomic_init(&s->ttl); + nni_atomic_set(&s->ttl, 8); return (0); } @@ -89,7 +85,7 @@ xsurv0_sock_open(void *arg) { xsurv0_sock *s = arg; - nni_msgq_aio_get(s->uwq, s->aio_getq); + nni_msgq_aio_get(s->uwq, &s->aio_getq); } static void @@ -97,7 +93,7 @@ xsurv0_sock_close(void *arg) { xsurv0_sock *s = arg; - nni_aio_close(s->aio_getq); + nni_aio_close(&s->aio_getq); } static void @@ -105,10 +101,10 @@ xsurv0_pipe_stop(void *arg) { xsurv0_pipe *p = arg; - nni_aio_stop(p->aio_getq); - nni_aio_stop(p->aio_send); - nni_aio_stop(p->aio_recv); - nni_aio_stop(p->aio_putq); + nni_aio_stop(&p->aio_getq); + nni_aio_stop(&p->aio_send); + nni_aio_stop(&p->aio_recv); + nni_aio_stop(&p->aio_putq); } static void @@ -116,10 +112,10 @@ xsurv0_pipe_fini(void *arg) { xsurv0_pipe *p = arg; - nni_aio_free(p->aio_getq); - nni_aio_free(p->aio_send); - nni_aio_free(p->aio_recv); - nni_aio_free(p->aio_putq); + nni_aio_fini(&p->aio_getq); + nni_aio_fini(&p->aio_send); + nni_aio_fini(&p->aio_recv); + nni_aio_fini(&p->aio_putq); nni_msgq_fini(p->sendq); } @@ -129,17 +125,18 @@ xsurv0_pipe_init(void *arg, nni_pipe *npipe, void *s) xsurv0_pipe *p = arg; int rv; + nni_aio_init(&p->aio_getq, xsurv0_getq_cb, p); + nni_aio_init(&p->aio_putq, xsurv0_putq_cb, p); + nni_aio_init(&p->aio_send, xsurv0_send_cb, p); + nni_aio_init(&p->aio_recv, xsurv0_recv_cb, p); + // This depth could be tunable. The queue exists so that if we // have multiple requests coming in faster than we can deliver them, // we try to avoid dropping them. We don't really have a solution // for applying back pressure. It would be nice if surveys carried // an expiration with them, so that we could discard any that are // not delivered before their expiration date. - if (((rv = nni_msgq_init(&p->sendq, 16)) != 0) || - ((rv = nni_aio_alloc(&p->aio_getq, xsurv0_getq_cb, p)) != 0) || - ((rv = nni_aio_alloc(&p->aio_putq, xsurv0_putq_cb, p)) != 0) || - ((rv = nni_aio_alloc(&p->aio_send, xsurv0_send_cb, p)) != 0) || - ((rv = nni_aio_alloc(&p->aio_recv, xsurv0_recv_cb, p)) != 0)) { + if ((rv = nni_msgq_init(&p->sendq, 16)) != 0) { xsurv0_pipe_fini(p); return (rv); } @@ -163,8 +160,8 @@ xsurv0_pipe_start(void *arg) nni_list_append(&s->pipes, p); nni_mtx_unlock(&s->mtx); - nni_msgq_aio_get(p->sendq, p->aio_getq); - nni_pipe_recv(p->npipe, p->aio_recv); + nni_msgq_aio_get(p->sendq, &p->aio_getq); + nni_pipe_recv(p->npipe, &p->aio_recv); return (0); } @@ -174,10 +171,10 @@ xsurv0_pipe_close(void *arg) xsurv0_pipe *p = arg; xsurv0_sock *s = p->psock; - nni_aio_close(p->aio_getq); - nni_aio_close(p->aio_send); - nni_aio_close(p->aio_recv); - nni_aio_close(p->aio_putq); + nni_aio_close(&p->aio_getq); + nni_aio_close(&p->aio_send); + nni_aio_close(&p->aio_recv); + nni_aio_close(&p->aio_putq); nni_msgq_close(p->sendq); @@ -193,15 +190,15 @@ xsurv0_getq_cb(void *arg) { xsurv0_pipe *p = arg; - if (nni_aio_result(p->aio_getq) != 0) { + if (nni_aio_result(&p->aio_getq) != 0) { nni_pipe_close(p->npipe); return; } - nni_aio_set_msg(p->aio_send, nni_aio_get_msg(p->aio_getq)); - nni_aio_set_msg(p->aio_getq, NULL); + nni_aio_set_msg(&p->aio_send, nni_aio_get_msg(&p->aio_getq)); + nni_aio_set_msg(&p->aio_getq, NULL); - nni_pipe_send(p->npipe, p->aio_send); + nni_pipe_send(p->npipe, &p->aio_send); } static void @@ -209,14 +206,14 @@ xsurv0_send_cb(void *arg) { xsurv0_pipe *p = arg; - if (nni_aio_result(p->aio_send) != 0) { - nni_msg_free(nni_aio_get_msg(p->aio_send)); - nni_aio_set_msg(p->aio_send, NULL); + if (nni_aio_result(&p->aio_send) != 0) { + nni_msg_free(nni_aio_get_msg(&p->aio_send)); + nni_aio_set_msg(&p->aio_send, NULL); nni_pipe_close(p->npipe); return; } - nni_msgq_aio_get(p->sendq, p->aio_getq); + nni_msgq_aio_get(p->sendq, &p->aio_getq); } static void @@ -224,14 +221,14 @@ xsurv0_putq_cb(void *arg) { xsurv0_pipe *p = arg; - if (nni_aio_result(p->aio_putq) != 0) { - nni_msg_free(nni_aio_get_msg(p->aio_putq)); - nni_aio_set_msg(p->aio_putq, NULL); + if (nni_aio_result(&p->aio_putq) != 0) { + nni_msg_free(nni_aio_get_msg(&p->aio_putq)); + nni_aio_set_msg(&p->aio_putq, NULL); nni_pipe_close(p->npipe); return; } - nni_pipe_recv(p->npipe, p->aio_recv); + nni_pipe_recv(p->npipe, &p->aio_recv); } static void @@ -240,13 +237,13 @@ xsurv0_recv_cb(void *arg) xsurv0_pipe *p = arg; nni_msg * msg; - if (nni_aio_result(p->aio_recv) != 0) { + if (nni_aio_result(&p->aio_recv) != 0) { nni_pipe_close(p->npipe); 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->npipe)); // We yank 4 bytes of body, and move them to the header. @@ -256,30 +253,33 @@ xsurv0_recv_cb(void *arg) nni_pipe_close(p->npipe); return; } - if (nni_msg_header_append(msg, nni_msg_body(msg), 4) != 0) { - // Probably ENOMEM, discard and keep going. - nni_msg_free(msg); - nni_pipe_recv(p->npipe, p->aio_recv); - return; - } + + // This cannot fail because the header should be zero bytes with + // 32 bytes of room. + (void) nni_msg_header_append(msg, nni_msg_body(msg), 4); (void) nni_msg_trim(msg, 4); - nni_aio_set_msg(p->aio_putq, msg); - nni_msgq_aio_put(p->psock->urq, p->aio_putq); + nni_aio_set_msg(&p->aio_putq, msg); + nni_msgq_aio_put(p->psock->urq, &p->aio_putq); } static int -xsurv0_sock_set_maxttl(void *arg, const void *buf, size_t sz, nni_opt_type t) +xsurv0_sock_set_max_ttl(void *arg, const void *buf, size_t sz, nni_opt_type t) { xsurv0_sock *s = arg; - return (nni_copyin_int(&s->ttl, buf, sz, 1, 255, t)); + int ttl; + int rv; + if ((rv = nni_copyin_int(&ttl, buf, sz, 1, 255, t)) == 0) { + nni_atomic_set(&s->ttl, ttl); + } + return (rv); } static int -xsurv0_sock_get_maxttl(void *arg, void *buf, size_t *szp, nni_opt_type t) +xsurv0_sock_get_max_ttl(void *arg, void *buf, size_t *szp, nni_opt_type t) { xsurv0_sock *s = arg; - return (nni_copyout_int(s->ttl, buf, szp, t)); + return (nni_copyout_int(nni_atomic_get(&s->ttl), buf, szp, t)); } static void @@ -290,12 +290,12 @@ xsurv0_sock_getq_cb(void *arg) xsurv0_pipe *last; nni_msg * msg, *dup; - if (nni_aio_result(s->aio_getq) != 0) { + if (nni_aio_result(&s->aio_getq) != 0) { // Should be NNG_ECLOSED. return; } - msg = nni_aio_get_msg(s->aio_getq); - nni_aio_set_msg(s->aio_getq, NULL); + msg = nni_aio_get_msg(&s->aio_getq); + nni_aio_set_msg(&s->aio_getq, NULL); nni_mtx_lock(&s->mtx); last = nni_list_last(&s->pipes); @@ -312,7 +312,7 @@ xsurv0_sock_getq_cb(void *arg) } } - nni_msgq_aio_get(s->uwq, s->aio_getq); + nni_msgq_aio_get(s->uwq, &s->aio_getq); nni_mtx_unlock(&s->mtx); if (last == NULL) { @@ -349,8 +349,8 @@ static nni_proto_pipe_ops xsurv0_pipe_ops = { static nni_option xsurv0_sock_options[] = { { .o_name = NNG_OPT_MAXTTL, - .o_get = xsurv0_sock_get_maxttl, - .o_set = xsurv0_sock_set_maxttl, + .o_get = xsurv0_sock_get_max_ttl, + .o_set = xsurv0_sock_set_max_ttl, }, // terminate list { diff --git a/src/protocol/survey0/xsurvey_test.c b/src/protocol/survey0/xsurvey_test.c new file mode 100644 index 00000000..0d436dfa --- /dev/null +++ b/src/protocol/survey0/xsurvey_test.c @@ -0,0 +1,368 @@ +// +// 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/reqrep0/rep.h> +#include <nng/protocol/reqrep0/req.h> +#include <nng/protocol/survey0/respond.h> +#include <nng/protocol/survey0/survey.h> + +#include <acutest.h> +#include <testutil.h> + +#ifndef NNI_PROTO +#define NNI_PROTO(x, y) (((x) << 4u) | (y)) +#endif + +static void +test_xsurveyor_identity(void) +{ + nng_socket s; + int p; + char * n; + + TEST_CHECK(nng_surveyor0_open_raw(&s) == 0); + TEST_CHECK(nng_getopt_int(s, NNG_OPT_PROTO, &p) == 0); + TEST_CHECK(p == NNI_PROTO(6u, 2u)); // 0x62 + TEST_CHECK(nng_getopt_int(s, NNG_OPT_PEER, &p) == 0); + TEST_CHECK(p == NNI_PROTO(6u, 3u)); // 0x63 + TEST_CHECK(nng_getopt_string(s, NNG_OPT_PROTONAME, &n) == 0); + TEST_CHECK(strcmp(n, "surveyor") == 0); + nng_strfree(n); + TEST_CHECK(nng_getopt_string(s, NNG_OPT_PEERNAME, &n) == 0); + TEST_CHECK(strcmp(n, "respondent") == 0); + nng_strfree(n); + TEST_CHECK(nng_close(s) == 0); +} + +static void +test_xsurveyor_raw(void) +{ + nng_socket s; + bool b; + + TEST_NNG_PASS(nng_surveyor0_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_xsurvey_no_context(void) +{ + nng_socket s; + nng_ctx ctx; + + TEST_NNG_PASS(nng_surveyor0_open_raw(&s)); + TEST_NNG_FAIL(nng_ctx_open(&ctx, s), NNG_ENOTSUP); + TEST_NNG_PASS(nng_close(s)); +} + +static void +test_xsurvey_poll_writeable(void) +{ + int fd; + nng_socket surv; + nng_socket resp; + + TEST_NNG_PASS(nng_surveyor0_open_raw(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(nng_getopt_int(surv, NNG_OPT_SENDFD, &fd)); + TEST_CHECK(fd >= 0); + + // Survey is broadcast, so we can always write. + TEST_CHECK(testutil_pollfd(fd) == true); + + TEST_NNG_PASS(testutil_marry(surv, resp)); + + // Now it's writable. + TEST_CHECK(testutil_pollfd(fd) == true); + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_xsurvey_poll_readable(void) +{ + int fd; + nng_socket surv; + nng_socket resp; + nng_msg * msg; + + TEST_NNG_PASS(nng_surveyor0_open_raw(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(nng_getopt_int(surv, NNG_OPT_RECVFD, &fd)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 1000)); + + 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(surv, resp)); + 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_PASS(nng_msg_alloc(&msg, 0)); + // Request ID + TEST_NNG_PASS(nng_msg_append_u32(msg, 0x80000000)); + TEST_NNG_PASS(nng_sendmsg(surv, msg, 0)); + + TEST_NNG_PASS(nng_recvmsg(resp, &msg, 0)); + TEST_NNG_PASS(nng_sendmsg(resp, msg, 0)); + + testutil_sleep(100); + + TEST_CHECK(testutil_pollfd(fd) == true); + + // and receiving makes it no longer ready + TEST_NNG_PASS(nng_recvmsg(surv, &msg, 0)); + nng_msg_free(msg); + TEST_CHECK(testutil_pollfd(fd) == false); + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_xsurvey_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_surveyor0_open_raw(&s1)); + TEST_NNG_PASS(nng_surveyor0_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_xsurvey_recv_aio_stopped(void) +{ + nng_socket surv; + nng_aio * aio; + + TEST_NNG_PASS(nng_surveyor0_open_raw(&surv)); + TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); + + nng_aio_stop(aio); + nng_recv_aio(surv, aio); + nng_aio_wait(aio); + TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECANCELED); + TEST_NNG_PASS(nng_close(surv)); + nng_aio_free(aio); +} + +static void +test_xsurvey_recv_garbage(void) +{ + nng_socket resp; + nng_socket surv; + nng_msg * m; + uint32_t req_id; + + TEST_NNG_PASS(nng_respondent0_open_raw(&resp)); + TEST_NNG_PASS(nng_surveyor0_open_raw(&surv)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 100)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 1000)); + + TEST_NNG_PASS(testutil_marry(surv, resp)); + + TEST_NNG_PASS(nng_msg_alloc(&m, 0)); + TEST_NNG_PASS(nng_msg_append_u32(m, 0x80000000)); + TEST_NNG_PASS(nng_sendmsg(surv, m, 0)); + + TEST_NNG_PASS(nng_recvmsg(resp, &m, 0)); + + // The message will have a header that contains the 32-bit pipe ID, + // followed by the 32-bit request ID. We will discard the request + // ID before sending it out. + TEST_CHECK(nng_msg_header_len(m) == 8); + TEST_NNG_PASS(nng_msg_header_chop_u32(m, &req_id)); + TEST_CHECK(req_id == 0x80000000); + + TEST_NNG_PASS(nng_sendmsg(resp, m, 0)); + TEST_NNG_FAIL(nng_recvmsg(surv, &m, 0), NNG_ETIMEDOUT); + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_xsurvey_close_during_recv(void) +{ + nng_socket resp; + nng_socket surv; + nng_msg * m; + nng_pipe p1; + nng_pipe p2; + + TEST_NNG_PASS(nng_respondent0_open_raw(&resp)); + TEST_NNG_PASS(nng_surveyor0_open_raw(&surv)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 100)); + TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_RECVBUF, 1)); + TEST_NNG_PASS(nng_setopt_int(resp, NNG_OPT_SENDBUF, 20)); + + TEST_NNG_PASS(testutil_marry_ex(surv, resp, &p1, &p2)); + TEST_CHECK(nng_pipe_id(p1) > 0); + TEST_CHECK(nng_pipe_id(p2) > 0); + + for (unsigned i = 0; i < 20; i++) { + TEST_NNG_PASS(nng_msg_alloc(&m, 4)); + TEST_NNG_PASS(nng_msg_header_append_u32(m, nng_pipe_id(p2))); + TEST_NNG_PASS(nng_msg_header_append_u32(m, i | 0x80000000u)); + testutil_sleep(10); + TEST_NNG_PASS(nng_sendmsg(resp, m, 0)); + } + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_xsurvey_close_pipe_during_send(void) +{ + nng_socket resp; + nng_socket surv; + nng_msg * m; + nng_pipe p1; + nng_pipe p2; + + TEST_NNG_PASS(nng_respondent0_open_raw(&resp)); + TEST_NNG_PASS(nng_surveyor0_open_raw(&surv)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 100)); + TEST_NNG_PASS(nng_setopt_int(resp, NNG_OPT_RECVBUF, 5)); + TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_SENDBUF, 20)); + + TEST_NNG_PASS(testutil_marry_ex(surv, resp, &p1, &p2)); + TEST_CHECK(nng_pipe_id(p1) > 0); + TEST_CHECK(nng_pipe_id(p2) > 0); + + for (unsigned i = 0; i < 20; i++) { + TEST_NNG_PASS(nng_msg_alloc(&m, 4)); + TEST_NNG_PASS(nng_msg_header_append_u32(m, i | 0x80000000u)); + testutil_sleep(10); + TEST_NNG_PASS(nng_sendmsg(surv, m, 0)); + } + + TEST_NNG_PASS(nng_pipe_close(p1)); + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_xsurvey_ttl_option(void) +{ + nng_socket s; + int v; + bool b; + size_t sz = sizeof(v); + const char *opt = NNG_OPT_MAXTTL; + + TEST_NNG_PASS(nng_surveyor0_open_raw(&s)); + + TEST_NNG_PASS(nng_setopt_int(s, opt, 1)); + TEST_NNG_FAIL(nng_setopt_int(s, opt, 0), NNG_EINVAL); + TEST_NNG_FAIL(nng_setopt_int(s, opt, -1), NNG_EINVAL); + TEST_NNG_FAIL(nng_setopt_int(s, opt, 256), NNG_EINVAL); + TEST_NNG_PASS(nng_setopt_int(s, opt, 3)); + TEST_NNG_PASS(nng_getopt_int(s, opt, &v)); + TEST_CHECK(v == 3); + v = 0; + sz = sizeof(v); + TEST_NNG_PASS(nng_getopt(s, opt, &v, &sz)); + TEST_CHECK(v == 3); + TEST_CHECK(sz == sizeof(v)); + + TEST_CHECK(nng_setopt(s, opt, "", 1) == NNG_EINVAL); + sz = 1; + TEST_CHECK(nng_getopt(s, opt, &v, &sz) == NNG_EINVAL); + TEST_CHECK(nng_setopt_bool(s, opt, true) == NNG_EBADTYPE); + TEST_CHECK(nng_getopt_bool(s, opt, &b) == NNG_EBADTYPE); + + TEST_CHECK(nng_close(s) == 0); +} + +static void +test_xsurvey_broadcast(void) +{ + nng_socket resp1; + nng_socket resp2; + nng_socket surv; + nng_msg * m; + + TEST_NNG_PASS(nng_respondent0_open(&resp1)); + TEST_NNG_PASS(nng_respondent0_open(&resp2)); + TEST_NNG_PASS(nng_surveyor0_open_raw(&surv)); + TEST_NNG_PASS(nng_setopt_ms(resp1, NNG_OPT_RECVTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(resp2, NNG_OPT_RECVTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 100)); + + TEST_NNG_PASS(testutil_marry(surv, resp1)); + TEST_NNG_PASS(testutil_marry(surv, resp2)); + + TEST_NNG_PASS(nng_msg_alloc(&m, 0)); + TEST_NNG_PASS(nng_msg_header_append_u32(m, 0x80000002u)); + TEST_NNG_PASS(nng_msg_append(m, "hello", 6)); + + TEST_NNG_PASS(nng_sendmsg(surv, m, 0)); + TEST_NNG_RECV_STR(resp1, "hello"); + TEST_NNG_RECV_STR(resp2, "hello"); + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp1)); + TEST_NNG_PASS(nng_close(resp2)); +} + +TEST_LIST = { + { "xsurvey identity", test_xsurveyor_identity }, + { "xsurvey raw", test_xsurveyor_raw }, + { "xsurvey no context", test_xsurvey_no_context }, + { "xsurvey poll readable", test_xsurvey_poll_readable }, + { "xsurvey poll writable", test_xsurvey_poll_writeable }, + { "xsurvey validate peer", test_xsurvey_validate_peer }, + { "xsurvey recv aio stopped", test_xsurvey_recv_aio_stopped }, + { "xsurvey recv garbage", test_xsurvey_recv_garbage }, + { "xsurvey close during recv", test_xsurvey_close_during_recv }, + { "xsurvey close pipe during send", + test_xsurvey_close_pipe_during_send }, + { "xsurvey ttl option", test_xsurvey_ttl_option }, + { "xsurvey broadcast", test_xsurvey_broadcast }, + { NULL, NULL }, +}; |
