aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-12 10:16:54 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-12 10:16:54 -0700
commit9b4d9e71a63cafcee0edee734847bba28d9fea35 (patch)
treec65290fb84bf3168af439ef8e92a5f1370aa2ceb /tests
parent0584aa354014e91a9036bc51bad438e8fddaf15f (diff)
downloadnng-9b4d9e71a63cafcee0edee734847bba28d9fea35.tar.gz
nng-9b4d9e71a63cafcee0edee734847bba28d9fea35.tar.bz2
nng-9b4d9e71a63cafcee0edee734847bba28d9fea35.zip
Convenience option accesor functions.
This adds functions that know about option sizes and make them easier to use. While here I added some validation of those, and cleaned up a few tests slightly. Note that we do not need to use the nng_impl.h for most tests. More of them need to be cleaned up.
Diffstat (limited to 'tests')
-rw-r--r--tests/device.c5
-rw-r--r--tests/pair1.c153
-rw-r--r--tests/pipeline.c7
-rw-r--r--tests/reqrep.c220
-rw-r--r--tests/sock.c432
5 files changed, 359 insertions, 458 deletions
diff --git a/tests/device.c b/tests/device.c
index e5239f25..ba0ebeca 100644
--- a/tests/device.c
+++ b/tests/device.c
@@ -9,7 +9,6 @@
//
#include "convey.h"
-#include "core/nng_impl.h"
#include "nng.h"
#include <string.h>
@@ -33,7 +32,6 @@ dodev(void *arg)
}
Main({
- nni_init();
Test("PAIRv1 device", {
const char *addr1 = "inproc://dev1";
@@ -64,7 +62,6 @@ Main({
So(nng_thread_create(&thr, dodev, &ddata) == 0);
Reset({
- printf("RESETING1\n");
nng_close(dev1);
nng_close(dev2);
nng_thread_destroy(thr);
@@ -108,5 +105,5 @@ Main({
});
});
- nni_fini();
+ nng_fini();
})
diff --git a/tests/pair1.c b/tests/pair1.c
index da5eaa24..5a8311e2 100644
--- a/tests/pair1.c
+++ b/tests/pair1.c
@@ -9,7 +9,6 @@
//
#include "convey.h"
-#include "core/nng_impl.h"
#include "nng.h"
#include "trantest.h"
@@ -35,7 +34,7 @@ TestMain("PAIRv1 protocol", {
nng_close(c1);
nng_close(c2);
nng_usleep(10000);
- nni_fini();
+ nng_fini();
});
Convey("Given a few sockets", {
@@ -45,9 +44,12 @@ TestMain("PAIRv1 protocol", {
So(nng_pair1_open(&c2) == 0);
tmo = 300000;
- So(nng_setopt(s1, NNG_OPT_RCVTIMEO, &tmo, sizeof(tmo)) == 0);
- So(nng_setopt(c1, NNG_OPT_RCVTIMEO, &tmo, sizeof(tmo)) == 0);
- So(nng_setopt(c2, NNG_OPT_RCVTIMEO, &tmo, sizeof(tmo)) == 0);
+ So(nng_setopt_duration(s1, NNG_OPT_RCVTIMEO, tmo) == 0);
+ So(nng_setopt_duration(c1, NNG_OPT_RCVTIMEO, tmo) == 0);
+ So(nng_setopt_duration(c2, NNG_OPT_RCVTIMEO, tmo) == 0);
+ tmo = 0;
+ So(nng_getopt_duration(s1, NNG_OPT_RCVTIMEO, &tmo) == 0);
+ So(tmo == 300000);
Convey("Monogamous cooked mode works", {
nng_msg *msg;
@@ -98,31 +100,22 @@ TestMain("PAIRv1 protocol", {
So(nng_dial(c1, addr, NULL, NNG_FLAG_SYNCH) == 0);
nng_usleep(100000);
- So(nng_setopt(s1, NNG_OPT_RAW, &r, sizeof(r)) ==
- NNG_ESTATE);
- So(nng_setopt(c1, NNG_OPT_RAW, &r, sizeof(r)) ==
- NNG_ESTATE);
+ So(nng_setopt_int(s1, NNG_OPT_RAW, 1) == NNG_ESTATE);
+ So(nng_setopt_int(c1, NNG_OPT_RAW, 1) == NNG_ESTATE);
});
Convey("Polyamorous mode is best effort", {
- int r;
- int rv;
- int i;
- size_t sz;
-
+ int rv;
+ int i;
nng_msg *msg;
- sz = sizeof(r);
- r = 1;
- So(nng_setopt(
- s1, NNG_OPT_POLYAMOROUS, &r, sizeof(r)) == 0);
-
- r = 1;
- So(nng_setopt(s1, NNG_OPT_RCVBUF, &r, sizeof(r)) == 0);
- So(nng_setopt(s1, NNG_OPT_SNDBUF, &r, sizeof(r)) == 0);
- So(nng_setopt(c1, NNG_OPT_RCVBUF, &r, sizeof(r)) == 0);
- tmo = 100000;
- So(nng_setopt(
- s1, NNG_OPT_SNDTIMEO, &tmo, sizeof(tmo)) == 0);
+
+ So(nng_setopt_int(s1, NNG_OPT_POLYAMOROUS, 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_duration(s1, NNG_OPT_SNDTIMEO, 100000) ==
+ 0);
So(nng_listen(s1, addr, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_dial(c1, addr, NULL, NNG_FLAG_SYNCH) == 0);
@@ -130,7 +123,7 @@ TestMain("PAIRv1 protocol", {
for (i = 0, rv = 0; i < 10; i++) {
So(nng_msg_alloc(&msg, 0) == 0);
if ((rv = nng_sendmsg(s1, msg, 0)) != 0) {
- nni_msg_free(msg);
+ nng_msg_free(msg);
break;
}
}
@@ -139,18 +132,15 @@ TestMain("PAIRv1 protocol", {
});
Convey("Monogamous mode exerts backpressure", {
- int r;
int i;
int rv;
nng_msg *msg;
- r = 1;
- So(nng_setopt(s1, NNG_OPT_RCVBUF, &r, sizeof(r)) == 0);
- So(nng_setopt(s1, NNG_OPT_SNDBUF, &r, sizeof(r)) == 0);
- So(nng_setopt(c1, NNG_OPT_RCVBUF, &r, sizeof(r)) == 0);
- tmo = 30000;
- So(nng_setopt(
- s1, NNG_OPT_SNDTIMEO, &tmo, sizeof(tmo)) == 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_duration(s1, NNG_OPT_SNDTIMEO, 30000) ==
+ 0);
So(nng_listen(s1, addr, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_dial(c1, addr, NULL, NNG_FLAG_SYNCH) == 0);
@@ -169,25 +159,23 @@ TestMain("PAIRv1 protocol", {
});
Convey("Cannot set polyamorous mode after connect", {
- int r = 1;
So(nng_listen(s1, addr, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_dial(c1, addr, NULL, NNG_FLAG_SYNCH) == 0);
nng_usleep(100000);
- So(nng_setopt(s1, NNG_OPT_POLYAMOROUS, &r,
- sizeof(r)) == NNG_ESTATE);
- So(nng_setopt(c1, NNG_OPT_POLYAMOROUS, &r,
- sizeof(r)) == NNG_ESTATE);
+ So(nng_setopt_int(s1, NNG_OPT_POLYAMOROUS, 1) ==
+ NNG_ESTATE);
+ So(nng_setopt_int(c1, NNG_OPT_POLYAMOROUS, 1) ==
+ NNG_ESTATE);
});
Convey("Monogamous raw mode works", {
nng_msg *msg;
- int r = 1;
uint32_t hops;
- So(nng_setopt(s1, NNG_OPT_RAW, &r, sizeof(r)) == 0);
- So(nng_setopt(c1, NNG_OPT_RAW, &r, sizeof(r)) == 0);
- So(nng_setopt(c2, NNG_OPT_RAW, &r, sizeof(r)) == 0);
+ 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_listen(s1, addr, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_dial(c1, addr, NULL, NNG_FLAG_SYNCH) == 0);
@@ -262,13 +250,10 @@ TestMain("PAIRv1 protocol", {
});
Convey("TTL is honored", {
- int ttl = 4;
+ int ttl;
- sz = sizeof(ttl);
- So(nng_setopt(s1, NNG_OPT_MAXTTL, &ttl, sz) ==
- 0);
- ttl = 0;
- So(nng_getopt(s1, NNG_OPT_MAXTTL, &ttl, &sz) ==
+ So(nng_setopt_int(s1, NNG_OPT_MAXTTL, 4) == 0);
+ So(nng_getopt_int(s1, NNG_OPT_MAXTTL, &ttl) ==
0);
So(ttl == 4);
Convey("Bad TTL bounces", {
@@ -297,8 +282,8 @@ TestMain("PAIRv1 protocol", {
Convey("Large TTL passes", {
ttl = 0xff;
- So(nng_setopt(s1, NNG_OPT_MAXTTL, &ttl,
- sz) == 0);
+ So(nng_setopt_int(
+ s1, NNG_OPT_MAXTTL, 0xff) == 0);
So(nng_msg_alloc(&msg, 0) == 0);
So(nng_msg_append_u32(msg, 1234) == 0);
So(nng_msg_header_append_u32(
@@ -315,8 +300,8 @@ TestMain("PAIRv1 protocol", {
Convey("Max TTL fails", {
ttl = 0xff;
- So(nng_setopt(s1, NNG_OPT_MAXTTL, &ttl,
- sz) == 0);
+ So(nng_setopt_int(
+ s1, NNG_OPT_MAXTTL, 0xff) == 0);
So(nng_msg_alloc(&msg, 0) == 0);
So(nng_msg_header_append_u32(
msg, 0xff) == 0);
@@ -332,39 +317,30 @@ TestMain("PAIRv1 protocol", {
ttl = 0;
sz = sizeof(ttl);
- So(nng_setopt(s1, NNG_OPT_MAXTTL, &ttl, sz) ==
+ So(nng_setopt_int(s1, NNG_OPT_MAXTTL, 0) ==
NNG_EINVAL);
- ttl = 1000;
- sz = sizeof(ttl);
- So(nng_setopt(s1, NNG_OPT_MAXTTL, &ttl, sz) ==
+ So(nng_setopt_int(s1, NNG_OPT_MAXTTL, 1000) ==
NNG_EINVAL);
sz = 1;
ttl = 8;
-
So(nng_setopt(s1, NNG_OPT_MAXTTL, &ttl, sz) ==
NNG_EINVAL);
});
Convey("Polyamorous cooked mode works", {
nng_msg *msg;
- int poly;
+ int v;
nng_pipe p1;
nng_pipe p2;
- size_t sz;
- sz = sizeof(poly);
- So(nng_getopt(s1, NNG_OPT_POLYAMOROUS, &poly, &sz) ==
- 0);
- So(poly == 0);
+ So(nng_getopt_int(s1, NNG_OPT_POLYAMOROUS, &v) == 0);
+ So(v == 0);
- poly = 1;
- So(nng_setopt(s1, NNG_OPT_POLYAMOROUS, &poly,
- sizeof(poly)) == 0);
- So(nng_getopt(s1, NNG_OPT_POLYAMOROUS, &poly, &sz) ==
- 0);
- So(poly == 1);
+ So(nng_setopt_int(s1, NNG_OPT_POLYAMOROUS, 1) == 0);
+ So(nng_getopt_int(s1, NNG_OPT_POLYAMOROUS, &v) == 0);
+ So(v == 1);
So(nng_listen(s1, addr, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_dial(c1, addr, NULL, NNG_FLAG_SYNCH) == 0);
@@ -418,13 +394,10 @@ TestMain("PAIRv1 protocol", {
Convey("Polyamorous default works", {
nng_msg *msg;
- int poly;
nng_pipe p1;
size_t sz;
- poly = 1;
- So(nng_setopt(s1, NNG_OPT_POLYAMOROUS, &poly,
- sizeof(poly)) == 0);
+ So(nng_setopt_int(s1, NNG_OPT_POLYAMOROUS, 1) == 0);
So(nng_listen(s1, addr, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_dial(c1, addr, NULL, NNG_FLAG_SYNCH) == 0);
@@ -451,33 +424,23 @@ TestMain("PAIRv1 protocol", {
Convey("Polyamorous raw mode works", {
nng_msg *msg;
- int poly;
- int raw;
+ int v;
uint32_t hops;
nng_pipe p1;
nng_pipe p2;
size_t sz;
- sz = sizeof(poly);
- So(nng_getopt(s1, NNG_OPT_POLYAMOROUS, &poly, &sz) ==
- 0);
- So(poly == 0);
-
- poly = 1;
- So(nng_setopt(s1, NNG_OPT_POLYAMOROUS, &poly,
- sizeof(poly)) == 0);
- So(nng_getopt(s1, NNG_OPT_POLYAMOROUS, &poly, &sz) ==
- 0);
- So(poly == 1);
+ So(nng_getopt_int(s1, NNG_OPT_POLYAMOROUS, &v) == 0);
+ So(v == 0);
- raw = 1;
- So(nng_setopt(s1, NNG_OPT_RAW, &raw, sizeof(poly)) ==
- 0);
+ So(nng_setopt_int(s1, NNG_OPT_POLYAMOROUS, 1) == 0);
+ So(nng_getopt_int(s1, NNG_OPT_POLYAMOROUS, &v) == 0);
+ So(v == 1);
- raw = 0;
- sz = sizeof(raw);
- So(nng_getopt(s1, NNG_OPT_RAW, &raw, &sz) == 0);
- So(raw == 1);
+ v = 0;
+ So(nng_setopt_int(s1, NNG_OPT_RAW, 1) == 0);
+ So(nng_getopt_int(s1, NNG_OPT_RAW, &v) == 0);
+ So(v == 1);
So(nng_listen(s1, addr, NULL, NNG_FLAG_SYNCH) == 0);
So(nng_dial(c1, addr, NULL, NNG_FLAG_SYNCH) == 0);
diff --git a/tests/pipeline.c b/tests/pipeline.c
index 510b7f77..e19faa9a 100644
--- a/tests/pipeline.c
+++ b/tests/pipeline.c
@@ -8,7 +8,6 @@
//
#include "convey.h"
-#include "core/nng_impl.h"
#include "nng.h"
#include <string.h>
@@ -20,8 +19,6 @@
Main({
const char *addr = "inproc://test";
- nni_init();
-
Test("PIPELINE (PUSH/PULL) pattern", {
Convey("We can create a PUSH socket", {
nng_socket push;
@@ -166,7 +163,7 @@ Main({
// 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.
- nni_usleep(100000);
+ nng_usleep(100000);
So(nng_sendmsg(push, abc, 0) == 0);
So(nng_sendmsg(push, def, 0) == 0);
@@ -186,5 +183,5 @@ Main({
});
});
- nni_fini();
+ nng_fini();
})
diff --git a/tests/reqrep.c b/tests/reqrep.c
index cadd0a5f..447060b0 100644
--- a/tests/reqrep.c
+++ b/tests/reqrep.c
@@ -8,147 +8,137 @@
//
#include "convey.h"
-#include "core/nng_impl.h"
#include "nng.h"
#include <string.h>
-Main({
+TestMain("REQ/REP pattern", {
int rv;
const char *addr = "inproc://test";
- nni_init();
+ Convey("We can create a REQ socket", {
+ nng_socket req;
- Test("REQ/REP pattern", {
- Convey("We can create a REQ socket", {
- nng_socket req;
+ So(nng_req_open(&req) == 0);
- So(nng_req_open(&req) == 0);
+ Reset({ nng_close(req); });
- Reset({ nng_close(req); });
+ Convey("Protocols match", {
+ So(nng_protocol(req) == NNG_PROTO_REQ);
+ So(nng_peer(req) == NNG_PROTO_REP);
+ });
+
+ Convey("Recv with no send fails", {
+ nng_msg *msg;
+ rv = nng_recvmsg(req, &msg, 0);
+ So(rv == NNG_ESTATE);
+ });
+ });
- Convey("Protocols match", {
- So(nng_protocol(req) == NNG_PROTO_REQ);
- So(nng_peer(req) == NNG_PROTO_REP);
- });
+ Convey("We can create a REP socket", {
+ nng_socket rep;
+ So(nng_rep_open(&rep) == 0);
- Convey("Recv with no send fails", {
- nng_msg *msg;
- rv = nng_recvmsg(req, &msg, 0);
- So(rv == NNG_ESTATE);
- });
+ Reset({ nng_close(rep); });
+
+ Convey("Protocols match", {
+ So(nng_protocol(rep) == NNG_PROTO_REP);
+ So(nng_peer(rep) == NNG_PROTO_REQ);
});
- Convey("We can create a REP socket", {
- nng_socket rep;
- So(nng_rep_open(&rep) == 0);
-
- Reset({ nng_close(rep); });
-
- Convey("Protocols match", {
- So(nng_protocol(rep) == NNG_PROTO_REP);
- So(nng_peer(rep) == NNG_PROTO_REQ);
- });
-
- Convey("Send with no recv fails", {
- nng_msg *msg;
- rv = nng_msg_alloc(&msg, 0);
- So(rv == 0);
- rv = nng_sendmsg(rep, msg, 0);
- So(rv == NNG_ESTATE);
- nng_msg_free(msg);
- });
+ Convey("Send with no recv fails", {
+ nng_msg *msg;
+ rv = nng_msg_alloc(&msg, 0);
+ So(rv == 0);
+ rv = nng_sendmsg(rep, msg, 0);
+ So(rv == NNG_ESTATE);
+ nng_msg_free(msg);
});
+ });
+
+ Convey("We can create a linked REQ/REP pair", {
+ nng_socket req;
+ nng_socket rep;
+
+ So(nng_rep_open(&rep) == 0);
- Convey("We can create a linked REQ/REP pair", {
- nng_socket req;
- nng_socket rep;
-
- So(nng_rep_open(&rep) == 0);
-
- So(nng_req_open(&req) == 0);
-
- Reset({
- nng_close(rep);
- nng_close(req);
- });
-
- So(nng_listen(rep, addr, NULL, NNG_FLAG_SYNCH) == 0);
- So(nng_dial(req, addr, NULL, NNG_FLAG_SYNCH) == 0);
-
- Convey("They can REQ/REP exchange", {
- nng_msg *ping;
- nng_msg *pong;
-
- So(nng_msg_alloc(&ping, 0) == 0);
- So(nng_msg_append(ping, "ping", 5) == 0);
- So(nng_msg_len(ping) == 5);
- So(memcmp(nng_msg_body(ping), "ping", 5) == 0);
- So(nng_sendmsg(req, ping, 0) == 0);
- pong = NULL;
- So(nng_recvmsg(rep, &pong, 0) == 0);
- So(pong != NULL);
- So(nng_msg_len(pong) == 5);
- So(memcmp(nng_msg_body(pong), "ping", 5) == 0);
- nng_msg_trim(pong, 5);
- So(nng_msg_append(pong, "pong", 5) == 0);
- So(nng_sendmsg(rep, pong, 0) == 0);
- ping = 0;
- So(nng_recvmsg(req, &ping, 0) == 0);
- So(ping != NULL);
- So(nng_msg_len(ping) == 5);
- So(memcmp(nng_msg_body(ping), "pong", 5) == 0);
- nng_msg_free(ping);
- });
+ So(nng_req_open(&req) == 0);
+
+ Reset({
+ nng_close(rep);
+ nng_close(req);
});
- Convey("Request cancellation works", {
- nng_msg *abc;
- nng_msg *def;
- nng_msg *cmd;
- uint64_t retry = 100000; // 100 ms
- size_t len;
+ So(nng_listen(rep, addr, NULL, NNG_FLAG_SYNCH) == 0);
+ So(nng_dial(req, addr, NULL, NNG_FLAG_SYNCH) == 0);
+
+ Convey("They can REQ/REP exchange", {
+ nng_msg *ping;
+ nng_msg *pong;
+
+ So(nng_msg_alloc(&ping, 0) == 0);
+ So(nng_msg_append(ping, "ping", 5) == 0);
+ So(nng_msg_len(ping) == 5);
+ So(memcmp(nng_msg_body(ping), "ping", 5) == 0);
+ So(nng_sendmsg(req, ping, 0) == 0);
+ pong = NULL;
+ So(nng_recvmsg(rep, &pong, 0) == 0);
+ So(pong != NULL);
+ So(nng_msg_len(pong) == 5);
+ So(memcmp(nng_msg_body(pong), "ping", 5) == 0);
+ nng_msg_trim(pong, 5);
+ So(nng_msg_append(pong, "pong", 5) == 0);
+ So(nng_sendmsg(rep, pong, 0) == 0);
+ ping = 0;
+ So(nng_recvmsg(req, &ping, 0) == 0);
+ So(ping != NULL);
+ So(nng_msg_len(ping) == 5);
+ So(memcmp(nng_msg_body(ping), "pong", 5) == 0);
+ nng_msg_free(ping);
+ });
+ });
- nng_socket req;
- nng_socket rep;
+ Convey("Request cancellation works", {
+ nng_msg *abc;
+ nng_msg *def;
+ nng_msg *cmd;
+ uint64_t retry = 100000; // 100 ms
- So(nng_rep_open(&rep) == 0);
+ nng_socket req;
+ nng_socket rep;
- So(nng_req_open(&req) == 0);
+ So(nng_rep_open(&rep) == 0);
- Reset({
- nng_close(rep);
- nng_close(req);
- });
+ So(nng_req_open(&req) == 0);
- So(nng_setopt(req, NNG_OPT_RESENDTIME, &retry,
- sizeof(retry)) == 0);
- len = 16;
- So(nng_setopt(
- req, NNG_OPT_SNDBUF, &len, sizeof(len)) == 0);
+ Reset({
+ nng_close(rep);
+ nng_close(req);
+ });
- So(nng_msg_alloc(&abc, 0) == 0);
- So(nng_msg_append(abc, "abc", 4) == 0);
- So(nng_msg_alloc(&def, 0) == 0);
- So(nng_msg_append(def, "def", 4) == 0);
+ So(nng_setopt_duration(req, NNG_OPT_RESENDTIME, retry) == 0);
+ So(nng_setopt_int(req, NNG_OPT_SNDBUF, 16) == 0);
- So(nng_listen(rep, addr, NULL, NNG_FLAG_SYNCH) == 0);
- So(nng_dial(req, addr, NULL, 0) == 0);
+ So(nng_msg_alloc(&abc, 0) == 0);
+ So(nng_msg_append(abc, "abc", 4) == 0);
+ So(nng_msg_alloc(&def, 0) == 0);
+ So(nng_msg_append(def, "def", 4) == 0);
- So(nng_sendmsg(req, abc, 0) == 0);
- So(nng_sendmsg(req, def, 0) == 0);
- So(nng_recvmsg(rep, &cmd, 0) == 0);
- So(cmd != NULL);
+ So(nng_listen(rep, addr, NULL, NNG_FLAG_SYNCH) == 0);
+ So(nng_dial(req, addr, NULL, 0) == 0);
- So(nng_sendmsg(rep, cmd, 0) == 0);
- So(nng_recvmsg(rep, &cmd, 0) == 0);
- So(nng_sendmsg(rep, cmd, 0) == 0);
- So(nng_recvmsg(req, &cmd, 0) == 0);
+ So(nng_sendmsg(req, abc, 0) == 0);
+ So(nng_sendmsg(req, def, 0) == 0);
+ So(nng_recvmsg(rep, &cmd, 0) == 0);
+ So(cmd != NULL);
- So(nng_msg_len(cmd) == 4);
- So(memcmp(nng_msg_body(cmd), "def", 4) == 0);
- nng_msg_free(cmd);
- });
- });
+ So(nng_sendmsg(rep, cmd, 0) == 0);
+ So(nng_recvmsg(rep, &cmd, 0) == 0);
+ So(nng_sendmsg(rep, cmd, 0) == 0);
+ So(nng_recvmsg(req, &cmd, 0) == 0);
- nni_fini();
+ So(nng_msg_len(cmd) == 4);
+ So(memcmp(nng_msg_body(cmd), "def", 4) == 0);
+ nng_msg_free(cmd);
+ });
+ nng_fini();
})
diff --git a/tests/sock.c b/tests/sock.c
index 4ac2fe35..8b3083ee 100644
--- a/tests/sock.c
+++ b/tests/sock.c
@@ -8,273 +8,227 @@
//
#include "convey.h"
-#include "core/nng_impl.h"
#include "nng.h"
#include <string.h>
-Main({
- Test("Socket Operations", {
+TestMain("Socket Operations", {
- Convey("We are able to open a PAIR socket", {
- int rv;
- nng_socket sock;
+ Convey("We are able to open a PAIR socket", {
+ int rv;
+ nng_socket s1;
- So(nng_pair_open(&sock) == 0);
+ So(nng_pair_open(&s1) == 0);
- Reset({ nng_close(sock); });
+ Reset({ nng_close(s1); });
- Convey("And we can shut it down", {
- rv = nng_shutdown(sock);
- So(rv == 0);
- rv = nng_shutdown(sock);
- So(rv == NNG_ECLOSED);
- });
+ Convey("And we can shut it down", {
+ So(nng_shutdown(s1) == 0);
+ So(nng_shutdown(s1) == NNG_ECLOSED);
+ });
- Convey("It's type is still proto",
- { So(nng_protocol(sock) == NNG_PROTO_PAIR); });
+ Convey("It's type & peer are still PAIR", {
+ So(nng_protocol(s1) == NNG_PROTO_PAIR);
+ So(nng_peer(s1) == NNG_PROTO_PAIR);
+ });
- Convey("Recv with no pipes times out correctly", {
- nng_msg *msg = NULL;
- int64_t when = 100000;
- uint64_t now;
+ Convey("Recv with no pipes times out correctly", {
+ nng_msg *msg = NULL;
+ int64_t to = 100000;
+ uint64_t now;
+
+ now = nng_clock();
+ So(now > 0);
+ So(nng_setopt_duration(s1, NNG_OPT_RCVTIMEO, to) == 0);
+ So(nng_recvmsg(s1, &msg, 0) == NNG_ETIMEDOUT);
+ So(msg == NULL);
+ So(nng_clock() >= (now + to));
+ So(nng_clock() < (now + (to * 2)));
+ });
- now = nni_clock();
+ Convey("Recv nonblock with no pipes gives EAGAIN", {
+ nng_msg *msg = NULL;
+ So(nng_recvmsg(s1, &msg, NNG_FLAG_NONBLOCK) ==
+ NNG_EAGAIN);
+ So(msg == NULL);
+ });
+
+ Convey("Send with no pipes times out correctly", {
+ nng_msg *msg = NULL;
+ int64_t to = 100000;
+ uint64_t now;
+
+ // We cheat to get access to the core's clock.
+ So(nng_msg_alloc(&msg, 0) == 0);
+ So(msg != NULL);
+ now = nng_clock();
+
+ So(nng_setopt_duration(s1, NNG_OPT_SNDTIMEO, to) == 0);
+ So(nng_sendmsg(s1, msg, 0) == NNG_ETIMEDOUT);
+ So(nng_clock() >= (now + to));
+ So(nng_clock() < (now + (to * 2)));
+ nng_msg_free(msg);
+ });
- rv = nng_setopt(sock, NNG_OPT_RCVTIMEO, &when,
- sizeof(when));
- So(rv == 0);
- rv = nng_recvmsg(sock, &msg, 0);
- So(rv == NNG_ETIMEDOUT);
- So(msg == NULL);
- So(nni_clock() >= (now + when));
- So(nni_clock() < (now + (when * 2)));
+ Convey("We can set and get options", {
+ int64_t to = 1234;
+ int64_t v = 0;
+ size_t sz;
+
+ So(nng_setopt_duration(s1, NNG_OPT_SNDTIMEO, to) == 0);
+
+ Convey("Short size is not copied", {
+ sz = 0;
+ So(nng_getopt(s1, NNG_OPT_SNDTIMEO, &v, &sz) ==
+ 0);
+ So(sz == sizeof(v));
+ So(v == 0);
});
- Convey("Recv nonblock with no pipes gives EAGAIN", {
- nng_msg *msg = NULL;
- rv =
- nng_recvmsg(sock, &msg, NNG_FLAG_NONBLOCK);
- So(rv == NNG_EAGAIN);
- So(msg == NULL);
+ Convey("Correct size is copied", {
+ sz = sizeof(v);
+ So(nng_getopt(s1, NNG_OPT_SNDTIMEO, &v, &sz) ==
+ 0);
+ So(sz == sizeof(v));
+ So(v == 1234);
});
- Convey("Send with no pipes times out correctly", {
- nng_msg *msg = NULL;
- int64_t when = 100000;
- uint64_t now;
-
- // We cheat to get access to the core's clock.
- So(nng_msg_alloc(&msg, 0) == 0);
- So(msg != NULL);
- now = nni_clock();
-
- rv = nng_setopt(sock, NNG_OPT_SNDTIMEO, &when,
- sizeof(when));
- So(rv == 0);
- rv = nng_sendmsg(sock, msg, 0);
- So(rv == NNG_ETIMEDOUT);
- So(nni_clock() >= (now + when));
- So(nni_clock() < (now + (when * 2)));
- nng_msg_free(msg);
+ Convey("Short size buf is not copied", {
+ int l = 5;
+ sz = 0;
+ So(nng_getopt(s1, NNG_OPT_RCVBUF, &l, &sz) ==
+ 0);
+ So(sz == sizeof(l));
+ So(l == 5);
});
- Convey("We can set and get options", {
- int64_t when = 1234;
- int64_t check = 0;
- size_t sz;
- rv = nng_setopt(sock, NNG_OPT_SNDTIMEO, &when,
- sizeof(when));
- So(rv == 0);
- sz = sizeof(check);
- Convey("Short size is not copied", {
- sz = 0;
- rv = nng_getopt(sock, NNG_OPT_SNDTIMEO,
- &check, &sz);
- So(rv == 0);
- So(sz == sizeof(check));
- So(check == 0);
- });
- Convey("Correct size is copied", {
- sz = sizeof(check);
- rv = nng_getopt(sock, NNG_OPT_SNDTIMEO,
- &check, &sz);
- So(rv == 0);
- So(sz == sizeof(check));
- So(check == 1234);
- });
-
- Convey("Short size buf is not copied", {
- int len = 5;
- sz = 0;
- So(nng_getopt(sock, NNG_OPT_RCVBUF,
- &len, &sz) == 0);
- So(len == 5);
- });
-
- Convey("Insane buffer size fails", {
- int len = 1024 * 1024;
- sz = sizeof(len);
- So(nng_setopt(sock, NNG_OPT_RCVBUF,
- &len, sz) == NNG_EINVAL);
- });
-
- Convey("Negative timeout fails", {
- when = -5;
- sz = sizeof(when);
- So(nng_setopt(sock, NNG_OPT_RCVTIMEO,
- &when, sz) == NNG_EINVAL);
- });
-
- Convey("Short timeout fails", {
- when = 0;
- sz = sizeof(when) - 1;
- So(nng_setopt(sock, NNG_OPT_RCVTIMEO,
- &when, sz) == NNG_EINVAL);
- });
-
- Convey("Bogus raw fails", {
- int raw = 42;
- sz = sizeof(raw);
-
- raw = 42;
- So(nng_setopt(sock, NNG_OPT_RAW, &raw,
- sz) == NNG_EINVAL);
- raw = -42;
- So(nng_setopt(sock, NNG_OPT_RAW, &raw,
- sz) == NNG_EINVAL);
-
- raw = 0;
- So(nng_setopt(sock, NNG_OPT_RAW, &raw,
- sz) == 0);
- });
-
- Convey("Unsupported options fail", {
- char *crap = "crap";
- So(nng_setopt(sock, NNG_OPT_SUBSCRIBE,
- crap,
- strlen(crap)) == NNG_ENOTSUP);
- });
-
- Convey("Bogus sizes fail", {
- size_t rmax;
- rmax = 6550;
- So(nng_setopt(sock, NNG_OPT_RCVMAXSZ,
- &rmax, sizeof(rmax)) == 0);
- rmax = 0;
- sz = sizeof(rmax);
- So(nng_getopt(sock, NNG_OPT_RCVMAXSZ,
- &rmax, &sz) == 0);
- So(rmax == 6550);
-
- rmax = 102400;
- So(nng_setopt(sock, NNG_OPT_RCVMAXSZ,
- &rmax, 1) == NNG_EINVAL);
- So(nng_getopt(sock, NNG_OPT_RCVMAXSZ,
- &rmax, &sz) == 0);
- So(rmax == 6550);
-
- if (sizeof(size_t) == 8) {
- rmax = 0x10000;
- rmax <<= 30;
- So(nng_setopt(sock,
- NNG_OPT_RCVMAXSZ, &rmax,
- sizeof(rmax)) ==
- NNG_EINVAL);
- So(nng_getopt(sock,
- NNG_OPT_RCVMAXSZ, &rmax,
- &sz) == 0);
- So(rmax == 6550);
- }
- });
+ Convey("Insane buffer size fails", {
+ So(nng_setopt_int(s1, NNG_OPT_RCVBUF,
+ 0x100000) == NNG_EINVAL);
});
- Convey("Bogus URLs not supported", {
- Convey("Dialing fails properly", {
- rv = nng_dial(sock,
- "bogus://somewhere", NULL, 0);
- So(rv == NNG_ENOTSUP);
- });
- Convey("Listening fails properly", {
- rv = nng_listen(sock,
- "bogus://elsewhere", NULL, 0);
- So(rv == NNG_ENOTSUP);
- });
+ Convey("Negative timeout fails", {
+ So(nng_setopt_duration(s1, NNG_OPT_RCVTIMEO,
+ -5) == NNG_EINVAL);
});
- Convey("Dialing synch can get refused", {
- rv = nng_dial(sock, "inproc://notthere", NULL,
- NNG_FLAG_SYNCH);
- So(rv == NNG_ECONNREFUSED);
+ Convey("Short timeout fails", {
+ to = 0;
+ sz = sizeof(to) - 1;
+ So(nng_setopt(s1, NNG_OPT_RCVTIMEO, &to, sz) ==
+ NNG_EINVAL);
});
- Convey("Listening works", {
- rv = nng_listen(sock, "inproc://here", NULL,
- NNG_FLAG_SYNCH);
- So(rv == 0);
-
- Convey("Second listen fails ADDRINUSE", {
- rv = nng_listen(sock, "inproc://here",
- NULL, NNG_FLAG_SYNCH);
- So(rv == NNG_EADDRINUSE);
- });
-
- Convey("We can connect to it", {
- nng_socket sock2;
- So(nng_pair_open(&sock2) == 0);
- Reset({ nng_close(sock2); });
- rv = nng_dial(sock2, "inproc://here",
- NULL, NNG_FLAG_SYNCH);
- So(rv == 0);
- nng_close(sock2);
- });
+ Convey("Bogus raw fails", {
+ So(nng_setopt_int(s1, NNG_OPT_RAW, 42) ==
+ NNG_EINVAL);
+ So(nng_setopt_int(s1, NNG_OPT_RAW, -42) ==
+ NNG_EINVAL);
+ So(nng_setopt_int(s1, NNG_OPT_RAW, 0) == 0);
});
- Convey("We can send and receive messages", {
- nng_socket sock2;
- int len = 1;
- size_t sz;
- uint64_t second = 3000000;
- char * buf;
-
- So(nng_pair_open(&sock2) == 0);
- Reset({ nng_close(sock2); });
-
- So(nng_setopt(sock, NNG_OPT_RCVBUF, &len,
- sizeof(len)) == 0);
- sz = sizeof(len);
- So(nng_getopt(
- sock, NNG_OPT_RCVBUF, &len, &sz) == 0);
- So(len == 1);
- sz = 0;
+ Convey("Unsupported options fail", {
+ char *crap = "crap";
+ So(nng_setopt(s1, NNG_OPT_SUBSCRIBE, crap,
+ strlen(crap)) == NNG_ENOTSUP);
+ });
+
+ Convey("Bogus sizes fail", {
+ size_t v;
+
+ So(nng_setopt_size(
+ s1, NNG_OPT_RCVMAXSZ, 6550) == 0);
+ So(nng_getopt_size(s1, NNG_OPT_RCVMAXSZ, &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(v == 6550);
+
+ if (sizeof(size_t) == 8) {
+ v = 0x10000;
+ v <<= 30;
+ So(nng_setopt_size(s1,
+ NNG_OPT_RCVMAXSZ,
+ v) == NNG_EINVAL);
+ So(nng_getopt_size(
+ s1, NNG_OPT_RCVMAXSZ, &v) == 0);
+ So(v == 6550);
+ }
+ });
+ });
- So(nng_setopt(sock, NNG_OPT_SNDBUF, &len,
- sizeof(len)) == 0);
- So(nng_setopt(sock2, NNG_OPT_SNDBUF, &len,
- sizeof(len)) == 0);
-
- So(nng_setopt(sock, NNG_OPT_SNDTIMEO, &second,
- sizeof(second)) == 0);
- So(nng_setopt(sock, NNG_OPT_RCVTIMEO, &second,
- sizeof(second)) == 0);
- So(nng_setopt(sock2, NNG_OPT_SNDTIMEO, &second,
- sizeof(second)) == 0);
- So(nng_setopt(sock2, NNG_OPT_RCVTIMEO, &second,
- sizeof(second)) == 0);
-
- So(nng_listen(sock, "inproc://test1", NULL,
- NNG_FLAG_SYNCH) == 0);
- So(nng_dial(sock2, "inproc://test1", NULL,
- NNG_FLAG_SYNCH) == 0);
-
- So(nng_send(sock, "abc", 4, 0) == 0);
- So(nng_recv(
- sock2, &buf, &sz, NNG_FLAG_ALLOC) == 0);
- So(buf != NULL);
- So(sz == 4);
- So(memcmp(buf, "abc", 4) == 0);
- nng_free(buf, sz);
+ Convey("Bogus URLs not supported", {
+ Convey("Dialing fails properly", {
+ rv = nng_dial(s1, "bogus://1", NULL, 0);
+ So(rv == NNG_ENOTSUP);
});
+ Convey("Listening fails properly", {
+ rv = nng_listen(s1, "bogus://2", NULL, 0);
+ So(rv == NNG_ENOTSUP);
+ });
+ });
+
+ Convey("Dialing synch can get refused", {
+ rv = nng_dial(s1, "inproc://no", NULL, NNG_FLAG_SYNCH);
+ So(rv == NNG_ECONNREFUSED);
+ });
+
+ Convey("Listening works", {
+ char *a = "inproc://here";
+ rv = nng_listen(s1, a, NULL, NNG_FLAG_SYNCH);
+ So(rv == 0);
+
+ Convey("Second listen fails ADDRINUSE", {
+ rv = nng_listen(s1, a, NULL, NNG_FLAG_SYNCH);
+ So(rv == NNG_EADDRINUSE);
+ });
+
+ Convey("We can connect to it", {
+ nng_socket s2;
+ So(nng_pair_open(&s2) == 0);
+ Reset({ nng_close(s2); });
+ So(nng_dial(s2, a, NULL, NNG_FLAG_SYNCH) == 0);
+ nng_close(s2);
+ });
+ });
+
+ Convey("We can send and receive messages", {
+ nng_socket s2;
+ int len;
+ size_t sz;
+ uint64_t to = 3000000;
+ char * buf;
+ char * a = "inproc://t1";
+
+ 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(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_duration(s1, NNG_OPT_SNDTIMEO, to) == 0);
+ So(nng_setopt_duration(s1, NNG_OPT_RCVTIMEO, to) == 0);
+ So(nng_setopt_duration(s2, NNG_OPT_SNDTIMEO, to) == 0);
+ So(nng_setopt_duration(s2, NNG_OPT_RCVTIMEO, to) == 0);
+
+ So(nng_listen(s1, a, NULL, NNG_FLAG_SYNCH) == 0);
+ So(nng_dial(s2, a, NULL, NNG_FLAG_SYNCH) == 0);
+
+ So(nng_send(s1, "abc", 4, 0) == 0);
+ So(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC) == 0);
+ So(buf != NULL);
+ So(sz == 4);
+ So(memcmp(buf, "abc", 4) == 0);
+ nng_free(buf, sz);
});
});
})