aboutsummaryrefslogtreecommitdiff
path: root/tests/pubsub.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-14 15:27:38 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-14 15:27:38 -0700
commit63479c2938cbc80c1aac9367cb95564f6e7540e1 (patch)
tree495584b637f73b593d25f01577eeaa944477f159 /tests/pubsub.c
parent343417234aa3fd86e8ae0b56ae500a1ed3411cfc (diff)
downloadnng-63479c2938cbc80c1aac9367cb95564f6e7540e1.tar.gz
nng-63479c2938cbc80c1aac9367cb95564f6e7540e1.tar.bz2
nng-63479c2938cbc80c1aac9367cb95564f6e7540e1.zip
fixes #63 NNG_FLAG_SYNCH should be the default
Also enables creating endpoints that are idle (first part of endpoint options API) and shutting down endpoints.
Diffstat (limited to 'tests/pubsub.c')
-rw-r--r--tests/pubsub.c249
1 files changed, 121 insertions, 128 deletions
diff --git a/tests/pubsub.c b/tests/pubsub.c
index 19d85848..2712181d 100644
--- a/tests/pubsub.c
+++ b/tests/pubsub.c
@@ -9,7 +9,6 @@
//
#include "convey.h"
-#include "core/nng_impl.h"
#include "nng.h"
#include <string.h>
@@ -19,161 +18,155 @@
So(nng_msg_len(m) == strlen(s)); \
So(memcmp(nng_msg_body(m), s, strlen(s)) == 0)
-Main({
+TestMain("PUB/SUB pattern", {
const char *addr = "inproc://test";
- nni_init();
- Test("PUB/SUB pattern", {
- Convey("We can create a PUB socket", {
- nng_socket pub;
+ Reset({ nng_fini(); });
- So(nng_pub_open(&pub) == 0);
+ Convey("We can create a PUB socket", {
+ nng_socket pub;
- Reset({ nng_close(pub); });
+ So(nng_pub_open(&pub) == 0);
- Convey("Protocols match", {
- So(nng_protocol(pub) == NNG_PROTO_PUB);
- So(nng_peer(pub) == NNG_PROTO_SUB);
- });
+ Reset({ nng_close(pub); });
- Convey("Recv fails", {
- nng_msg *msg;
- So(nng_recvmsg(pub, &msg, 0) == NNG_ENOTSUP);
- });
+ Convey("Protocols match", {
+ So(nng_protocol(pub) == NNG_PROTO_PUB);
+ So(nng_peer(pub) == NNG_PROTO_SUB);
});
- Convey("We can create a SUB socket", {
- nng_socket sub;
- So(nng_sub_open(&sub) == 0);
+ Convey("Recv fails", {
+ nng_msg *msg;
+ So(nng_recvmsg(pub, &msg, 0) == NNG_ENOTSUP);
+ });
+ });
- Reset({ nng_close(sub); });
+ Convey("We can create a SUB socket", {
+ nng_socket sub;
+ So(nng_sub_open(&sub) == 0);
- Convey("Protocols match", {
- So(nng_protocol(sub) == NNG_PROTO_SUB);
- So(nng_peer(sub) == NNG_PROTO_PUB);
- });
+ Reset({ nng_close(sub); });
- Convey("Send fails", {
- nng_msg *msg;
- So(nng_msg_alloc(&msg, 0) == 0);
- So(nng_sendmsg(sub, msg, 0) == NNG_ENOTSUP);
- nng_msg_free(msg);
- });
+ Convey("Protocols match", {
+ So(nng_protocol(sub) == NNG_PROTO_SUB);
+ So(nng_peer(sub) == NNG_PROTO_PUB);
});
- Convey("We can create a linked PUB/SUB pair", {
- nng_socket pub;
- nng_socket sub;
+ Convey("Send fails", {
+ nng_msg *msg;
+ So(nng_msg_alloc(&msg, 0) == 0);
+ So(nng_sendmsg(sub, msg, 0) == NNG_ENOTSUP);
+ nng_msg_free(msg);
+ });
+ });
- So(nng_pub_open(&pub) == 0);
+ Convey("We can create a linked PUB/SUB pair", {
+ nng_socket pub;
+ nng_socket sub;
- So(nng_sub_open(&sub) == 0);
+ So(nng_pub_open(&pub) == 0);
- Reset({
- nng_close(pub);
- nng_close(sub);
- });
+ So(nng_sub_open(&sub) == 0);
+
+ Reset({
+ nng_close(pub);
+ nng_close(sub);
+ });
- // Most consumers will usually have the pub
- // listen, and the sub dial. However, this
- // creates a problem for our tests, since we
- // can wind up trying to push data before the
- // pipe is fully registered (the accept runs in
- // an asynch thread.) Doing the reverse here
- // ensures that we won't lose data.
- So(nng_listen(sub, addr, NULL, NNG_FLAG_SYNCH) == 0);
- So(nng_dial(pub, addr, NULL, NNG_FLAG_SYNCH) == 0);
-
- Convey("Sub can subscribe", {
+ // Most applications will usually have the pub listen,
+ // and the sub dial. However, this creates a problem
+ // for our tests, since we can wind up trying to push
+ // data before the pipe is fully registered (the accept
+ // runs asynchronously.) Doing the reverse here
+ // ensures that we won't lose data.
+ So(nng_listen(sub, addr, NULL, 0) == 0);
+ 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);
+ Convey("Unsubscribe works", {
+ So(nng_setopt(sub, NNG_OPT_UNSUBSCRIBE, "ABC",
+ 3) == 0);
So(nng_setopt(
- sub, NNG_OPT_SUBSCRIBE, "ABC", 3) == 0);
- So(nng_setopt(sub, NNG_OPT_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,
- "HELLO", 0) == NNG_ENOENT);
- });
- });
+ sub, NNG_OPT_UNSUBSCRIBE, "", 0) == 0);
- Convey("Pub cannot subscribe", {
- So(nng_setopt(pub, NNG_OPT_SUBSCRIBE, "", 0) ==
- NNG_ENOTSUP);
+ So(nng_setopt(sub, NNG_OPT_UNSUBSCRIBE, "",
+ 0) == NNG_ENOENT);
+ So(nng_setopt(sub, NNG_OPT_UNSUBSCRIBE,
+ "HELLO", 0) == NNG_ENOENT);
});
+ });
- Convey("Subs can receive from pubs", {
- nng_msg *msg;
- uint64_t rtimeo;
-
- So(nng_setopt(sub, NNG_OPT_SUBSCRIBE, "/some/",
- strlen("/some/")) == 0);
- rtimeo = 50000; // 50ms
- So(nng_setopt(sub, NNG_OPT_RCVTIMEO, &rtimeo,
- sizeof(rtimeo)) == 0);
-
- So(nng_msg_alloc(&msg, 0) == 0);
- APPENDSTR(msg, "/some/like/it/hot");
- So(nng_sendmsg(pub, msg, 0) == 0);
- So(nng_recvmsg(sub, &msg, 0) == 0);
- CHECKSTR(msg, "/some/like/it/hot");
- nng_msg_free(msg);
-
- So(nng_msg_alloc(&msg, 0) == 0);
- APPENDSTR(msg, "/somewhere/over/the/rainbow");
- CHECKSTR(msg, "/somewhere/over/the/rainbow");
-
- So(nng_sendmsg(pub, msg, 0) == 0);
- So(nng_recvmsg(sub, &msg, 0) == NNG_ETIMEDOUT);
-
- So(nng_msg_alloc(&msg, 0) == 0);
- APPENDSTR(msg, "/some/day/some/how");
- CHECKSTR(msg, "/some/day/some/how");
-
- So(nng_sendmsg(pub, msg, 0) == 0);
- So(nng_recvmsg(sub, &msg, 0) == 0);
- CHECKSTR(msg, "/some/day/some/how");
- nng_msg_free(msg);
- });
+ Convey("Pub cannot subscribe", {
+ So(nng_setopt(pub, NNG_OPT_SUBSCRIBE, "", 0) ==
+ NNG_ENOTSUP);
+ });
+
+ Convey("Subs can receive from pubs", {
+ nng_msg *msg;
+ uint64_t rtimeo;
+
+ So(nng_setopt(sub, NNG_OPT_SUBSCRIBE, "/some/",
+ strlen("/some/")) == 0);
+ rtimeo = 50000; // 50ms
+ So(nng_setopt(sub, NNG_OPT_RCVTIMEO, &rtimeo,
+ sizeof(rtimeo)) == 0);
+
+ So(nng_msg_alloc(&msg, 0) == 0);
+ APPENDSTR(msg, "/some/like/it/hot");
+ So(nng_sendmsg(pub, msg, 0) == 0);
+ So(nng_recvmsg(sub, &msg, 0) == 0);
+ CHECKSTR(msg, "/some/like/it/hot");
+ nng_msg_free(msg);
+
+ So(nng_msg_alloc(&msg, 0) == 0);
+ APPENDSTR(msg, "/somewhere/over/the/rainbow");
+ CHECKSTR(msg, "/somewhere/over/the/rainbow");
+
+ So(nng_sendmsg(pub, msg, 0) == 0);
+ So(nng_recvmsg(sub, &msg, 0) == NNG_ETIMEDOUT);
+
+ So(nng_msg_alloc(&msg, 0) == 0);
+ APPENDSTR(msg, "/some/day/some/how");
+ CHECKSTR(msg, "/some/day/some/how");
+
+ So(nng_sendmsg(pub, msg, 0) == 0);
+ So(nng_recvmsg(sub, &msg, 0) == 0);
+ CHECKSTR(msg, "/some/day/some/how");
+ nng_msg_free(msg);
+ });
- Convey("Subs without subsciptions don't receive", {
+ 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);
+ uint64_t rtimeo = 50000; // 50ms
+ nng_msg *msg;
+ So(nng_setopt(sub, NNG_OPT_RCVTIMEO, &rtimeo,
+ sizeof(rtimeo)) == 0);
- So(nng_msg_alloc(&msg, 0) == 0);
- APPENDSTR(msg, "/some/don't/like/it");
- So(nng_sendmsg(pub, msg, 0) == 0);
- So(nng_recvmsg(sub, &msg, 0) == NNG_ETIMEDOUT);
- });
+ So(nng_msg_alloc(&msg, 0) == 0);
+ APPENDSTR(msg, "/some/don't/like/it");
+ So(nng_sendmsg(pub, msg, 0) == 0);
+ So(nng_recvmsg(sub, &msg, 0) == NNG_ETIMEDOUT);
+ });
- Convey("Subs in raw receive", {
+ Convey("Subs in raw receive", {
- uint64_t rtimeo = 50000; // 500ms
- int raw = 1;
- nng_msg *msg;
+ 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)) == 0);
+ So(nng_setopt(sub, NNG_OPT_RCVTIMEO, &rtimeo,
+ sizeof(rtimeo)) == 0);
+ So(nng_setopt(sub, NNG_OPT_RAW, &raw, sizeof(raw)) ==
+ 0);
- So(nng_msg_alloc(&msg, 0) == 0);
- APPENDSTR(msg, "/some/like/it/raw");
- So(nng_sendmsg(pub, msg, 0) == 0);
- So(nng_recvmsg(sub, &msg, 0) == 0);
- CHECKSTR(msg, "/some/like/it/raw");
- nng_msg_free(msg);
- });
+ So(nng_msg_alloc(&msg, 0) == 0);
+ APPENDSTR(msg, "/some/like/it/raw");
+ So(nng_sendmsg(pub, msg, 0) == 0);
+ So(nng_recvmsg(sub, &msg, 0) == 0);
+ CHECKSTR(msg, "/some/like/it/raw");
+ nng_msg_free(msg);
});
});
-
- nni_fini();
})