From c9a68bfe6bea2acc708bf49045f6cb65017a3306 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Thu, 24 Aug 2017 14:15:48 -0700 Subject: Eliminate legacy option settings, provide easier option IDs. This eliminates all the old #define's or enum values, making all option IDs now totally dynamic, and providing well-known string values for well-behaved applications. We have added tests of some of these options, including lookups, and so forth. We have also fixed a few problems; including at least one crasher bug when the timeouts on reconnect were zero. Protocol specific options are now handled in the protocol. We will be moving the initialization for a few of those well known entities to the protocol startup code, following the PAIRv1 pattern, later. Applications must therefore not depend on the value of the integer IDs, at least until the application has opened a socket of the appropriate type. --- tests/bus.c | 12 +-- tests/device.c | 19 ++-- tests/pair1.c | 120 ++++++++++------------ tests/pipeline.c | 287 ++++++++++++++++++++++++---------------------------- tests/pollfd.c | 219 +++++++++++++++++++++------------------ tests/pubsub.c | 44 ++++---- tests/reqrep.c | 29 +++++- tests/scalability.c | 51 +++------- tests/sock.c | 176 ++++++++++++++++---------------- tests/survey.c | 243 +++++++++++++++++++++----------------------- 10 files changed, 589 insertions(+), 611 deletions(-) (limited to 'tests') diff --git a/tests/bus.c b/tests/bus.c index d242a7dd..c38f8c5a 100644 --- a/tests/bus.c +++ b/tests/bus.c @@ -1,5 +1,6 @@ // // 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 @@ -56,14 +57,9 @@ TestMain("BUS pattern", { So(nng_dial(bus3, addr, NULL, 0) == 0); rtimeo = 50000; - So(nng_setopt( - bus1, NNG_OPT_RCVTIMEO, &rtimeo, sizeof(rtimeo)) == 0); - rtimeo = 50000; - So(nng_setopt( - bus2, NNG_OPT_RCVTIMEO, &rtimeo, sizeof(rtimeo)) == 0); - rtimeo = 50000; - So(nng_setopt( - bus3, NNG_OPT_RCVTIMEO, &rtimeo, sizeof(rtimeo)) == 0); + So(nng_setopt_usec(bus1, nng_optid_recvtimeo, rtimeo) == 0); + So(nng_setopt_usec(bus2, nng_optid_recvtimeo, rtimeo) == 0); + So(nng_setopt_usec(bus3, nng_optid_recvtimeo, rtimeo) == 0); Convey("Messages delivered", { nng_msg *msg; diff --git a/tests/device.c b/tests/device.c index 85a06fca..d3407eab 100644 --- a/tests/device.c +++ b/tests/device.c @@ -42,19 +42,15 @@ Main({ nng_socket dev2; nng_socket end1; nng_socket end2; - int raw; uint64_t tmo; nng_msg * msg; void * thr; So(nng_pair1_open(&dev1) == 0); So(nng_pair1_open(&dev2) == 0); - raw = 1; - So(nng_setopt(dev1, NNG_OPT_RAW, &raw, sizeof(raw)) == - 0); - raw = 1; - So(nng_setopt(dev2, NNG_OPT_RAW, &raw, sizeof(raw)) == - 0); + + So(nng_setopt_int(dev1, nng_optid_raw, 1) == 0); + So(nng_setopt_int(dev2, nng_optid_raw, 1) == 0); struct dev_data ddata; ddata.s1 = dev1; @@ -77,11 +73,10 @@ Main({ So(nng_dial(end2, addr2, NULL, 0) == 0); tmo = 1000000; - So(nng_setopt(end1, NNG_OPT_RCVTIMEO, &tmo, - sizeof(tmo)) == 0); - tmo = 1000000; - So(nng_setopt(end2, NNG_OPT_RCVTIMEO, &tmo, - sizeof(tmo)) == 0); + So(nng_setopt_usec(end1, nng_optid_recvtimeo, tmo) == + 0); + So(nng_setopt_usec(end2, nng_optid_recvtimeo, tmo) == + 0); nng_usleep(100000); Convey("Device can send and receive", { diff --git a/tests/pair1.c b/tests/pair1.c index adb50c88..6e7a22cb 100644 --- a/tests/pair1.c +++ b/tests/pair1.c @@ -14,6 +14,9 @@ #include +extern int nng_optid_pair1_poly; +extern const char *nng_opt_pair1_poly; + #define APPENDSTR(m, s) nng_msg_append(m, s, strlen(s)) #define CHECKSTR(m, s) \ So(nng_msg_len(m) == strlen(s)); \ @@ -44,13 +47,25 @@ TestMain("PAIRv1 protocol", { So(nng_pair1_open(&c2) == 0); tmo = 300000; - So(nng_setopt_usec(s1, NNG_OPT_RCVTIMEO, tmo) == 0); - So(nng_setopt_usec(c1, NNG_OPT_RCVTIMEO, tmo) == 0); - So(nng_setopt_usec(c2, NNG_OPT_RCVTIMEO, tmo) == 0); + So(nng_setopt_usec(s1, nng_optid_recvtimeo, tmo) == 0); + So(nng_setopt_usec(c1, nng_optid_recvtimeo, tmo) == 0); + So(nng_setopt_usec(c2, nng_optid_recvtimeo, tmo) == 0); tmo = 0; - So(nng_getopt_usec(s1, NNG_OPT_RCVTIMEO, &tmo) == 0); + So(nng_getopt_usec(s1, nng_optid_recvtimeo, &tmo) == 0); So(tmo == 300000); + Convey("Polyamorous option id works", { + // This test has to be done after polyamorous mode + // is registered! + int poly; + poly = nng_option_lookup(nng_opt_pair1_poly); + So(poly >= 0); + So(poly == nng_optid_pair1_poly); + So(nng_option_name(poly) != 0); + So(strcmp(nng_option_name(poly), nng_opt_pair1_poly) == + 0); + }); + Convey("Monogamous cooked mode works", { nng_msg *msg; @@ -98,26 +113,22 @@ TestMain("PAIRv1 protocol", { So(nng_dial(c1, addr, NULL, 0) == 0); nng_usleep(100000); - So(nng_setopt_int(s1, NNG_OPT_RAW, 1) == NNG_ESTATE); - So(nng_setopt_int(c1, NNG_OPT_RAW, 1) == NNG_ESTATE); + So(nng_setopt_int(s1, nng_optid_raw, 1) == NNG_ESTATE); + So(nng_setopt_int(c1, nng_optid_raw, 1) == NNG_ESTATE); }); Convey("Polyamorous mode is best effort", { int rv; int i; nng_msg *msg; - int poly; - poly = nng_option_lookup("polyamorous"); - So(poly >= 0); - So(nng_option_name(poly) != NULL); - So(strcmp(nng_option_name(poly), "polyamorous") == 0); - So(nng_setopt_int(s1, poly, 1) == 0); + So(nng_setopt_int(s1, nng_optid_pair1_poly, 1) == 0); - So(nng_setopt_int(s1, NNG_OPT_RCVBUF, 1) == 0); - So(nng_setopt_int(s1, NNG_OPT_SNDBUF, 1) == 0); - So(nng_setopt_int(c1, NNG_OPT_RCVBUF, 1) == 0); - So(nng_setopt_usec(s1, NNG_OPT_SNDTIMEO, 100000) == 0); + So(nng_setopt_int(s1, nng_optid_recvbuf, 1) == 0); + So(nng_setopt_int(s1, nng_optid_sendbuf, 1) == 0); + So(nng_setopt_int(c1, nng_optid_recvbuf, 1) == 0); + So(nng_setopt_usec(s1, nng_optid_sendtimeo, 100000) == + 0); So(nng_listen(s1, addr, NULL, 0) == 0); So(nng_dial(c1, addr, NULL, 0) == 0); @@ -138,10 +149,11 @@ TestMain("PAIRv1 protocol", { int rv; nng_msg *msg; - So(nng_setopt_int(s1, NNG_OPT_RCVBUF, 1) == 0); - So(nng_setopt_int(s1, NNG_OPT_SNDBUF, 1) == 0); - So(nng_setopt_int(c1, NNG_OPT_RCVBUF, 1) == 0); - So(nng_setopt_usec(s1, NNG_OPT_SNDTIMEO, 30000) == 0); + So(nng_setopt_int(s1, nng_optid_recvbuf, 1) == 0); + So(nng_setopt_int(s1, nng_optid_sendbuf, 1) == 0); + So(nng_setopt_int(c1, nng_optid_recvbuf, 1) == 0); + So(nng_setopt_usec(s1, nng_optid_sendtimeo, 30000) == + 0); So(nng_listen(s1, addr, NULL, 0) == 0); So(nng_dial(c1, addr, NULL, 0) == 0); @@ -164,21 +176,18 @@ TestMain("PAIRv1 protocol", { So(nng_listen(s1, addr, NULL, 0) == 0); So(nng_dial(c1, addr, NULL, 0) == 0); nng_usleep(100000); - poly = nng_option_lookup("polyamorous"); - So(poly >= 0); - So(nng_option_name(poly) != NULL); - So(strcmp(nng_option_name(poly), "polyamorous") == 0); - So(nng_setopt_int(s1, poly, 1) == NNG_ESTATE); + So(nng_setopt_int(s1, nng_optid_pair1_poly, 1) == + NNG_ESTATE); }); Convey("Monogamous raw mode works", { nng_msg *msg; uint32_t hops; - So(nng_setopt_int(s1, NNG_OPT_RAW, 1) == 0); - So(nng_setopt_int(c1, NNG_OPT_RAW, 1) == 0); - So(nng_setopt_int(c2, NNG_OPT_RAW, 1) == 0); + So(nng_setopt_int(s1, nng_optid_raw, 1) == 0); + So(nng_setopt_int(c1, nng_optid_raw, 1) == 0); + So(nng_setopt_int(c2, nng_optid_raw, 1) == 0); So(nng_listen(s1, addr, NULL, 0) == 0); So(nng_dial(c1, addr, NULL, 0) == 0); @@ -255,9 +264,10 @@ TestMain("PAIRv1 protocol", { Convey("TTL is honored", { int ttl; - So(nng_setopt_int(s1, NNG_OPT_MAXTTL, 4) == 0); - So(nng_getopt_int(s1, NNG_OPT_MAXTTL, &ttl) == + So(nng_setopt_int(s1, nng_optid_maxttl, 4) == 0); + So(nng_getopt_int( + s1, nng_optid_maxttl, &ttl) == 0); So(ttl == 4); Convey("Bad TTL bounces", { So(nng_msg_alloc(&msg, 0) == 0); @@ -285,8 +295,8 @@ TestMain("PAIRv1 protocol", { Convey("Large TTL passes", { ttl = 0xff; - So(nng_setopt_int( - s1, NNG_OPT_MAXTTL, 0xff) == 0); + So(nng_setopt_int(s1, nng_optid_maxttl, + 0xff) == 0); So(nng_msg_alloc(&msg, 0) == 0); So(nng_msg_append_u32(msg, 1234) == 0); So(nng_msg_header_append_u32( @@ -303,8 +313,8 @@ TestMain("PAIRv1 protocol", { Convey("Max TTL fails", { ttl = 0xff; - So(nng_setopt_int( - s1, NNG_OPT_MAXTTL, 0xff) == 0); + So(nng_setopt_int(s1, nng_optid_maxttl, + 0xff) == 0); So(nng_msg_alloc(&msg, 0) == 0); So(nng_msg_header_append_u32( msg, 0xff) == 0); @@ -319,15 +329,15 @@ TestMain("PAIRv1 protocol", { int ttl; ttl = 0; - So(nng_setopt_int(s1, NNG_OPT_MAXTTL, 0) == + So(nng_setopt_int(s1, nng_optid_maxttl, 0) == NNG_EINVAL); - So(nng_setopt_int(s1, NNG_OPT_MAXTTL, 1000) == + So(nng_setopt_int(s1, nng_optid_maxttl, 1000) == NNG_EINVAL); sz = 1; ttl = 8; - So(nng_setopt(s1, NNG_OPT_MAXTTL, &ttl, sz) == + So(nng_setopt(s1, nng_optid_maxttl, &ttl, sz) == NNG_EINVAL); }); @@ -336,18 +346,12 @@ TestMain("PAIRv1 protocol", { int v; nng_pipe p1; nng_pipe p2; - int poly; - poly = nng_option_lookup("polyamorous"); - So(poly >= 0); - So(nng_option_name(poly) != NULL); - So(strcmp(nng_option_name(poly), "polyamorous") == 0); - - So(nng_getopt_int(s1, poly, &v) == 0); + So(nng_getopt_int(s1, nng_optid_pair1_poly, &v) == 0); So(v == 0); - So(nng_setopt_int(s1, poly, 1) == 0); - So(nng_getopt_int(s1, poly, &v) == 0); + So(nng_setopt_int(s1, nng_optid_pair1_poly, 1) == 0); + So(nng_getopt_int(s1, nng_optid_pair1_poly, &v) == 0); So(v == 1); So(nng_listen(s1, addr, NULL, 0) == 0); @@ -402,14 +406,8 @@ TestMain("PAIRv1 protocol", { Convey("Polyamorous default works", { nng_msg *msg; - int poly; - - poly = nng_option_lookup("polyamorous"); - So(poly >= 0); - So(nng_option_name(poly) != NULL); - So(strcmp(nng_option_name(poly), "polyamorous") == 0); - So(nng_setopt_int(s1, poly, 1) == 0); + So(nng_setopt_int(s1, nng_optid_pair1_poly, 1) == 0); So(nng_listen(s1, addr, NULL, 0) == 0); So(nng_dial(c1, addr, NULL, 0) == 0); @@ -440,23 +438,17 @@ TestMain("PAIRv1 protocol", { uint32_t hops; nng_pipe p1; nng_pipe p2; - int poly; - - poly = nng_option_lookup("polyamorous"); - So(poly >= 0); - So(nng_option_name(poly) != NULL); - So(strcmp(nng_option_name(poly), "polyamorous") == 0); - So(nng_getopt_int(s1, poly, &v) == 0); + So(nng_getopt_int(s1, nng_optid_pair1_poly, &v) == 0); So(v == 0); - So(nng_setopt_int(s1, poly, 1) == 0); - So(nng_getopt_int(s1, poly, &v) == 0); + So(nng_setopt_int(s1, nng_optid_pair1_poly, 1) == 0); + So(nng_getopt_int(s1, nng_optid_pair1_poly, &v) == 0); So(v == 1); v = 0; - So(nng_setopt_int(s1, NNG_OPT_RAW, 1) == 0); - So(nng_getopt_int(s1, NNG_OPT_RAW, &v) == 0); + So(nng_setopt_int(s1, nng_optid_raw, 1) == 0); + So(nng_getopt_int(s1, nng_optid_raw, &v) == 0); So(v == 1); So(nng_listen(s1, addr, NULL, 0) == 0); diff --git a/tests/pipeline.c b/tests/pipeline.c index 70b52350..78002fc6 100644 --- a/tests/pipeline.c +++ b/tests/pipeline.c @@ -16,172 +16,155 @@ So(nng_msg_len(m) == strlen(s)); \ So(memcmp(nng_msg_body(m), s, strlen(s)) == 0) -Main({ +TestMain("PIPELINE (PUSH/PULL) pattern", { + atexit(nng_fini); const char *addr = "inproc://test"; + Convey("We can create a PUSH socket", { + nng_socket push; - Test("PIPELINE (PUSH/PULL) pattern", { - Convey("We can create a PUSH socket", { - nng_socket push; + So(nng_push_open(&push) == 0); - So(nng_push_open(&push) == 0); + Reset({ nng_close(push); }); - Reset({ nng_close(push); }); - - Convey("Protocols match", { - So(nng_protocol(push) == NNG_PROTO_PUSH); - So(nng_peer(push) == NNG_PROTO_PULL); - }); + Convey("Protocols match", { + So(nng_protocol(push) == NNG_PROTO_PUSH); + So(nng_peer(push) == NNG_PROTO_PULL); + }); - Convey("Recv fails", { - nng_msg *msg; - So(nng_recvmsg(push, &msg, 0) == NNG_ENOTSUP); - }); + Convey("Recv fails", { + nng_msg *msg; + So(nng_recvmsg(push, &msg, 0) == NNG_ENOTSUP); }); + }); - Convey("We can create a PULL socket", { - nng_socket pull; - So(nng_pull_open(&pull) == 0); + Convey("We can create a PULL socket", { + nng_socket pull; + So(nng_pull_open(&pull) == 0); - Reset({ nng_close(pull); }); + Reset({ nng_close(pull); }); - Convey("Protocols match", { - So(nng_protocol(pull) == NNG_PROTO_PULL); - So(nng_peer(pull) == NNG_PROTO_PUSH); - }); + Convey("Protocols match", { + So(nng_protocol(pull) == NNG_PROTO_PULL); + So(nng_peer(pull) == NNG_PROTO_PUSH); + }); - Convey("Send fails", { - nng_msg *msg; - So(nng_msg_alloc(&msg, 0) == 0); - So(nng_sendmsg(pull, msg, 0) == NNG_ENOTSUP); - nng_msg_free(msg); - }); + Convey("Send fails", { + nng_msg *msg; + So(nng_msg_alloc(&msg, 0) == 0); + So(nng_sendmsg(pull, msg, 0) == NNG_ENOTSUP); + nng_msg_free(msg); }); + }); - Convey("We can create a linked PUSH/PULL pair", { - nng_socket push; - nng_socket pull; - nng_socket what; - - So(nng_push_open(&push) == 0); - So(nng_pull_open(&pull) == 0); - So(nng_push_open(&what) == 0); - - Reset({ - nng_close(push); - nng_close(pull); - nng_close(what); - }); - - // Its important to avoid a startup race that the - // sender be the dialer. Otherwise you need a delay - // since the server accept is really asynchronous. - So(nng_listen(pull, addr, NULL, 0) == 0); - So(nng_dial(push, addr, NULL, 0) == 0); - So(nng_dial(what, addr, NULL, 0) == 0); - So(nng_shutdown(what) == 0); - - Convey("Push can send messages, and pull can recv", { - nng_msg *msg; - - So(nng_msg_alloc(&msg, 0) == 0); - APPENDSTR(msg, "hello"); - So(nng_sendmsg(push, msg, 0) == 0); - msg = NULL; - So(nng_recvmsg(pull, &msg, 0) == 0); - So(msg != NULL); - CHECKSTR(msg, "hello"); - nng_msg_free(msg); - }); + Convey("We can create a linked PUSH/PULL pair", { + nng_socket push; + nng_socket pull; + nng_socket what; + + So(nng_push_open(&push) == 0); + So(nng_pull_open(&pull) == 0); + So(nng_push_open(&what) == 0); + + Reset({ + nng_close(push); + nng_close(pull); + nng_close(what); }); - Convey("Load balancing", { - nng_msg * abc; - nng_msg * def; - uint64_t usecs; - int len; - nng_socket push; - nng_socket pull1; - nng_socket pull2; - nng_socket pull3; - - So(nng_push_open(&push) == 0); - So(nng_pull_open(&pull1) == 0); - So(nng_pull_open(&pull2) == 0); - So(nng_pull_open(&pull3) == 0); - - Reset({ - nng_close(push); - nng_close(pull1); - nng_close(pull2); - nng_close(pull3); - }); - - // We need to increase the buffer from zero, because - // there is no guarantee that the various listeners - // will be present, which means that they will push - // back during load balancing. Adding a small buffer - // ensures that we can write to each stream, even if - // the listeners are not running yet. - len = 4; - So(nng_setopt( - push, NNG_OPT_RCVBUF, &len, sizeof(len)) == 0); - So(nng_setopt( - push, NNG_OPT_SNDBUF, &len, sizeof(len)) == 0); - So(nng_setopt( - pull1, NNG_OPT_RCVBUF, &len, sizeof(len)) == 0); - So(nng_setopt( - pull1, NNG_OPT_SNDBUF, &len, sizeof(len)) == 0); - So(nng_setopt( - pull2, NNG_OPT_RCVBUF, &len, sizeof(len)) == 0); - So(nng_setopt( - pull2, NNG_OPT_SNDBUF, &len, sizeof(len)) == 0); - So(nng_setopt( - pull3, NNG_OPT_RCVBUF, &len, sizeof(len)) == 0); - So(nng_setopt( - pull3, NNG_OPT_SNDBUF, &len, sizeof(len)) == 0); - - So(nng_msg_alloc(&abc, 0) == 0); - APPENDSTR(abc, "abc"); - So(nng_msg_alloc(&def, 0) == 0); - APPENDSTR(def, "def"); - - usecs = 100000; - So(nng_setopt(pull1, NNG_OPT_RCVTIMEO, &usecs, - sizeof(usecs)) == 0); - So(nng_setopt(pull2, NNG_OPT_RCVTIMEO, &usecs, - sizeof(usecs)) == 0); - So(nng_setopt(pull3, NNG_OPT_RCVTIMEO, &usecs, - sizeof(usecs)) == 0); - So(nng_listen(push, addr, NULL, 0) == 0); - So(nng_dial(pull1, addr, NULL, 0) == 0); - So(nng_dial(pull2, addr, NULL, 0) == 0); - So(nng_dial(pull3, addr, NULL, 0) == 0); - So(nng_shutdown(pull3) == 0); - - // So pull3 might not be done accepting yet, but pull1 - // and pull2 definitely are, because otherwise the - // server couldn't have gotten to the accept. (The - // accept logic is single threaded.) Let's wait a bit - // though, to ensure that stuff has settled. - nng_usleep(100000); - - So(nng_sendmsg(push, abc, 0) == 0); - So(nng_sendmsg(push, def, 0) == 0); - - abc = NULL; - def = NULL; - - So(nng_recvmsg(pull1, &abc, 0) == 0); - CHECKSTR(abc, "abc"); - So(nng_recvmsg(pull2, &def, 0) == 0); - CHECKSTR(def, "def"); - nng_msg_free(abc); - nng_msg_free(def); - - So(nng_recvmsg(pull1, &abc, 0) == NNG_ETIMEDOUT); - So(nng_recvmsg(pull2, &abc, 0) == NNG_ETIMEDOUT); + // Its important to avoid a startup race that the + // sender be the dialer. Otherwise you need a delay + // since the server accept is really asynchronous. + So(nng_listen(pull, addr, NULL, 0) == 0); + So(nng_dial(push, addr, NULL, 0) == 0); + So(nng_dial(what, addr, NULL, 0) == 0); + So(nng_shutdown(what) == 0); + + Convey("Push can send messages, and pull can recv", { + nng_msg *msg; + + So(nng_msg_alloc(&msg, 0) == 0); + APPENDSTR(msg, "hello"); + So(nng_sendmsg(push, msg, 0) == 0); + msg = NULL; + So(nng_recvmsg(pull, &msg, 0) == 0); + So(msg != NULL); + CHECKSTR(msg, "hello"); + nng_msg_free(msg); }); }); - nng_fini(); -}) + Convey("Load balancing", { + nng_msg * abc; + nng_msg * def; + uint64_t usecs; + nng_socket push; + nng_socket pull1; + nng_socket pull2; + nng_socket pull3; + + So(nng_push_open(&push) == 0); + So(nng_pull_open(&pull1) == 0); + So(nng_pull_open(&pull2) == 0); + So(nng_pull_open(&pull3) == 0); + + Reset({ + nng_close(push); + nng_close(pull1); + nng_close(pull2); + nng_close(pull3); + }); + + // We need to increase the buffer from zero, because + // there is no guarantee that the various listeners + // will be present, which means that they will push + // back during load balancing. Adding a small buffer + // ensures that we can write to each stream, even if + // the listeners are not running yet. + So(nng_setopt_int(push, nng_optid_recvbuf, 4) == 0); + So(nng_setopt_int(push, nng_optid_sendbuf, 4) == 0); + So(nng_setopt_int(pull1, nng_optid_recvbuf, 4) == 0); + So(nng_setopt_int(pull1, nng_optid_sendbuf, 4) == 0); + So(nng_setopt_int(pull2, nng_optid_recvbuf, 4) == 0); + So(nng_setopt_int(pull2, nng_optid_sendbuf, 4) == 0); + So(nng_setopt_int(pull3, nng_optid_recvbuf, 4) == 0); + So(nng_setopt_int(pull3, nng_optid_sendbuf, 4) == 0); + + So(nng_msg_alloc(&abc, 0) == 0); + APPENDSTR(abc, "abc"); + So(nng_msg_alloc(&def, 0) == 0); + APPENDSTR(def, "def"); + + usecs = 100000; + So(nng_setopt_usec(pull1, nng_optid_recvtimeo, usecs) == 0); + So(nng_setopt_usec(pull2, nng_optid_recvtimeo, usecs) == 0); + So(nng_setopt_usec(pull3, nng_optid_recvtimeo, usecs) == 0); + So(nng_listen(push, addr, NULL, 0) == 0); + So(nng_dial(pull1, addr, NULL, 0) == 0); + So(nng_dial(pull2, addr, NULL, 0) == 0); + So(nng_dial(pull3, addr, NULL, 0) == 0); + So(nng_shutdown(pull3) == 0); + + // So pull3 might not be done accepting yet, but pull1 + // and pull2 definitely are, because otherwise the + // server couldn't have gotten to the accept. (The + // accept logic is single threaded.) Let's wait a bit + // though, to ensure that stuff has settled. + nng_usleep(100000); + + So(nng_sendmsg(push, abc, 0) == 0); + So(nng_sendmsg(push, def, 0) == 0); + + abc = NULL; + def = NULL; + + So(nng_recvmsg(pull1, &abc, 0) == 0); + CHECKSTR(abc, "abc"); + So(nng_recvmsg(pull2, &def, 0) == 0); + CHECKSTR(def, "def"); + nng_msg_free(abc); + nng_msg_free(def); + + So(nng_recvmsg(pull1, &abc, 0) == NNG_ETIMEDOUT); + So(nng_recvmsg(pull2, &abc, 0) == NNG_ETIMEDOUT); + }); +}); diff --git a/tests/pollfd.c b/tests/pollfd.c index 05ed2939..6cf712f1 100644 --- a/tests/pollfd.c +++ b/tests/pollfd.c @@ -1,5 +1,6 @@ // // 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 @@ -10,6 +11,8 @@ #include "convey.h" #include "nng.h" +#include + #ifndef _WIN32 #include #include @@ -29,102 +32,120 @@ #endif -TestMain("Poll FDs", - { - - Convey("Given a connected pair of sockets", { - nng_socket s1; - nng_socket s2; - - So(nng_pair_open(&s1) == 0); - So(nng_pair_open(&s2) == 0); - Reset({ - nng_close(s1); - nng_close(s2); - }); - So(nng_listen(s1, "inproc://yeahbaby", NULL, 0) == 0); - nng_usleep(50000); - - So(nng_dial(s2, "inproc://yeahbaby", NULL, 0) == 0); - nng_usleep(50000); - - Convey("We can get a recv FD", { - int fd; - size_t sz; - - sz = sizeof(fd); - So(nng_getopt(s1, NNG_OPT_RCVFD, &fd, &sz) == 0); - So(fd != INVALID_SOCKET); - - Convey("And it is always the same fd", { - int fd2; - sz = sizeof(fd2); - So(nng_getopt(s1, NNG_OPT_RCVFD, &fd2, &sz) == - 0); - So(fd2 == fd); - }); - - Convey("And they start non pollable", { - struct pollfd pfd; - pfd.fd = fd; - pfd.events = POLLIN; - pfd.revents = 0; - - So(poll(&pfd, 1, 0) == 0); - So(pfd.revents == 0); - }); - - Convey("But if we write they are pollable", { - struct pollfd pfd; - pfd.fd = fd; - pfd.events = POLLIN; - pfd.revents = 0; - - So(nng_send(s2, "kick", 5, 0) == 0); - So(poll(&pfd, 1, 1000) == 1); - So((pfd.revents & POLLIN) != 0); - }); - }); - - Convey("We can get a send FD", { - int fd; - size_t sz; - - sz = sizeof(fd); - So(nng_getopt(s1, NNG_OPT_SNDFD, &fd, &sz) == 0); - So(fd != INVALID_SOCKET); - So(nng_send(s1, "oops", 4, 0) == 0); - }); - - Convey("Must have a big enough size", { - int fd; - size_t sz; - sz = 1; - So(nng_getopt(s1, NNG_OPT_RCVFD, &fd, &sz) == - NNG_EINVAL); - sz = 128; - So(nng_getopt(s1, NNG_OPT_RCVFD, &fd, &sz) == 0); - So(sz == sizeof(fd)); - }); - Convey("We cannot get a send FD for PULL", { - nng_socket s3; - int fd; - size_t sz; - So(nng_pull_open(&s3) == 0); - Reset({ nng_close(s3); }); - sz = sizeof(fd); - So(nng_getopt(s3, NNG_OPT_SNDFD, &fd, &sz) == - NNG_ENOTSUP); - }); - - Convey("We cannot get a recv FD for PUSH", { - nng_socket s3; - int fd; - size_t sz; - So(nng_push_open(&s3) == 0); - Reset({ nng_close(s3); }); - sz = sizeof(fd); - So(nng_getopt(s3, NNG_OPT_RCVFD, &fd, &sz) == - NNG_ENOTSUP); - }); - }) }) +TestMain("Poll FDs", { + + Convey("Option values work", { + int opt; + const char *name; + + opt = nng_option_lookup(nng_opt_recvfd); + So(opt > 0); + So(opt == nng_optid_recvfd); + name = nng_option_name(opt); + So(name != NULL); + So(strcmp(name, nng_opt_recvfd) == 0); + opt = nng_option_lookup(nng_opt_sendfd); + So(opt > 0); + So(opt == nng_optid_sendfd); + name = nng_option_name(opt); + So(name != NULL); + So(strcmp(name, nng_opt_sendfd) == 0); + }); + + Convey("Given a connected pair of sockets", { + nng_socket s1; + nng_socket s2; + + So(nng_pair_open(&s1) == 0); + So(nng_pair_open(&s2) == 0); + Reset({ + nng_close(s1); + nng_close(s2); + }); + So(nng_listen(s1, "inproc://yeahbaby", NULL, 0) == 0); + nng_usleep(50000); + + So(nng_dial(s2, "inproc://yeahbaby", NULL, 0) == 0); + nng_usleep(50000); + + Convey("We can get a recv FD", { + int fd; + size_t sz; + + sz = sizeof(fd); + So(nng_getopt(s1, nng_optid_recvfd, &fd, &sz) == 0); + So(fd != INVALID_SOCKET); + + Convey("And it is always the same fd", { + int fd2; + sz = sizeof(fd2); + So(nng_getopt( + s1, nng_optid_recvfd, &fd2, &sz) == 0); + So(fd2 == fd); + }); + + Convey("And they start non pollable", { + struct pollfd pfd; + pfd.fd = fd; + pfd.events = POLLIN; + pfd.revents = 0; + + So(poll(&pfd, 1, 0) == 0); + So(pfd.revents == 0); + }); + + Convey("But if we write they are pollable", { + struct pollfd pfd; + pfd.fd = fd; + pfd.events = POLLIN; + pfd.revents = 0; + + So(nng_send(s2, "kick", 5, 0) == 0); + So(poll(&pfd, 1, 1000) == 1); + So((pfd.revents & POLLIN) != 0); + }); + }); + + Convey("We can get a send FD", { + int fd; + size_t sz; + + sz = sizeof(fd); + So(nng_getopt(s1, nng_optid_sendfd, &fd, &sz) == 0); + So(fd != INVALID_SOCKET); + So(nng_send(s1, "oops", 4, 0) == 0); + }); + + Convey("Must have a big enough size", { + int fd; + size_t sz; + sz = 1; + So(nng_getopt(s1, nng_optid_recvfd, &fd, &sz) == + NNG_EINVAL); + sz = 128; + So(nng_getopt(s1, nng_optid_recvfd, &fd, &sz) == 0); + So(sz == sizeof(fd)); + }); + Convey("We cannot get a send FD for PULL", { + nng_socket s3; + int fd; + size_t sz; + So(nng_pull_open(&s3) == 0); + Reset({ nng_close(s3); }); + sz = sizeof(fd); + So(nng_getopt(s3, nng_optid_sendfd, &fd, &sz) == + NNG_ENOTSUP); + }); + + Convey("We cannot get a recv FD for PUSH", { + nng_socket s3; + int fd; + size_t sz; + So(nng_push_open(&s3) == 0); + Reset({ nng_close(s3); }); + sz = sizeof(fd); + So(nng_getopt(s3, nng_optid_recvfd, &fd, &sz) == + NNG_ENOTSUP); + }); + }); +}) diff --git a/tests/pubsub.c b/tests/pubsub.c index 2712181d..de2fcfc5 100644 --- a/tests/pubsub.c +++ b/tests/pubsub.c @@ -83,35 +83,35 @@ TestMain("PUB/SUB pattern", { So(nng_dial(pub, addr, NULL, 0) == 0); Convey("Sub can subscribe", { - So(nng_setopt(sub, NNG_OPT_SUBSCRIBE, "ABC", 3) == 0); - So(nng_setopt(sub, NNG_OPT_SUBSCRIBE, "", 0) == 0); + So(nng_setopt( + sub, nng_optid_sub_subscribe, "ABC", 3) == 0); + So(nng_setopt(sub, nng_optid_sub_subscribe, "", 0) == + 0); Convey("Unsubscribe works", { - So(nng_setopt(sub, NNG_OPT_UNSUBSCRIBE, "ABC", - 3) == 0); - So(nng_setopt( - sub, NNG_OPT_UNSUBSCRIBE, "", 0) == 0); - - So(nng_setopt(sub, NNG_OPT_UNSUBSCRIBE, "", - 0) == NNG_ENOENT); - So(nng_setopt(sub, NNG_OPT_UNSUBSCRIBE, + So(nng_setopt(sub, nng_optid_sub_unsubscribe, + "ABC", 3) == 0); + So(nng_setopt(sub, nng_optid_sub_unsubscribe, + "", 0) == 0); + + So(nng_setopt(sub, nng_optid_sub_unsubscribe, + "", 0) == NNG_ENOENT); + So(nng_setopt(sub, nng_optid_sub_unsubscribe, "HELLO", 0) == NNG_ENOENT); }); }); Convey("Pub cannot subscribe", { - So(nng_setopt(pub, NNG_OPT_SUBSCRIBE, "", 0) == + So(nng_setopt(pub, nng_optid_sub_subscribe, "", 0) == NNG_ENOTSUP); }); Convey("Subs can receive from pubs", { nng_msg *msg; - uint64_t rtimeo; - So(nng_setopt(sub, NNG_OPT_SUBSCRIBE, "/some/", + So(nng_setopt(sub, nng_optid_sub_subscribe, "/some/", strlen("/some/")) == 0); - rtimeo = 50000; // 50ms - So(nng_setopt(sub, NNG_OPT_RCVTIMEO, &rtimeo, - sizeof(rtimeo)) == 0); + So(nng_setopt_usec(sub, nng_optid_recvtimeo, 90000) == + 0); So(nng_msg_alloc(&msg, 0) == 0); APPENDSTR(msg, "/some/like/it/hot"); @@ -139,10 +139,9 @@ TestMain("PUB/SUB pattern", { Convey("Subs without subsciptions don't receive", { - uint64_t rtimeo = 50000; // 50ms nng_msg *msg; - So(nng_setopt(sub, NNG_OPT_RCVTIMEO, &rtimeo, - sizeof(rtimeo)) == 0); + So(nng_setopt_usec(sub, nng_optid_recvtimeo, 90000) == + 0); So(nng_msg_alloc(&msg, 0) == 0); APPENDSTR(msg, "/some/don't/like/it"); @@ -152,14 +151,11 @@ TestMain("PUB/SUB pattern", { Convey("Subs in raw receive", { - uint64_t rtimeo = 50000; // 500ms - int raw = 1; nng_msg *msg; - So(nng_setopt(sub, NNG_OPT_RCVTIMEO, &rtimeo, - sizeof(rtimeo)) == 0); - So(nng_setopt(sub, NNG_OPT_RAW, &raw, sizeof(raw)) == + So(nng_setopt_usec(sub, nng_optid_recvtimeo, 90000) == 0); + So(nng_setopt_int(sub, nng_optid_raw, 1) == 0); So(nng_msg_alloc(&msg, 0) == 0); APPENDSTR(msg, "/some/like/it/raw"); diff --git a/tests/reqrep.c b/tests/reqrep.c index e36232b6..537d45c8 100644 --- a/tests/reqrep.c +++ b/tests/reqrep.c @@ -1,5 +1,6 @@ // // 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 @@ -12,6 +13,9 @@ #include +extern const char *nng_opt_req_resendtime; +extern int nng_optid_req_resendtime; + TestMain("REQ/REP pattern", { int rv; const char *addr = "inproc://test"; @@ -27,6 +31,22 @@ TestMain("REQ/REP pattern", { So(nng_peer(req) == NNG_PROTO_REP); }); + Convey("Resend time option id works", { + int opt; + const char *name; + opt = nng_option_lookup(nng_opt_req_resendtime); + So(opt >= 0); + So(opt == nng_optid_req_resendtime); + name = nng_option_name(opt); + So(name != NULL); + So(strcmp(name, nng_opt_req_resendtime) == 0); + + // Set timeout. + So(nng_setopt_usec(req, opt, 10000) == 0); + // Check invalid size + So(nng_setopt(req, opt, name, 1) == NNG_EINVAL); + }); + Convey("Recv with no send fails", { nng_msg *msg; rv = nng_recvmsg(req, &msg, 0); @@ -53,6 +73,11 @@ TestMain("REQ/REP pattern", { So(rv == NNG_ESTATE); nng_msg_free(msg); }); + + Convey("Cannot set resend time", { + So(nng_setopt_usec(rep, nng_optid_req_resendtime, + 100) == NNG_ENOTSUP); + }); }); Convey("We can create a linked REQ/REP pair", { @@ -115,8 +140,8 @@ TestMain("REQ/REP pattern", { nng_close(req); }); - So(nng_setopt_usec(req, NNG_OPT_RESENDTIME, retry) == 0); - So(nng_setopt_int(req, NNG_OPT_SNDBUF, 16) == 0); + So(nng_setopt_usec(req, nng_optid_req_resendtime, retry) == 0); + So(nng_setopt_int(req, nng_optid_sendbuf, 16) == 0); So(nng_msg_alloc(&abc, 0) == 0); So(nng_msg_append(abc, "abc", 4) == 0); diff --git a/tests/scalability.c b/tests/scalability.c index 0bb93401..f0fc6366 100644 --- a/tests/scalability.c +++ b/tests/scalability.c @@ -52,29 +52,15 @@ openclients(nng_socket *clients, int num) int i; uint64_t t; for (i = 0; i < num; i++) { - if ((rv = nng_req_open(&clients[i])) != 0) { - printf("open #%d: %s\n", i, nng_strerror(rv)); - return (rv); - } - t = 100000; // 100ms - rv = nng_setopt(clients[i], NNG_OPT_RCVTIMEO, &t, sizeof(t)); - if (rv != 0) { - printf( - "setopt(RCVTIMEO) #%d: %s\n", i, nng_strerror(rv)); - return (rv); - } - t = 100000; // 100ms - rv = nng_setopt(clients[i], NNG_OPT_SNDTIMEO, &t, sizeof(t)); - if (rv != 0) { - printf( - "setopt(SNDTIMEO) #%d: %s\n", i, nng_strerror(rv)); - return (rv); - } - rv = nng_dial(clients[i], addr, NULL, 0); - if (rv != 0) { - printf("dial #%d: %s\n", i, nng_strerror(rv)); + t = 100000; // 100ms + nng_socket c; + if (((rv = nng_req_open(&c)) != 0) || + ((rv = nng_setopt_usec(c, nng_optid_recvtimeo, t)) != 0) || + ((rv = nng_setopt_usec(c, nng_optid_sendtimeo, t)) != 0) || + ((rv = nng_dial(c, addr, NULL, 0)) != 0)) { return (rv); } + clients[i] = c; } return (0); } @@ -88,31 +74,22 @@ transact(nng_socket *clients, int num) for (i = 0; i < num; i++) { - if ((rv = nng_msg_alloc(&msg, 0)) != 0) { - break; - } - - if ((rv = nng_sendmsg(clients[i], msg, 0)) != 0) { - break; - } - - msg = NULL; - if ((rv = nng_recvmsg(clients[i], &msg, 0)) != 0) { + if (((rv = nng_msg_alloc(&msg, 0)) != 0) || + ((rv = nng_sendmsg(clients[i], msg, 0)) != 0) || + ((rv = nng_recvmsg(clients[i], &msg, 0)) != 0)) { + // We may leak a message, but this is an + // error case anyway. break; } nng_msg_free(msg); msg = NULL; } - if (msg != NULL) { - nng_msg_free(msg); - } return (rv); } Main({ nng_socket *clients; int * results; - int depth = 256; atexit(stop); @@ -120,8 +97,8 @@ Main({ results = calloc(nclients, sizeof(int)); if ((nng_rep_open(&rep) != 0) || - (nng_setopt(rep, NNG_OPT_RCVBUF, &depth, sizeof(depth)) != 0) || - (nng_setopt(rep, NNG_OPT_SNDBUF, &depth, sizeof(depth)) != 0) || + (nng_setopt_int(rep, nng_optid_recvbuf, 256) != 0) || + (nng_setopt_int(rep, nng_optid_sendbuf, 256) != 0) || (nng_listen(rep, addr, NULL, 0) != 0) || (nng_thread_create(&server, serve, NULL) != 0)) { fprintf(stderr, "Unable to set up server!\n"); diff --git a/tests/sock.c b/tests/sock.c index 2e2345c9..81c0eb59 100644 --- a/tests/sock.c +++ b/tests/sock.c @@ -1,5 +1,6 @@ // // 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 @@ -63,7 +64,7 @@ TestMain("Socket Operations", { now = nng_clock(); So(now > 0); - So(nng_setopt_usec(s1, NNG_OPT_RCVTIMEO, to) == 0); + So(nng_setopt_usec(s1, nng_optid_recvtimeo, to) == 0); So(nng_recvmsg(s1, &msg, 0) == NNG_ETIMEDOUT); So(msg == NULL); So(nng_clock() >= (now + to)); @@ -87,7 +88,7 @@ TestMain("Socket Operations", { So(msg != NULL); now = nng_clock(); - So(nng_setopt_usec(s1, NNG_OPT_SNDTIMEO, to) == 0); + So(nng_setopt_usec(s1, nng_optid_sendtimeo, to) == 0); So(nng_sendmsg(s1, msg, 0) == NNG_ETIMEDOUT); So(nng_clock() >= (now + to)); So(nng_clock() < (now + (to * 2))); @@ -99,17 +100,17 @@ TestMain("Socket Operations", { int64_t v = 0; size_t sz; - So(nng_setopt_usec(s1, NNG_OPT_SNDTIMEO, to) == 0); + So(nng_setopt_usec(s1, nng_optid_sendtimeo, to) == 0); Convey("Read only options handled properly", { - So(nng_setopt_int(s1, NNG_OPT_RCVFD, 0) == + So(nng_setopt_int(s1, nng_optid_recvfd, 0) == NNG_EINVAL); - So(nng_setopt_int(s1, NNG_OPT_SNDFD, 0) == + So(nng_setopt_int(s1, nng_optid_sendfd, 0) == NNG_EINVAL); - So(nng_setopt(s1, NNG_OPT_LOCALADDR, "a", 1) == + So(nng_setopt(s1, nng_optid_locaddr, "a", 1) == + NNG_EINVAL); + So(nng_setopt(s1, nng_optid_remaddr, "a", 1) == NNG_EINVAL); - So(nng_setopt(s1, NNG_OPT_REMOTEADDR, "a", - 1) == NNG_EINVAL); }); Convey("We can apply options before endpoint", { @@ -119,51 +120,51 @@ TestMain("Socket Operations", { addr, "ipc:///tmp/lopt_%u"); So(nng_setopt_size( - s1, NNG_OPT_RCVMAXSZ, 543) == 0); + s1, nng_optid_recvmaxsz, 543) == 0); So(nng_listener_create(&l, s1, addr) == 0); So(nng_listener_getopt_size( - l, NNG_OPT_RCVMAXSZ, &sz) == 0); + l, nng_optid_recvmaxsz, &sz) == 0); So(sz == 543); Convey("Endpoint option can be overridden", { - So(nng_listener_setopt_size( - l, NNG_OPT_RCVMAXSZ, 678) == 0); - So(nng_listener_getopt_size( - l, NNG_OPT_RCVMAXSZ, &sz) == 0); + So(nng_listener_setopt_size(l, + nng_optid_recvmaxsz, 678) == 0); + So(nng_listener_getopt_size(l, + nng_optid_recvmaxsz, &sz) == 0); So(sz == 678); So(nng_getopt_size(s1, - NNG_OPT_RCVMAXSZ, &sz) == 0); + nng_optid_recvmaxsz, &sz) == 0); So(sz == 543); }); Convey("And socket overrides again", { So(nng_setopt_size(s1, - NNG_OPT_RCVMAXSZ, 911) == 0); - So(nng_listener_getopt_size( - l, NNG_OPT_RCVMAXSZ, &sz) == 0); + nng_optid_recvmaxsz, 911) == 0); + So(nng_listener_getopt_size(l, + nng_optid_recvmaxsz, &sz) == 0); So(sz == 911); }); }); Convey("Short size is not copied", { sz = 0; - So(nng_getopt(s1, NNG_OPT_SNDTIMEO, &v, &sz) == - 0); + So(nng_getopt( + s1, nng_optid_sendtimeo, &v, &sz) == 0); So(sz == sizeof(v)); So(v == 0); sz = 0; - So(nng_getopt( - s1, NNG_OPT_RECONN_TIME, &v, &sz) == 0); + So(nng_getopt(s1, nng_optid_reconnmint, &v, + &sz) == 0); So(v == 0); - So(nng_getopt(s1, NNG_OPT_RECONN_MAXTIME, &v, + So(nng_getopt(s1, nng_optid_reconnmaxt, &v, &sz) == 0); So(v == 0); }); Convey("Correct size is copied", { sz = sizeof(v); - So(nng_getopt(s1, NNG_OPT_SNDTIMEO, &v, &sz) == - 0); + So(nng_getopt( + s1, nng_optid_sendtimeo, &v, &sz) == 0); So(sz == sizeof(v)); So(v == 1234); }); @@ -171,48 +172,47 @@ TestMain("Socket Operations", { Convey("Short size buf is not copied", { int l = 5; sz = 0; - So(nng_getopt(s1, NNG_OPT_RCVBUF, &l, &sz) == - 0); + So(nng_getopt( + s1, nng_optid_recvbuf, &l, &sz) == 0); So(sz == sizeof(l)); So(l == 5); }); Convey("Insane buffer size fails", { - So(nng_setopt_int(s1, NNG_OPT_RCVBUF, + So(nng_setopt_int(s1, nng_optid_recvbuf, 0x100000) == NNG_EINVAL); - So(nng_setopt_int(s1, NNG_OPT_RCVBUF, -200) == - NNG_EINVAL); - + So(nng_setopt_int(s1, nng_optid_recvbuf, + -200) == NNG_EINVAL); }); Convey("Negative timeout fails", { - So(nng_setopt_usec(s1, NNG_OPT_RCVTIMEO, -5) == - NNG_EINVAL); + So(nng_setopt_usec(s1, nng_optid_recvtimeo, + -5) == NNG_EINVAL); }); Convey("Short timeout fails", { to = 0; sz = sizeof(to) - 1; - So(nng_setopt(s1, NNG_OPT_RCVTIMEO, &to, sz) == - NNG_EINVAL); - So(nng_setopt(s1, NNG_OPT_RECONN_TIME, &to, + So(nng_setopt(s1, nng_optid_recvtimeo, &to, + sz) == NNG_EINVAL); + So(nng_setopt(s1, nng_optid_reconnmint, &to, sz) == NNG_EINVAL); }); Convey("Bogus raw fails", { - So(nng_setopt_int(s1, NNG_OPT_RAW, 42) == + So(nng_setopt_int(s1, nng_optid_raw, 42) == NNG_EINVAL); - So(nng_setopt_int(s1, NNG_OPT_RAW, -42) == + So(nng_setopt_int(s1, nng_optid_raw, -42) == NNG_EINVAL); - So(nng_setopt_int(s1, NNG_OPT_RAW, 0) == 0); - So(nng_setopt(s1, NNG_OPT_RAW, "a", 1) == + So(nng_setopt_int(s1, nng_optid_raw, 0) == 0); + So(nng_setopt(s1, nng_optid_raw, "a", 1) == NNG_EINVAL); }); Convey("Unsupported options fail", { char *crap = "crap"; - So(nng_setopt(s1, NNG_OPT_SUBSCRIBE, crap, - strlen(crap)) == NNG_ENOTSUP); + So(nng_setopt(s1, nng_optid_sub_subscribe, + crap, strlen(crap)) == NNG_ENOTSUP); }); Convey("Bogus sizes fail", { @@ -220,30 +220,30 @@ TestMain("Socket Operations", { int i; So(nng_setopt_size( - s1, NNG_OPT_RCVMAXSZ, 6550) == 0); - So(nng_getopt_size(s1, NNG_OPT_RCVMAXSZ, &v) == - 0); + s1, nng_optid_recvmaxsz, 6550) == 0); + So(nng_getopt_size( + s1, nng_optid_recvmaxsz, &v) == 0); So(v == 6550); v = 102400; - So(nng_setopt(s1, NNG_OPT_RCVMAXSZ, &v, 1) == - NNG_EINVAL); - So(nng_getopt_size(s1, NNG_OPT_RCVMAXSZ, &v) == - 0); + So(nng_setopt(s1, nng_optid_recvmaxsz, &v, + 1) == NNG_EINVAL); + So(nng_getopt_size( + s1, nng_optid_recvmaxsz, &v) == 0); So(v == 6550); i = 42; - So(nng_setopt(s1, NNG_OPT_RCVBUF, &i, 1) == + So(nng_setopt(s1, nng_optid_recvbuf, &i, 1) == NNG_EINVAL); if (sizeof(size_t) == 8) { v = 0x10000; v <<= 30; So(nng_setopt_size(s1, - NNG_OPT_RCVMAXSZ, + nng_optid_recvmaxsz, v) == NNG_EINVAL); - So(nng_getopt_size( - s1, NNG_OPT_RCVMAXSZ, &v) == 0); + So(nng_getopt_size(s1, + nng_optid_recvmaxsz, &v) == 0); So(v == 6550); } }); @@ -316,21 +316,22 @@ TestMain("Socket Operations", { Convey("Options work", { size_t sz; So(nng_dialer_setopt_size( - ep, NNG_OPT_RCVMAXSZ, 4321) == 0); + ep, nng_optid_recvmaxsz, 4321) == 0); So(nng_dialer_getopt_size( - ep, NNG_OPT_RCVMAXSZ, &sz) == 0); + ep, nng_optid_recvmaxsz, &sz) == 0); So(sz == 4321); }); Convey("Socket opts not for dialer", { // Not appropriate for dialer. - So(nng_dialer_setopt_int(ep, NNG_OPT_RAW, 1) == - NNG_ENOTSUP); + So(nng_dialer_setopt_int( + ep, nng_optid_raw, 1) == NNG_ENOTSUP); So(nng_dialer_setopt_usec(ep, - NNG_OPT_RECONN_TIME, 1) == NNG_ENOTSUP); + nng_optid_reconnmint, + 1) == NNG_ENOTSUP); }); Convey("Bad size checks", { - So(nng_dialer_setopt(ep, NNG_OPT_RCVMAXSZ, "a", - 1) == NNG_EINVAL); + So(nng_dialer_setopt(ep, nng_optid_recvmaxsz, + "a", 1) == NNG_EINVAL); }); Convey("Cannot listen", { So(nng_listener_start(ep, 0) == NNG_ENOTSUP); }); @@ -344,20 +345,21 @@ TestMain("Socket Operations", { Convey("Options work", { size_t sz; So(nng_listener_setopt_size( - ep, NNG_OPT_RCVMAXSZ, 4321) == 0); + ep, nng_optid_recvmaxsz, 4321) == 0); So(nng_listener_getopt_size( - ep, NNG_OPT_RCVMAXSZ, &sz) == 0); + ep, nng_optid_recvmaxsz, &sz) == 0); So(sz == 4321); }); Convey("Socket opts not for dialer", { // Not appropriate for dialer. So(nng_listener_setopt_int( - ep, NNG_OPT_RAW, 1) == NNG_ENOTSUP); + ep, nng_optid_raw, 1) == NNG_ENOTSUP); So(nng_listener_setopt_usec(ep, - NNG_OPT_RECONN_TIME, 1) == NNG_ENOTSUP); + nng_optid_reconnmint, + 1) == NNG_ENOTSUP); }); Convey("Bad size checks", { - So(nng_listener_setopt(ep, NNG_OPT_RCVMAXSZ, + So(nng_listener_setopt(ep, nng_optid_recvmaxsz, "a", 1) == NNG_EINVAL); }); Convey("Cannot dial", @@ -370,30 +372,30 @@ TestMain("Socket Operations", { uint64_t t; So(nng_dialer_setopt_size( - 1999, NNG_OPT_RCVMAXSZ, 10) == NNG_ENOENT); + 1999, nng_optid_recvmaxsz, 10) == NNG_ENOENT); So(nng_listener_setopt_size( - 1999, NNG_OPT_RCVMAXSZ, 10) == NNG_ENOENT); + 1999, nng_optid_recvmaxsz, 10) == NNG_ENOENT); s = 1; - So(nng_dialer_getopt(1999, NNG_OPT_RAW, &i, &s) == + So(nng_dialer_getopt(1999, nng_optid_raw, &i, &s) == NNG_ENOENT); - So(nng_listener_getopt(1999, NNG_OPT_RAW, &i, &s) == + So(nng_listener_getopt(1999, nng_optid_raw, &i, &s) == NNG_ENOENT); So(nng_dialer_getopt_size( - 1999, NNG_OPT_RCVMAXSZ, &s) == NNG_ENOENT); + 1999, nng_optid_recvmaxsz, &s) == NNG_ENOENT); So(nng_listener_getopt_size( - 1999, NNG_OPT_RCVMAXSZ, &s) == NNG_ENOENT); + 1999, nng_optid_recvmaxsz, &s) == NNG_ENOENT); - So(nng_dialer_getopt_int(1999, NNG_OPT_RAW, &i) == + So(nng_dialer_getopt_int(1999, nng_optid_raw, &i) == NNG_ENOENT); - So(nng_listener_getopt_int(1999, NNG_OPT_RAW, &i) == + So(nng_listener_getopt_int(1999, nng_optid_raw, &i) == NNG_ENOENT); - So(nng_dialer_getopt_usec(1999, NNG_OPT_LINGER, &t) == - NNG_ENOENT); + So(nng_dialer_getopt_usec( + 1999, nng_optid_linger, &t) == NNG_ENOENT); So(nng_listener_getopt_usec( - 1999, NNG_OPT_LINGER, &t) == NNG_ENOENT); + 1999, nng_optid_linger, &t) == NNG_ENOENT); }); @@ -404,8 +406,8 @@ TestMain("Socket Operations", { trantest_next_address(addr, "ipc:///tmp/sock_test_%u"); So(nng_dialer_create(&ep, s1, addr) == 0); So(nng_dialer_start(ep, NNG_FLAG_NONBLOCK) == 0); - So(nng_dialer_setopt_size(ep, NNG_OPT_RCVMAXSZ, 10) == - NNG_ESTATE); + So(nng_dialer_setopt_size( + ep, nng_optid_recvmaxsz, 10) == NNG_ESTATE); So(nng_dialer_close(ep) == 0); So(nng_dialer_close(ep) == NNG_ENOENT); }); @@ -419,7 +421,7 @@ TestMain("Socket Operations", { So(nng_listener_create(&ep, s1, addr) == 0); So(nng_listener_start(ep, 0) == 0); So(nng_listener_setopt_size( - ep, NNG_OPT_RCVMAXSZ, 10) == NNG_ESTATE); + ep, nng_optid_recvmaxsz, 10) == NNG_ESTATE); So(nng_listener_close(ep) == 0); So(nng_listener_close(ep) == NNG_ENOENT); }); @@ -435,17 +437,17 @@ TestMain("Socket Operations", { So(nng_pair_open(&s2) == 0); Reset({ nng_close(s2); }); - So(nng_setopt_int(s1, NNG_OPT_RCVBUF, 1) == 0); - So(nng_getopt_int(s1, NNG_OPT_RCVBUF, &len) == 0); + So(nng_setopt_int(s1, nng_optid_recvbuf, 1) == 0); + So(nng_getopt_int(s1, nng_optid_recvbuf, &len) == 0); So(len == 1); - So(nng_setopt_int(s1, NNG_OPT_SNDBUF, 1) == 0); - So(nng_setopt_int(s2, NNG_OPT_SNDBUF, 1) == 0); + So(nng_setopt_int(s1, nng_optid_sendbuf, 1) == 0); + So(nng_setopt_int(s2, nng_optid_sendbuf, 1) == 0); - So(nng_setopt_usec(s1, NNG_OPT_SNDTIMEO, to) == 0); - So(nng_setopt_usec(s1, NNG_OPT_RCVTIMEO, to) == 0); - So(nng_setopt_usec(s2, NNG_OPT_SNDTIMEO, to) == 0); - So(nng_setopt_usec(s2, NNG_OPT_RCVTIMEO, to) == 0); + So(nng_setopt_usec(s1, nng_optid_sendtimeo, to) == 0); + So(nng_setopt_usec(s1, nng_optid_recvtimeo, to) == 0); + So(nng_setopt_usec(s2, nng_optid_sendtimeo, to) == 0); + So(nng_setopt_usec(s2, nng_optid_recvtimeo, to) == 0); So(nng_listen(s1, a, NULL, 0) == 0); So(nng_dial(s2, a, NULL, 0) == 0); diff --git a/tests/survey.c b/tests/survey.c index 2bdd2930..ae252cd5 100644 --- a/tests/survey.c +++ b/tests/survey.c @@ -9,142 +9,133 @@ // #include "convey.h" -#include "core/nng_impl.h" #include "nng.h" #include +extern const char *nng_opt_surveyor_surveytime; +extern int nng_optid_surveyor_surveytime; + #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) -Main({ +TestMain("SURVEY pattern", { const char *addr = "inproc://test"; - nni_init(); - - Test("SURVEY pattern", { - Convey("We can create a SURVEYOR socket", - { - nng_socket surv; - - So(nng_surveyor_open(&surv) == 0); - - Reset({ nng_close(surv); }); - - Convey("Protocols match", { - So(nng_protocol(surv) == - NNG_PROTO_SURVEYOR); - So(nng_peer(surv) == NNG_PROTO_RESPONDENT); - }); - - Convey("Recv with no survey fails", { - nng_msg *msg; - So(nng_recvmsg(surv, &msg, 0) == - NNG_ESTATE); - }); - - Convey("Survey without responder times out", { - uint64_t expire = 50000; - nng_msg *msg; - - So(nng_setopt(surv, NNG_OPT_SURVEYTIME, - &expire, sizeof(expire)) == 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("Protocols match", { - So(nng_protocol(resp) == - NNG_PROTO_RESPONDENT); - So(nng_peer(resp) == - NNG_PROTO_SURVEYOR); - }); - - Convey("Send fails with no suvey", { - 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; - uint64_t expire; - - So(nng_surveyor_open(&surv) == 0); - So(nng_respondent_open(&resp) == 0); - - Reset({ - nng_close(surv); - nng_close(resp); - }); - - expire = 50000; - So(nng_setopt(surv, NNG_OPT_SURVEYTIME, - &expire, sizeof(expire)) == 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; - uint64_t rtimeo; - - 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", { - rtimeo = 200000; - So(nng_setopt(surv, - NNG_OPT_RCVTIMEO, - &rtimeo, - sizeof(rtimeo)) == - 0); - So(nng_recvmsg(surv, &msg, - 0) == NNG_ESTATE); - }); - }); - }); + atexit(nng_fini); + + Convey("We can create a SURVEYOR socket", { + nng_socket surv; + + So(nng_surveyor_open(&surv) == 0); + + Reset({ nng_close(surv); }); + + Convey("Surveytime option id works", { + int opt; + const char *name; + opt = nng_option_lookup(nng_opt_surveyor_surveytime); + So(opt >= 0); + So(opt == nng_optid_surveyor_surveytime); + name = nng_option_name(opt); + So(name != NULL); + So(strcmp(name, nng_opt_surveyor_surveytime) == 0); + }); + + Convey("Protocols match", { + So(nng_protocol(surv) == NNG_PROTO_SURVEYOR); + So(nng_peer(surv) == NNG_PROTO_RESPONDENT); + }); + + 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_usec(surv, nng_optid_surveyor_surveytime, + 50000) == 0); + So(nng_msg_alloc(&msg, 0) == 0); + So(nng_sendmsg(surv, msg, 0) == 0); + So(nng_recvmsg(surv, &msg, 0) == NNG_ETIMEDOUT); + }); }); - nni_fini(); -}) + Convey("We can create a RESPONDENT socket", { + nng_socket resp; + So(nng_respondent_open(&resp) == 0); + + Reset({ nng_close(resp); }); + + Convey("Protocols match", { + So(nng_protocol(resp) == NNG_PROTO_RESPONDENT); + So(nng_peer(resp) == NNG_PROTO_SURVEYOR); + }); + + Convey("Send fails with no suvey", { + 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_usec( + surv, nng_optid_surveyor_surveytime, 50000) == 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; + uint64_t rtimeo; + + 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", { + rtimeo = 200000; + So(nng_setopt_usec(surv, nng_optid_recvtimeo, + 200000) == 0); + So(nng_recvmsg(surv, &msg, 0) == NNG_ESTATE); + }); + }); + }); +}); -- cgit v1.2.3-70-g09d2