aboutsummaryrefslogtreecommitdiff
path: root/tests/survey.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-08 21:19:09 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-09 02:38:55 -0700
commitd64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba (patch)
treef6bdac79578176f0d00528d191f862009e761eac /tests/survey.c
parent5f0398de8edd1ed4ddbf6455c66273a6608aad9a (diff)
downloadnng-d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba.tar.gz
nng-d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba.tar.bz2
nng-d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba.zip
fixes #44 open protocol by "name" (symbol) instead number
fixes #38 Make protocols "pluggable", or at least optional This is a breaking change, as we've done away with the central registered list of protocols, and instead demand the user call nng_xxx_open() where xxx is a protocol name. (We did keep a table around in the compat framework though.) There is a nice way for protocols to plug in via an nni_proto_open(), where they can use a generic constructor that they use to build a protocol specific constructor (passing their ops vector in.)
Diffstat (limited to 'tests/survey.c')
-rw-r--r--tests/survey.c240
1 files changed, 129 insertions, 111 deletions
diff --git a/tests/survey.c b/tests/survey.c
index a7dd3cca..6f85850f 100644
--- a/tests/survey.c
+++ b/tests/survey.c
@@ -1,5 +1,6 @@
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
+// Copyright 2017 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
@@ -8,14 +9,15 @@
//
#include "convey.h"
-#include "nng.h"
#include "core/nng_impl.h"
+#include "nng.h"
#include <string.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)
+#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({
const char *addr = "inproc://test";
@@ -23,113 +25,129 @@ Main({
nni_init();
Test("SURVEY pattern", {
- Convey("We can create a SURVEYOR socket", {
- nng_socket surv;
-
- So(nng_open(&surv, NNG_PROTO_SURVEYOR) == 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_open(&resp, NNG_PROTO_RESPONDENT) == 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_open(&surv, NNG_PROTO_SURVEYOR) == 0);
- So(nng_open(&resp, NNG_PROTO_RESPONDENT) == 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, NNG_FLAG_SYNCH) == 0);
- So(nng_dial(resp, addr, NULL, NNG_FLAG_SYNCH) == 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_open(&sock, NNG_PROTO_RESPONDENT) == 0);
- So(nng_dial(sock, addr, NULL, NNG_FLAG_SYNCH) == 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_trunc(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);
- })
- })
- })
- })
+ 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, NNG_FLAG_SYNCH) == 0);
+ So(nng_dial(
+ resp, addr, NULL, NNG_FLAG_SYNCH) == 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, NNG_FLAG_SYNCH) == 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_trunc(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);
+ });
+ });
+ });
+ });
nni_fini();
})