diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-08-08 21:19:09 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-08-09 02:38:55 -0700 |
| commit | d64f12553eb6ceb67ed6f6a5b2ceb6c061d375ba (patch) | |
| tree | f6bdac79578176f0d00528d191f862009e761eac /tests/sock.c | |
| parent | 5f0398de8edd1ed4ddbf6455c66273a6608aad9a (diff) | |
| download | nng-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/sock.c')
| -rw-r--r-- | tests/sock.c | 330 |
1 files changed, 170 insertions, 160 deletions
diff --git a/tests/sock.c b/tests/sock.c index 6c6d5b06..27599e47 100644 --- a/tests/sock.c +++ b/tests/sock.c @@ -8,175 +8,185 @@ // #include "convey.h" -#include "nng.h" #include "core/nng_impl.h" +#include "nng.h" #include <string.h> Main({ Test("Socket Operations", { - Convey("We are able to open a PAIR socket", { - int rv; - nng_socket sock; - - So(nng_open(&sock, NNG_PROTO_PAIR) == 0); - - Reset({ - nng_close(sock); - }) - - Convey("And we can shut it down", { - rv = nng_shutdown(sock); - So(rv == 0); - rv = nng_shutdown(sock); - So(rv == NNG_ECLOSED); - }) - - Convey("It's type is still proto", { - So(nng_protocol(sock) == NNG_PROTO_PAIR); - }) - - Convey("Recv with no pipes times out correctly", { - nng_msg *msg = NULL; - int64_t when = 100000; - uint64_t now; - - now = nni_clock(); - - 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("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("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("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); + Convey("We are able to open a PAIR socket", { + int rv; + nng_socket sock; + + So(nng_pair_open(&sock) == 0); + + Reset({ nng_close(sock); }); + + Convey("And we can shut it down", { + rv = nng_shutdown(sock); 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); + rv = nng_shutdown(sock); + So(rv == NNG_ECLOSED); + }); + + Convey("It's type is still proto", + { So(nng_protocol(sock) == NNG_PROTO_PAIR); }); + + Convey("Recv with no pipes times out correctly", { + nng_msg *msg = NULL; + int64_t when = 100000; + uint64_t now; + + now = nni_clock(); + + rv = nng_setopt(sock, NNG_OPT_RCVTIMEO, &when, + sizeof(when)); So(rv == 0); - So(sz == sizeof (check)); - So(check == 1234); - }) - }) - - 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("Dialing synch can get refused", { - rv = nng_dial(sock, "inproc://notthere", NULL, NNG_FLAG_SYNCH); - So(rv == NNG_ECONNREFUSED); - }) - - 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_open(&sock2, NNG_PROTO_PAIR) == 0); - Reset({ - nng_close(sock2); + 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("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("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("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); }) - rv = nng_dial(sock2, "inproc://here", NULL, NNG_FLAG_SYNCH); + }); + + 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("Dialing synch can get refused", { + rv = nng_dial(sock, "inproc://notthere", NULL, + NNG_FLAG_SYNCH); + So(rv == NNG_ECONNREFUSED); + }); + + Convey("Listening works", { + rv = nng_listen(sock, "inproc://here", NULL, + NNG_FLAG_SYNCH); So(rv == 0); - nng_close(sock2); - }) - }) - - Convey("We can send and receive messages", { - nng_socket sock2; - int len = 1; - size_t sz; - uint64_t second = 3000000; - char *buf; - - So(nng_open(&sock2, NNG_PROTO_PAIR) == 0); - Reset({ - nng_close(sock2); - }) - - So(nng_setopt(sock, NNG_OPT_RCVBUF, &len, sizeof (len)) == 0); - So(nng_setopt(sock, NNG_OPT_SNDBUF, &len, sizeof (len)) == 0); - - So(nng_setopt(sock2, NNG_OPT_RCVBUF, &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("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("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); + So(nng_setopt(sock, NNG_OPT_SNDBUF, &len, + sizeof(len)) == 0); + + So(nng_setopt(sock2, NNG_OPT_RCVBUF, &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); + }); + }); + }); }) |
