From b46d532dc90b52eec42376e8c75bed5c6c7416a4 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 5 Feb 2020 00:17:52 -0800 Subject: Survey test rewrite. This bumps the coverage for survey up. While here fixed a few nits in req test, and removed the now pointless legacy survey and respond tests. --- src/protocol/reqrep0/req_test.c | 4 +- src/protocol/survey0/CMakeLists.txt | 1 + src/protocol/survey0/survey.c | 7 +- src/protocol/survey0/survey_test.c | 686 ++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 4 - tests/respondpoll.c | 109 ------ tests/survey.c | 335 ------------------ tests/surveyctx.c | 307 ---------------- tests/surveypoll.c | 126 ------- 9 files changed, 690 insertions(+), 889 deletions(-) create mode 100644 src/protocol/survey0/survey_test.c delete mode 100644 tests/respondpoll.c delete mode 100644 tests/survey.c delete mode 100644 tests/surveyctx.c delete mode 100644 tests/surveypoll.c diff --git a/src/protocol/reqrep0/req_test.c b/src/protocol/reqrep0/req_test.c index 6cb06f62..fa685e52 100644 --- a/src/protocol/reqrep0/req_test.c +++ b/src/protocol/reqrep0/req_test.c @@ -921,8 +921,8 @@ test_req_validate_peer(void) } TEST_LIST = { - { "req rep identity", test_req_identity }, - { "req resend option", test_req_ttl_option }, + { "req identity", test_req_identity }, + { "req ttl option", test_req_ttl_option }, { "req resend option", test_req_resend_option }, { "req recv bad state", test_req_recv_bad_state }, { "req recv garbage", test_req_recv_garbage }, diff --git a/src/protocol/survey0/CMakeLists.txt b/src/protocol/survey0/CMakeLists.txt index a180a196..6b3c8277 100644 --- a/src/protocol/survey0/CMakeLists.txt +++ b/src/protocol/survey0/CMakeLists.txt @@ -24,5 +24,6 @@ nng_headers_if(NNG_PROTO_RESPONDENT0 nng/protocol/survey0/respond.h) nng_defines_if(NNG_PROTO_RESPONDENT0 NNG_HAVE_RESPONDENT0) nng_test(respond_test) +nng_test(survey_test) nng_test(xrespond_test) nng_test(xsurvey_test) \ No newline at end of file diff --git a/src/protocol/survey0/survey.c b/src/protocol/survey0/survey.c index dcf92e07..999b8a56 100644 --- a/src/protocol/survey0/survey.c +++ b/src/protocol/survey0/survey.c @@ -384,12 +384,7 @@ surv0_pipe_recv_cb(void *arg) return; } id = nni_msg_trim_u32(msg); - if (nni_msg_header_append_u32(msg, id) != 0) { - // Should be NNG_ENOMEM - discard and try again. - nni_msg_free(msg); - nni_pipe_recv(p->npipe, p->aio_recv); - return; - } + nni_msg_header_must_append_u32(msg ,id); nni_mtx_lock(&sock->mtx); diff --git a/src/protocol/survey0/survey_test.c b/src/protocol/survey0/survey_test.c new file mode 100644 index 00000000..65395eee --- /dev/null +++ b/src/protocol/survey0/survey_test.c @@ -0,0 +1,686 @@ +// +// Copyright 2020 Staysail Systems, Inc. +// Copyright 2018 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 + +#include +#include +#include + +#include +#include + +static void +test_surv_identity(void) +{ + nng_socket s; + int p; + char * n; + + TEST_NNG_PASS(nng_surveyor0_open(&s)); + TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PROTO, &p)); + TEST_CHECK(p == NNG_SURVEYOR0_SELF); + TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PEER, &p)); + TEST_CHECK(p == NNG_SURVEYOR0_PEER); // 49 + TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PROTONAME, &n)); + TEST_CHECK(strcmp(n, NNG_SURVEYOR0_SELF_NAME) == 0); + nng_strfree(n); + TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PEERNAME, &n)); + TEST_CHECK(strcmp(n, NNG_SURVEYOR0_PEER_NAME) == 0); + nng_strfree(n); + TEST_NNG_PASS(nng_close(s)); +} + +static void +test_surv_ttl_option(void) +{ + nng_socket surv; + int v; + bool b; + size_t sz; + const char *opt = NNG_OPT_MAXTTL; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + + TEST_NNG_PASS(nng_setopt_int(surv, opt, 1)); + TEST_NNG_FAIL(nng_setopt_int(surv, opt, 0), NNG_EINVAL); + TEST_NNG_FAIL(nng_setopt_int(surv, opt, -1), NNG_EINVAL); + // This test will fail if the NNI_MAX_MAX_TTL is changed from the + // builtin default of 15. + TEST_NNG_FAIL(nng_setopt_int(surv, opt, 16), NNG_EINVAL); + TEST_NNG_FAIL(nng_setopt_int(surv, opt, 256), NNG_EINVAL); + TEST_NNG_PASS(nng_setopt_int(surv, opt, 3)); + TEST_NNG_PASS(nng_getopt_int(surv, opt, &v)); + TEST_CHECK(v == 3); + v = 0; + sz = sizeof(v); + TEST_NNG_PASS(nng_getopt(surv, opt, &v, &sz)); + TEST_CHECK(v == 3); + TEST_CHECK(sz == sizeof(v)); + + TEST_NNG_FAIL(nng_setopt(surv, opt, "", 1), NNG_EINVAL); + sz = 1; + TEST_NNG_FAIL(nng_getopt(surv, opt, &v, &sz), NNG_EINVAL); + TEST_NNG_FAIL(nng_setopt_bool(surv, opt, true), NNG_EBADTYPE); + TEST_NNG_FAIL(nng_getopt_bool(surv, opt, &b), NNG_EBADTYPE); + + TEST_NNG_PASS(nng_close(surv)); +} + +static void +test_surv_survey_time_option(void) +{ + nng_socket surv; + nng_duration d; + bool b; + size_t sz = sizeof(b); + const char * opt = NNG_OPT_SURVEYOR_SURVEYTIME; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + + TEST_NNG_PASS(nng_setopt_ms(surv, opt, 10)); + TEST_NNG_FAIL(nng_setopt(surv, opt, "", 1), NNG_EINVAL); + TEST_NNG_FAIL(nng_getopt(surv, opt, &b, &sz), NNG_EINVAL); + TEST_NNG_FAIL(nng_setopt_bool(surv, opt, true), NNG_EBADTYPE); + TEST_NNG_FAIL(nng_getopt_bool(surv, opt, &b), NNG_EBADTYPE); + + TEST_NNG_PASS(nng_getopt_ms(surv, opt, &d)); + TEST_CHECK(d == 10); + TEST_NNG_PASS(nng_close(surv)); +} + +void +test_surv_recv_bad_state(void) +{ + nng_socket surv; + nng_msg * msg = NULL; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_FAIL(nng_recvmsg(surv, &msg, 0), NNG_ESTATE); + TEST_CHECK(msg == NULL); + TEST_NNG_PASS(nng_close(surv)); +} + +static void +test_surv_recv_garbage(void) +{ + nng_socket resp; + nng_socket surv; + nng_msg * m; + uint32_t surv_id; + + TEST_NNG_PASS(nng_respondent0_open_raw(&resp)); + TEST_NNG_PASS(nng_surveyor0_open(&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_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, &surv_id)); + + 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)); +} + +#define SECOND 1000 + +void +test_surv_resp_exchange(void) +{ + nng_socket surv; + nng_socket resp; + nng_msg * msg = NULL; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, SECOND)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, SECOND)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, SECOND)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, SECOND)); + + TEST_NNG_PASS(testutil_marry(resp, surv)); + + TEST_NNG_PASS(nng_msg_alloc(&msg, 0)); + TEST_NNG_PASS(nng_msg_append(msg, "ping", 5)); + TEST_CHECK(nng_msg_len(msg) == 5); + TEST_CHECK(strcmp(nng_msg_body(msg), "ping") == 0); + TEST_NNG_PASS(nng_sendmsg(surv, msg, 0)); + msg = NULL; + TEST_NNG_PASS(nng_recvmsg(resp, &msg, 0)); + TEST_CHECK(msg != NULL); + TEST_CHECK(nng_msg_len(msg) == 5); + TEST_CHECK(strcmp(nng_msg_body(msg), "ping") == 0); + nng_msg_trim(msg, 5); + TEST_NNG_PASS(nng_msg_append(msg, "pong", 5)); + TEST_NNG_PASS(nng_sendmsg(resp, msg, 0)); + msg = NULL; + TEST_NNG_PASS(nng_recvmsg(surv, &msg, 0)); + TEST_CHECK(msg != NULL); + TEST_CHECK(nng_msg_len(msg) == 5); + TEST_CHECK(strcmp(nng_msg_body(msg), "pong") == 0); + nng_msg_free(msg); + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +void +test_surv_cancel(void) +{ + nng_msg * abc; + nng_msg * def; + nng_msg * cmd; + nng_socket surv; + nng_socket resp; + + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, SECOND)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, SECOND)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 5 * SECOND)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 5 * SECOND)); + TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_SENDBUF, 16)); + + TEST_NNG_PASS(nng_msg_alloc(&abc, 0)); + TEST_NNG_PASS(nng_msg_append(abc, "abc", 4)); + TEST_NNG_PASS(nng_msg_alloc(&def, 0)); + TEST_NNG_PASS(nng_msg_append(def, "def", 4)); + + TEST_NNG_PASS(testutil_marry(resp, surv)); + + // Send req #1 (abc). + TEST_CHECK(nng_sendmsg(surv, abc, 0) == 0); + + // Sleep a bit. This is so that we ensure that our request gets + // to the far side. (If we cancel too fast, then our outgoing send + // will be canceled before it gets to the peer.) + testutil_sleep(100); + + // Send the next next request ("def"). Note that + // the RESP side server will have already buffered the receive + // request, and should simply be waiting for us to reply to abc. + TEST_NNG_PASS(nng_sendmsg(surv, def, 0)); + + // Receive the first request (should be abc) on the REP server. + TEST_NNG_PASS(nng_recvmsg(resp, &cmd, 0)); + TEST_ASSERT(cmd != NULL); + TEST_CHECK(nng_msg_len(cmd) == 4); + TEST_CHECK(strcmp(nng_msg_body(cmd), "abc") == 0); + + // RESP sends the reply to first command. This will be discarded + // by the SURV socket. + TEST_NNG_PASS(nng_sendmsg(resp, cmd, 0)); + + // Now get the next command from the REP; should be "def". + TEST_NNG_PASS(nng_recvmsg(resp, &cmd, 0)); + TEST_ASSERT(cmd != NULL); + TEST_CHECK(nng_msg_len(cmd) == 4); + TEST_CHECK(strcmp(nng_msg_body(cmd), "def") == 0); + TEST_MSG("Received body was %s", nng_msg_body(cmd)); + + // And send it back to REQ. + TEST_NNG_PASS(nng_sendmsg(resp, cmd, 0)); + + // Try a req command. This should give back "def" + TEST_NNG_PASS(nng_recvmsg(surv, &cmd, 0)); + TEST_CHECK(nng_msg_len(cmd) == 4); + TEST_CHECK(strcmp(nng_msg_body(cmd), "def") == 0); + nng_msg_free(cmd); + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +void +test_surv_cancel_abort_recv(void) +{ + + nng_msg * abc; + nng_msg * def; + nng_msg * cmd; + nng_aio * aio; + nng_duration time = SECOND * 10; // 10s (kind of never) + nng_socket surv; + nng_socket resp; + + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); + + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SURVEYOR_SURVEYTIME, time)); + TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_SENDBUF, 16)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 5 * SECOND)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 5 * SECOND)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 5 * SECOND)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 5 * SECOND)); + + TEST_NNG_PASS(nng_msg_alloc(&abc, 0)); + TEST_NNG_PASS(nng_msg_append(abc, "abc", 4)); + TEST_NNG_PASS(nng_msg_alloc(&def, 0)); + TEST_NNG_PASS(nng_msg_append(def, "def", 4)); + + TEST_NNG_PASS(testutil_marry(resp, surv)); + + // Send survey #1 (abc). + TEST_NNG_PASS(nng_sendmsg(surv, abc, 0)); + + // Wait for it to get ot the other side. + testutil_sleep(100); + + nng_aio_set_timeout(aio, 5 * SECOND); + nng_recv_aio(surv, aio); + + // Give time for this recv to post properly. + testutil_sleep(100); + + // Send the next next request ("def"). Note that + // the respondent side server will have already buffered the receive + // request, and should simply be waiting for us to reply to + // abc. + TEST_NNG_PASS(nng_sendmsg(surv, def, 0)); + + // Our pending I/O should have been canceled. + nng_aio_wait(aio); + TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECANCELED); + + // Receive the first request (should be abc) on the respondent. + TEST_NNG_PASS(nng_recvmsg(resp, &cmd, 0)); + TEST_CHECK(nng_msg_len(cmd) == 4); + TEST_CHECK(strcmp(nng_msg_body(cmd), "abc") == 0); + + // Respondent sends the reply to first survey. This will be + // discarded by the SURV socket. + TEST_CHECK(nng_sendmsg(resp, cmd, 0) == 0); + + // Now get the next survey from the RESP; should be "def". + TEST_NNG_PASS(nng_recvmsg(resp, &cmd, 0)); + TEST_CHECK(nng_msg_len(cmd) == 4); + TEST_CHECK(strcmp(nng_msg_body(cmd), "def") == 0); + + // And send it back to REQ. + TEST_NNG_PASS(nng_sendmsg(resp, cmd, 0)); + + // Try a req command. This should give back "def" + TEST_NNG_PASS(nng_recvmsg(surv, &cmd, 0)); + TEST_CHECK(nng_msg_len(cmd) == 4); + TEST_CHECK(strcmp(nng_msg_body(cmd), "def") == 0); + nng_msg_free(cmd); + + nng_aio_free(aio); + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_surv_cancel_post_recv(void) +{ + nng_socket surv; + nng_socket resp; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + 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(surv, NNG_OPT_RECVTIMEO, 1000)); + TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 1000)); + TEST_NNG_PASS(testutil_marry(surv, resp)); + + TEST_NNG_SEND_STR(surv, "ONE"); + TEST_NNG_RECV_STR(resp, "ONE"); + TEST_NNG_SEND_STR(resp, "one"); + testutil_sleep(100); // Make sure reply arrives! + TEST_NNG_SEND_STR(surv, "TWO"); + TEST_NNG_RECV_STR(resp, "TWO"); + TEST_NNG_SEND_STR(resp, "two"); + TEST_NNG_RECV_STR(surv, "two"); + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_surv_poll_writeable(void) +{ + int fd; + nng_socket surv; + nng_socket resp; + + TEST_NNG_PASS(nng_surveyor0_open(&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)); +} + +void +test_surv_poll_readable(void) +{ + int fd; + nng_socket surv; + nng_socket resp; + nng_msg * msg; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(nng_getopt_int(surv, 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(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)); + TEST_NNG_PASS(nng_msg_append(msg, "xyz", 3)); + TEST_NNG_PASS(nng_sendmsg(surv, msg, 0)); + TEST_NNG_PASS(nng_recvmsg(resp, &msg, 0)); // recv on rep + TEST_NNG_PASS(nng_sendmsg(resp, msg, 0)); // echo it back + testutil_sleep(200); // give time for message to arrive + + 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); + + // TODO verify unsolicited response + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_surv_ctx_no_poll(void) +{ + int fd; + nng_socket surv; + nng_ctx ctx; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_ctx_open(&ctx, surv)); + 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(surv)); +} + +static void +test_surv_ctx_recv_nonblock(void) +{ + nng_socket surv; + nng_socket resp; + nng_ctx ctx; + nng_aio * aio; + nng_msg * msg; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(nng_ctx_open(&ctx, surv)); + TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); + TEST_NNG_PASS(nng_msg_alloc(&msg, 0)); + + TEST_NNG_PASS(testutil_marry(surv, resp)); + + nng_aio_set_msg(aio, msg); + nng_ctx_send(ctx, aio); + nng_aio_wait(aio); + TEST_NNG_PASS(nng_aio_result(aio)); + 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(surv)); + TEST_NNG_PASS(nng_close(resp)); + nng_aio_free(aio); +} + +static void +test_surv_ctx_send_nonblock(void) +{ + nng_socket surv; + nng_ctx ctx; + nng_aio * aio; + nng_msg * msg; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_ctx_open(&ctx, surv)); + TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); + TEST_NNG_PASS(nng_msg_alloc(&msg, 0)); + + nng_aio_set_msg(aio, msg); + nng_aio_set_timeout(aio, 0); // Instant timeout + nng_ctx_send(ctx, aio); + nng_aio_wait(aio); + TEST_NNG_PASS(nng_aio_result(aio)); // We never block + TEST_NNG_PASS(nng_close(surv)); + nng_aio_free(aio); +} + +static void +test_surv_send_best_effort(void) +{ + nng_socket surv; + nng_socket resp; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(testutil_marry(surv, resp)); + + for (int i = 0; i < 200; i++) { + TEST_NNG_SEND_STR(surv, "junk"); + } + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_surv_survey_timeout(void) +{ + nng_socket surv; + nng_socket resp; + char buf[16]; + size_t sz; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SURVEYOR_SURVEYTIME, 50)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 100)); + + TEST_NNG_PASS(testutil_marry(surv, resp)); + + TEST_NNG_SEND_STR(surv, "hello"); + TEST_NNG_RECV_STR(resp, "hello"); + + sz = sizeof(buf); + TEST_NNG_FAIL(nng_recv(surv, buf, &sz, 0), NNG_ETIMEDOUT); + TEST_NNG_SEND_STR(resp, "world"); + TEST_NNG_FAIL(nng_recv(surv, buf, &sz, 0), NNG_ESTATE); + + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_surv_ctx_recv_close_socket(void) +{ + nng_socket surv; + nng_socket resp; + nng_ctx ctx; + nng_aio * aio; + nng_msg * m; + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(nng_ctx_open(&ctx, surv)); + TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); + TEST_NNG_PASS(testutil_marry(surv, resp)); + TEST_NNG_PASS(nng_msg_alloc(&m, 0)); + nng_aio_set_msg(aio, m); + nng_ctx_send(ctx, aio); + nng_aio_wait(aio); + TEST_NNG_PASS(nng_aio_result(aio)); + + nng_ctx_recv(ctx, aio); + nng_close(surv); + + TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECLOSED); + nng_aio_free(aio); + TEST_NNG_PASS(nng_close(resp)); +} + +static void +test_surv_context_multi(void) +{ + nng_socket surv; + nng_socket resp; + nng_ctx c[5]; + nng_aio * aio; + nng_msg * m; + int cnt = sizeof(c) / sizeof(c[0]); + + TEST_NNG_PASS(nng_surveyor0_open(&surv)); + TEST_NNG_PASS(nng_respondent0_open(&resp)); + TEST_NNG_PASS(testutil_marry(surv, resp)); + TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SURVEYOR_SURVEYTIME, 200)); + TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); + + for (int i = 0; i < cnt; i++) { + TEST_NNG_PASS(nng_ctx_open(&c[i], surv)); + } + + for (int i = 0; i < cnt; i++) { + TEST_NNG_PASS(nng_msg_alloc(&m, 0)); + TEST_NNG_PASS(nng_msg_append_u32(m, i)); + nng_aio_set_msg(aio, m); + nng_ctx_send(c[i], aio); + nng_aio_wait(aio); + TEST_NNG_PASS(nng_aio_result(aio)); + } + + for (int i = 0; i < cnt; i++) { + TEST_NNG_PASS(nng_recvmsg(resp, &m, 0)); + TEST_NNG_PASS(nng_sendmsg(resp, m, 0)); + } + + for (int i = cnt - 1; i >= 0; i--) { + uint32_t x; + nng_ctx_recv(c[i], aio); + nng_aio_wait(aio); + TEST_NNG_PASS(nng_aio_result(aio)); + m = nng_aio_get_msg(aio); + TEST_ASSERT(m != NULL); + TEST_NNG_PASS(nng_msg_trim_u32(m, &x)); + TEST_CHECK(x == (uint32_t)i); + nng_msg_free(m); + } + + for (int i = 0; i < cnt; i++) { + nng_ctx_recv(c[i], aio); + nng_aio_wait(aio); + TEST_CHECK(nng_aio_result(aio) != 0); + } + for (int i = 0; i < cnt; i++) { + nng_ctx_close(c[i]); + } + TEST_NNG_PASS(nng_close(surv)); + TEST_NNG_PASS(nng_close(resp)); + nng_aio_free(aio); +} + +static void +test_surv_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(&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); +} + +TEST_LIST = { + { "survey identity", test_surv_identity }, + { "survey ttl option", test_surv_ttl_option }, + { "survey survey time option", test_surv_survey_time_option }, + { "survey recv bad state", test_surv_recv_bad_state }, + { "survey recv garbage", test_surv_recv_garbage }, + { "survey respondent exchange", test_surv_resp_exchange }, + { "survey cancel", test_surv_cancel }, + { "survey cancel abort recv", test_surv_cancel_abort_recv }, + { "survey cancel post recv", test_surv_cancel_post_recv }, + { "survey poll writable", test_surv_poll_writeable }, + { "survey poll readable", test_surv_poll_readable }, + { "survey context does not poll", test_surv_ctx_no_poll }, + { "survey context recv close socket", + test_surv_ctx_recv_close_socket }, + { "survey context recv nonblock", test_surv_ctx_recv_nonblock }, + { "survey context send nonblock", test_surv_ctx_send_nonblock }, + { "survey timeout", test_surv_survey_timeout }, + { "survey send best effort", test_surv_send_best_effort }, + { "survey context multi", test_surv_context_multi }, + { "survey validate peer", test_surv_validate_peer }, + { NULL, NULL }, +}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f64d6286..3690a4aa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -168,10 +168,6 @@ add_nng_test(bus 5) add_nng_test(pipeline 5) add_nng_test(reqctx 5) add_nng_test(reqstress 60) -add_nng_test(respondpoll 5) -add_nng_test(survey 5) -add_nng_test(surveyctx 5) -add_nng_test(surveypoll 5) # compatibility tests # We only support these if ALL the legacy protocols are supported. This diff --git a/tests/respondpoll.c b/tests/respondpoll.c deleted file mode 100644 index 4e869da8..00000000 --- a/tests/respondpoll.c +++ /dev/null @@ -1,109 +0,0 @@ -// -// Copyright 2018 Staysail Systems, Inc. -// Copyright 2018 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 -#include -#include -#include - -#include "convey.h" -#include "stubs.h" - -TestMain("Respondent pollable", { - atexit(nng_fini); - - Convey("Given a connected survey pair", { - nng_socket surv; - nng_socket resp; - nng_ctx ctx; - - So(nng_surveyor0_open(&surv) == 0); - So(nng_respondent0_open(&resp) == 0); - So(nng_ctx_open(&ctx, resp) == 0); - - So(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 2000) == 0); - So(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 2000) == 0); - So(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 2000) == 0); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 2000) == 0); - - Reset({ - nng_ctx_close(ctx); - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://ctx1", NULL, 0) == 0); - - Convey("Respondent ctx not pollable", { - int fd; - - So(nng_ctx_getopt_int(ctx, NNG_OPT_SENDFD, &fd) == - NNG_ENOTSUP); - So(nng_ctx_getopt_int(ctx, NNG_OPT_RECVFD, &fd) == - NNG_ENOTSUP); - }); - - Convey("Respondent starts not writable", { - int fd; - - So(nng_getopt_int(resp, NNG_OPT_SENDFD, &fd) == 0); - So(fdready(fd) == false); - - Convey("And remains unwritable on connect", { - So(nng_dial(surv, "inproc://ctx1", NULL, 0) == - 0); - nng_msleep(100); - So(fdready(fd) == false); - - Convey("Becomes writable after recv", { - nng_msg *m; - So(nng_msg_alloc(&m, 0) == 0); - So(nng_sendmsg(surv, m, 0) == 0); - So(nng_recvmsg(resp, &m, 0) == 0); - nng_msg_free(m); - So(fdready(fd) == true); - }); - }); - }); - - Convey("Respondent starts not readable", { - int fd; - - So(nng_getopt_int(resp, NNG_OPT_RECVFD, &fd) == 0); - So(fdready(fd) == false); - - Convey("And doesn't become readable on connect", { - So(nng_dial(surv, "inproc://ctx1", NULL, 0) == - 0); - nng_msleep(100); - So(fdready(fd) == false); - }); - - Convey("And becomes readable on data", { - nng_msg *msg; - - So(nng_dial(surv, "inproc://ctx1", NULL, 0) == - 0); - nng_msleep(200); - - So(nng_msg_alloc(&msg, 0) == 0); - So(fdready(fd) == false); - So(nng_msg_append(msg, "xyz", 3) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - nng_msleep(300); // give time for msg to arrive - So(fdready(fd) == true); - Convey("Is no longer readable after recv", { - So(nng_recvmsg(resp, &msg, 0) == 0); - nng_msg_free(msg); - So(fdready(fd) == false); - }); - }); - }); - }); -}) diff --git a/tests/survey.c b/tests/survey.c deleted file mode 100644 index d1af09ec..00000000 --- a/tests/survey.c +++ /dev/null @@ -1,335 +0,0 @@ -// -// 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 - -#include -#include -#include -#include - -#include "convey.h" -#include "stubs.h" - -#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("SURVEY pattern", { - const char *addr = "inproc://test"; - - atexit(nng_fini); - - Convey("We can create a SURVEYOR socket", { - nng_socket surv; - - So(nng_surveyor_open(&surv) == 0); - - Reset({ nng_close(surv); }); - - Convey("Recv with no survey fails", { - nng_msg *msg; - So(nng_recvmsg(surv, &msg, 0) == NNG_ESTATE); - }); - - Convey("Survey without responder times out", { - nng_msg *msg; - - So(nng_setopt_ms( - surv, NNG_OPT_SURVEYOR_SURVEYTIME, 50) == 0); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(surv, &msg, 0) == NNG_ETIMEDOUT); - }); - }); - - Convey("We can create a RESPONDENT socket", { - nng_socket resp; - So(nng_respondent_open(&resp) == 0); - - Reset({ nng_close(resp); }); - - Convey("Send fails with no survey", { - nng_msg *msg; - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_sendmsg(resp, msg, 0) == NNG_ESTATE); - nng_msg_free(msg); - }); - }); - - Convey("We can create a linked survey pair", { - nng_socket surv; - nng_socket resp; - nng_socket sock; - - So(nng_surveyor_open(&surv) == 0); - So(nng_respondent_open(&resp) == 0); - - Reset({ - nng_close(surv); - nng_close(resp); - }); - - So(nng_setopt_ms(surv, NNG_OPT_SURVEYOR_SURVEYTIME, 50) == 0); - So(nng_listen(surv, addr, NULL, 0) == 0); - So(nng_dial(resp, addr, NULL, 0) == 0); - - // We dial another socket as that will force - // the earlier dial to have completed *fully*. - // This is a hack that only works because our - // listen logic is single threaded. - So(nng_respondent_open(&sock) == 0); - So(nng_dial(sock, addr, NULL, 0) == 0); - nng_close(sock); - - Convey("Survey works", { - nng_msg *msg; - - So(nng_msg_alloc(&msg, 0) == 0); - APPENDSTR(msg, "abc"); - So(nng_sendmsg(surv, msg, 0) == 0); - msg = NULL; - So(nng_recvmsg(resp, &msg, 0) == 0); - CHECKSTR(msg, "abc"); - nng_msg_chop(msg, 3); - APPENDSTR(msg, "def"); - So(nng_sendmsg(resp, msg, 0) == 0); - msg = NULL; - So(nng_recvmsg(surv, &msg, 0) == 0); - CHECKSTR(msg, "def"); - nng_msg_free(msg); - - So(nng_recvmsg(surv, &msg, 0) == NNG_ETIMEDOUT); - - Convey("And goes to non-survey state", { - So(nng_setopt_ms( - surv, NNG_OPT_RECVTIMEO, 200) == 0); - So(nng_recvmsg(surv, &msg, 0) == NNG_ESTATE); - }); - }); - - Convey("Second send cancels pending recv", { - nng_msg *msg; - nng_aio *aio; - - So(nng_aio_alloc(&aio, NULL, NULL) == 0); - So(nng_msg_alloc(&msg, 0) == 0); - APPENDSTR(msg, "one"); - So(nng_sendmsg(surv, msg, 0) == 0); - msg = NULL; - nng_recv_aio(surv, aio); - So(nng_msg_alloc(&msg, 0) == 0); - APPENDSTR(msg, "two"); - So(nng_sendmsg(surv, msg, 0) == 0); - nng_aio_wait(aio); - So(nng_aio_result(aio) == NNG_ECANCELED); - nng_aio_free(aio); - }); - - Convey("Sending a NULL message does not panic", { - nng_aio *aio; - - So(nng_aio_alloc(&aio, NULL, NULL) == 0); - Reset({ nng_aio_free(aio); }); - So(nng_sendmsg(surv, NULL, 0) == NNG_EINVAL); - nng_send_aio(surv, aio); - nng_aio_wait(aio); - So(nng_aio_result(aio) == NNG_EINVAL); - }); - - Convey("Disconnecting before getting response", { - nng_msg *msg; - - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == 0); - nng_close(surv); - nng_msleep(100); - So(nng_sendmsg(resp, msg, 0) == 0); - }); - }); - - Convey("Bad backtrace survey is ignored", { - nng_socket surv; - nng_socket resp; - nng_msg * msg; - So(nng_surveyor0_open_raw(&surv) == 0); - So(nng_respondent0_open(&resp) == 0); - Reset({ - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://badsurvback", NULL, 0) == 0); - So(nng_dial(surv, "inproc://badsurvback", NULL, 0) == 0); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200) == 0); - nng_msleep(100); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_msg_header_append_u32(msg, 1) == - 0); // high order bit not set! - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == NNG_ETIMEDOUT); - }); - - Convey("Bad backtrace survey is ignored (raw)", { - nng_socket surv; - nng_socket resp; - nng_msg * msg; - So(nng_surveyor0_open_raw(&surv) == 0); - So(nng_respondent0_open_raw(&resp) == 0); - Reset({ - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://badsurvback", NULL, 0) == 0); - So(nng_dial(surv, "inproc://badsurvback", NULL, 0) == 0); - nng_msleep(100); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200) == 0); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_msg_header_append_u32(msg, 1) == - 0); // high order bit not set! - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == NNG_ETIMEDOUT); - }); - - Convey("Missing backtrace survey is ignored", { - nng_socket surv; - nng_socket resp; - nng_msg * msg; - So(nng_surveyor0_open_raw(&surv) == 0); - So(nng_respondent0_open(&resp) == 0); - Reset({ - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://badsurvback", NULL, 0) == 0); - So(nng_dial(surv, "inproc://badsurvback", NULL, 0) == 0); - nng_msleep(100); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200) == 0); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == NNG_ETIMEDOUT); - }); - - Convey("Missing backtrace survey is ignored (raw)", { - nng_socket surv; - nng_socket resp; - nng_msg * msg; - So(nng_surveyor0_open_raw(&surv) == 0); - So(nng_respondent0_open_raw(&resp) == 0); - Reset({ - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://badsurvback", NULL, 0) == 0); - So(nng_dial(surv, "inproc://badsurvback", NULL, 0) == 0); - nng_msleep(100); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200) == 0); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == NNG_ETIMEDOUT); - }); - - Convey("Bad backtrace response is ignored", { - nng_socket surv; - nng_socket resp; - nng_msg * msg; - So(nng_surveyor0_open(&surv) == 0); - So(nng_respondent0_open_raw(&resp) == 0); - Reset({ - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://badsurvback", NULL, 0) == 0); - So(nng_dial(surv, "inproc://badsurvback", NULL, 0) == 0); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200) == 0); - So(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 200) == 0); - nng_msleep(100); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == 0); - nng_msg_header_clear(msg); - nng_msg_header_append_u32(msg, 1); - So(nng_sendmsg(resp, msg, 0) == 0); - So(nng_recvmsg(surv, &msg, 0) == NNG_ETIMEDOUT); - }); - - Convey("Bad backtrace response is ignored (raw)", { - nng_socket surv; - nng_socket resp; - nng_msg * msg; - So(nng_surveyor0_open_raw(&surv) == 0); - So(nng_respondent0_open_raw(&resp) == 0); - Reset({ - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://badsurvback", NULL, 0) == 0); - So(nng_dial(surv, "inproc://badsurvback", NULL, 0) == 0); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200) == 0); - So(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 200) == 0); - nng_msleep(100); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_msg_header_append_u32(msg, 0x80000000) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == 0); - nng_msg_header_clear(msg); - nng_msg_header_append_u32(msg, 1); - So(nng_sendmsg(resp, msg, 0) == 0); - So(nng_recvmsg(surv, &msg, 0) == NNG_ETIMEDOUT); - }); - - Convey("Missing backtrace response is ignored", { - nng_socket surv; - nng_socket resp; - nng_msg * msg; - So(nng_surveyor0_open(&surv) == 0); - So(nng_respondent0_open_raw(&resp) == 0); - Reset({ - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://badsurvback", NULL, 0) == 0); - So(nng_dial(surv, "inproc://badsurvback", NULL, 0) == 0); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200) == 0); - So(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 200) == 0); - nng_msleep(100); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == 0); - nng_msg_header_clear(msg); - So(nng_sendmsg(resp, msg, 0) == 0); - So(nng_recvmsg(surv, &msg, 0) == NNG_ETIMEDOUT); - }); - - Convey("Missing backtrace response is ignored (raw)", { - nng_socket surv; - nng_socket resp; - nng_msg * msg; - So(nng_surveyor0_open_raw(&surv) == 0); - So(nng_respondent0_open_raw(&resp) == 0); - Reset({ - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://badsurvback", NULL, 0) == 0); - So(nng_dial(surv, "inproc://badsurvback", NULL, 0) == 0); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200) == 0); - So(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 200) == 0); - nng_msleep(100); - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_msg_header_append_u32(msg, 0x80000000) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == 0); - nng_msg_header_clear(msg); - So(nng_sendmsg(resp, msg, 0) == 0); - So(nng_recvmsg(surv, &msg, 0) == NNG_ETIMEDOUT); - }); -}) diff --git a/tests/surveyctx.c b/tests/surveyctx.c deleted file mode 100644 index d2d1c4d7..00000000 --- a/tests/surveyctx.c +++ /dev/null @@ -1,307 +0,0 @@ -// -// Copyright 2018 Staysail Systems, Inc. -// Copyright 2018 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 - -#include -#include -#include -#include - -#include "convey.h" -#include "stubs.h" - -static struct { - nng_aio *aio; - enum { START, SEND, RECV } state; - nng_socket s; - nng_msg * msg; - int cnt; -} resp_state; - -void -resp_cb(void *notused) -{ - int rv; - (void) notused; - - if (resp_state.state == START) { - resp_state.state = RECV; - nng_recv_aio(resp_state.s, resp_state.aio); - return; - } - if ((rv = nng_aio_result(resp_state.aio)) != 0) { - if (resp_state.msg != NULL) { - nng_msg_free(resp_state.msg); - resp_state.msg = NULL; - } - return; - } - switch (resp_state.state) { - case START: - break; - case RECV: - resp_state.msg = nng_aio_get_msg(resp_state.aio); - resp_state.state = SEND; - nng_aio_set_msg(resp_state.aio, resp_state.msg); - nng_send_aio(resp_state.s, resp_state.aio); - break; - case SEND: - resp_state.msg = NULL; - resp_state.state = RECV; - nng_aio_set_msg(resp_state.aio, NULL); - nng_recv_aio(resp_state.s, resp_state.aio); - resp_state.cnt++; - break; - } -} - -#define NCTX 10 - -void -markr(void *arg) -{ - *(bool *) arg = true; -} - -static void -marks(void *arg) -{ - *(bool *) arg = true; -} - -nng_ctx ctxs[NCTX]; -uint32_t recv_order[NCTX]; -nng_aio *saios[NCTX]; -nng_aio *raios[NCTX]; -bool recd[NCTX]; -bool sent[NCTX]; - -TestMain("Surveyor concurrent contexts", { - int rv; - const char *addr = "inproc://test"; - int i; - - memset(recv_order, 0, NCTX * sizeof(int)); - - atexit(nng_fini); - - Convey("We can use Surveyor contexts concurrently", { - nng_socket surv = NNG_SOCKET_INITIALIZER; - - So(nng_aio_alloc(&resp_state.aio, resp_cb, NULL) == 0); - So(nng_respondent0_open(&resp_state.s) == 0); - So(nng_surveyor0_open(&surv) == 0); - - for (i = 0; i < NCTX; i++) { - sent[i] = recd[i] = false; - recv_order[i] = (uint32_t) i; - if (nng_aio_alloc(&raios[i], markr, &(recd[i])) != 0) { - break; - } - nng_aio_set_timeout(raios[i], 5000); - if (nng_aio_alloc(&saios[i], marks, &(sent[i])) != 0) { - break; - } - nng_aio_set_timeout(saios[i], 5000); - } - - // So(nng_setopt_int(resp_state.s, NNG_OPT_SENDBUF, NCTX) == - // 0); - So(i == NCTX); - for (i = 0; i < NCTX; i++) { - uint32_t tmp; - int ni = rand() % NCTX; // recv index - - tmp = recv_order[i]; - recv_order[i] = recv_order[ni]; - recv_order[ni] = tmp; - } - Reset({ - for (i = 0; i < NCTX; i++) { - nng_aio_free(saios[i]); - nng_aio_free(raios[i]); - } - nng_close(surv); - nng_close(resp_state.s); - nng_aio_free(resp_state.aio); - }); - - So(nng_listen(resp_state.s, addr, NULL, 0) == 0); - So(nng_dial(surv, addr, NULL, 0) == 0); - - nng_msleep(100); // let things establish. - - // Start the rep state machine going. - resp_cb(NULL); - - for (i = 0; i < NCTX; i++) { - if ((rv = nng_ctx_open(&ctxs[i], surv)) != 0) { - break; - } - if (nng_ctx_id(ctxs[i]) < 0) { - Fail("Invalid context ID"); - break; - } - if ((i > 0) && - (nng_ctx_id(ctxs[i]) == nng_ctx_id(ctxs[i - 1]))) { - Fail("Context IDs not different"); - } - } - So(rv == 0); - So(i == NCTX); - - // Send messages - for (i = 0; i < NCTX; i++) { - nng_msg *m; - if ((rv = nng_msg_alloc(&m, sizeof(uint32_t))) != 0) { - Fail("msg alloc failed: %s", nng_strerror(rv)); - } - if ((rv = nng_msg_append_u32(m, i)) != 0) { - Fail("append failed: %s", nng_strerror(rv)); - } - nng_aio_set_msg(saios[i], m); - nng_ctx_send(ctxs[i], saios[i]); - } - So(rv == 0); - So(i == NCTX); - - for (i = 0; i < NCTX; i++) { - nng_aio_wait(saios[i]); - if ((rv = nng_aio_result(saios[i])) != 0) { - Fail("send failed: %s", nng_strerror(rv)); - So(false); - break; - } - } - for (i = 0; i < NCTX; i++) { - if (!sent[i]) { - Fail("Index %d (%d) not sent", i, i); - } - } - - So(rv == 0); - So(i == NCTX); - // Receive answers - for (i = 0; i < NCTX; i++) { - int ri = recv_order[i]; - nng_ctx_recv(ctxs[ri], raios[ri]); - } - - for (i = 0; i < NCTX; i++) { - nng_msg *msg; - uint32_t x; - - nng_aio_wait(raios[i]); - if ((rv = nng_aio_result(raios[i])) != 0) { - Fail("recv %d (%d) %d failed: %s", i, - recv_order[i], resp_state.cnt, - nng_strerror(rv)); - continue; - } - msg = nng_aio_get_msg(raios[i]); - if ((rv = nng_msg_chop_u32(msg, &x)) != 0) { - Fail("recv msg trim: %s", nng_strerror(rv)); - break; - } - if (x != (uint32_t) i) { - Fail("message body mismatch: %x %x\n", x, - (uint32_t) i); - break; - } - - nng_msg_free(msg); - } - for (i = 0; i < NCTX; i++) { - if (!recd[i]) { - Fail("Index %d (%d) not received", i, - recv_order[i]); - break; - } - } - - So(rv == 0); - So(i == NCTX); - }); - - Convey("Given a socket and a context", { - nng_socket surv; - nng_ctx ctx; - nng_aio * aio; - - So(nng_surveyor0_open(&surv) == 0); - So(nng_ctx_open(&ctx, surv) == 0); - So(nng_aio_alloc(&aio, NULL, NULL) == 0); - nng_aio_set_timeout(aio, 1000); - - Reset({ nng_aio_free(aio); }); - - Convey("Recv on the context is ESTATE", { - nng_ctx_recv(ctx, aio); - nng_aio_wait(aio); - So(nng_aio_result(aio) == NNG_ESTATE); - }); - - Convey("Closing the socket aborts a context recv", { - nng_msg *msg; - So(nng_msg_alloc(&msg, 0) == 0); - nng_aio_set_msg(aio, msg); - nng_ctx_send(ctx, aio); - nng_aio_wait(aio); - So(nng_aio_result(aio) == 0); - nng_ctx_recv(ctx, aio); - nng_close(surv); - nng_aio_wait(aio); - So(nng_aio_result(aio) == NNG_ECLOSED); - }); - - Convey("Sending a null message fails", { - nng_ctx_send(ctx, aio); - nng_aio_wait(aio); - So(nng_aio_result(aio) == NNG_EINVAL); - }); - - Convey("Closing the context aborts a context send", { - nng_msg *msg; - So(nng_msg_alloc(&msg, 0) == 0); - nng_aio_set_msg(aio, msg); - nng_ctx_send(ctx, aio); - nng_aio_wait(aio); - So(nng_aio_result(aio) == 0); - nng_ctx_recv(ctx, aio); - nng_ctx_close(ctx); - nng_aio_wait(aio); - So(nng_aio_result(aio) == NNG_ECLOSED); - nng_close(surv); - }); - - Convey("We can set separate survey times", { - nng_duration ms; - So(nng_setopt_ms( - surv, NNG_OPT_SURVEYOR_SURVEYTIME, 100) == 0); - So(nng_ctx_setopt_ms( - ctx, NNG_OPT_SURVEYOR_SURVEYTIME, 200) == 0); - So(nng_getopt_ms( - surv, NNG_OPT_SURVEYOR_SURVEYTIME, &ms) == 0); - So(ms == 100); - So(nng_ctx_getopt_ms( - ctx, NNG_OPT_SURVEYOR_SURVEYTIME, &ms) == 0); - So(ms == 200); - }); - }); - - Convey("Raw mode does not support contexts", { - nng_socket surv; - nng_ctx ctx; - So(nng_surveyor0_open_raw(&surv) == 0); - So(nng_ctx_open(&ctx, surv) == NNG_ENOTSUP); - nng_close(surv); - }); -}) diff --git a/tests/surveypoll.c b/tests/surveypoll.c deleted file mode 100644 index 2d6f02ae..00000000 --- a/tests/surveypoll.c +++ /dev/null @@ -1,126 +0,0 @@ -// -// Copyright 2018 Staysail Systems, Inc. -// Copyright 2018 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 -#include -#include -#include - -#include "convey.h" -#include "stubs.h" - -TestMain("Survey pollable", { - atexit(nng_fini); - - Convey("Given a connected survey pair", { - nng_socket surv; - nng_socket resp; - nng_ctx ctx; - - So(nng_surveyor0_open(&surv) == 0); - So(nng_respondent0_open(&resp) == 0); - So(nng_ctx_open(&ctx, surv) == 0); - - So(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 2000) == 0); - So(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 2000) == 0); - So(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 2000) == 0); - So(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 2000) == 0); - - Reset({ - nng_ctx_close(ctx); - nng_close(surv); - nng_close(resp); - }); - So(nng_listen(resp, "inproc://ctx1", NULL, 0) == 0); - - Convey("Surveyor ctx not pollable", { - int fd; - - So(nng_ctx_getopt_int(ctx, NNG_OPT_SENDFD, &fd) == - NNG_ENOTSUP); - So(nng_ctx_getopt_int(ctx, NNG_OPT_RECVFD, &fd) == - NNG_ENOTSUP); - }); - - Convey("Suveyor starts writable", { - int fd; - - So(nng_getopt_int(surv, NNG_OPT_SENDFD, &fd) == 0); - So(fdready(fd) == true); - - Convey("And becomes readable on connect", { - So(nng_dial(surv, "inproc://ctx1", NULL, 0) == - 0); - nng_msleep(100); - So(fdready(fd) == true); - - Convey("And stays writable", { - // 500 messages should force all - // the way to send depth. - int i; - for (i = 0; i < 500; i++) { - nng_msg *m; - if (nng_msg_alloc(&m, 0) != - 0) { - break; - } - // Fill intermediate queues. - if (nng_sendmsg(surv, m, - NNG_FLAG_NONBLOCK) != - 0) { - nng_msg_free(m); - } - } - So(i == 500); - So(fdready(fd) == true); - }); - }); - }); - - Convey("Surveyor starts not readable", { - int fd; - - So(nng_getopt_int(surv, NNG_OPT_RECVFD, &fd) == 0); - So(fdready(fd) == false); - - Convey("And doesn't become readable on connect", { - So(nng_dial(surv, "inproc://ctx1", NULL, 0) == - 0); - nng_msleep(100); - So(fdready(fd) == false); - }); - - Convey("And becomes readable on data", { - nng_msg *msg; - - So(nng_dial(surv, "inproc://ctx1", NULL, 0) == - 0); - nng_msleep(200); - - So(nng_msg_alloc(&msg, 0) == 0); - So(fdready(fd) == false); - So(nng_msg_append(msg, "xyz", 3) == 0); - So(nng_sendmsg(surv, msg, 0) == 0); - So(nng_recvmsg(resp, &msg, 0) == - 0); // recv on rep - So(nng_sendmsg(resp, msg, 0) == - 0); // echo it back - nng_msleep( - 300); // give time for message to arrive - So(fdready(fd) == true); - Convey("Is no longer readable after recv", { - So(nng_recvmsg(surv, &msg, 0) == 0); - nng_msg_free(msg); - So(fdready(fd) == false); - }); - }); - }); - }); -}) -- cgit v1.2.3-70-g09d2