summaryrefslogtreecommitdiff
path: root/tests/pollfd.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-24 14:15:48 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-24 14:20:34 -0700
commitc9a68bfe6bea2acc708bf49045f6cb65017a3306 (patch)
treee2b93b81b2962bdfb7953cb30fcfae08f0bd4093 /tests/pollfd.c
parent68ff9c823d3cead2b11a003c40c8f5affc11dc71 (diff)
downloadnng-c9a68bfe6bea2acc708bf49045f6cb65017a3306.tar.gz
nng-c9a68bfe6bea2acc708bf49045f6cb65017a3306.tar.bz2
nng-c9a68bfe6bea2acc708bf49045f6cb65017a3306.zip
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.
Diffstat (limited to 'tests/pollfd.c')
-rw-r--r--tests/pollfd.c219
1 files changed, 120 insertions, 99 deletions
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 <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
@@ -10,6 +11,8 @@
#include "convey.h"
#include "nng.h"
+#include <string.h>
+
#ifndef _WIN32
#include <poll.h>
#include <unistd.h>
@@ -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);
+ });
+ });
+})