diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-04-04 13:36:54 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-04-10 15:40:00 -0700 |
| commit | 5f7289e1f8e1427c9214c8e3e96ad56b1f868d53 (patch) | |
| tree | 39debf4ecde234b2a0be19c9cb15628cc32c2edb /tests/reqrep.c | |
| parent | 56f1bf30e61c53646dd2f8425da7c7fa0d97b3e1 (diff) | |
| download | nng-5f7289e1f8e1427c9214c8e3e96ad56b1f868d53.tar.gz nng-5f7289e1f8e1427c9214c8e3e96ad56b1f868d53.tar.bz2 nng-5f7289e1f8e1427c9214c8e3e96ad56b1f868d53.zip | |
fixes #334 Separate context for state machines from sockets
This provides context support for REQ and REP sockets.
More discussion around this is in the issue itself.
Optionally we would like to extend this to the surveyor pattern.
Note that we specifically do not support pollable descriptors
for non-default contexts, and the results of using file descriptors
for polling (NNG_OPT_SENDFD and NNG_OPT_RECVFD) is undefined.
In the future, it might be nice to figure out how to factor in
optional use of a message queue for users who want more buffering,
but we think there is little need for this with cooked mode.
Diffstat (limited to 'tests/reqrep.c')
| -rw-r--r-- | tests/reqrep.c | 84 |
1 files changed, 82 insertions, 2 deletions
diff --git a/tests/reqrep.c b/tests/reqrep.c index 97ed371a..76cf279c 100644 --- a/tests/reqrep.c +++ b/tests/reqrep.c @@ -1,6 +1,6 @@ // -// Copyright 2017 Garrett D'Amore <garrett@damore.org> -// Copyright 2017 Capitar IT Group BV <info@capitar.com> +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 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 @@ -175,5 +175,85 @@ TestMain("REQ/REP pattern", { nng_msg_free(cmd); }); + Convey("Request cancellation aborts pending recv", { + nng_msg * abc; + nng_msg * def; + nng_msg * cmd; + nng_aio * aio; + nng_duration retry = 100; // 100 ms + + nng_socket req; + nng_socket rep; + + So(nng_rep_open(&rep) == 0); + + So(nng_req_open(&req) == 0); + So(nng_aio_alloc(&aio, NULL, NULL) == 0); + + Reset({ + nng_close(rep); + nng_close(req); + nng_aio_free(aio); + }); + + So(nng_setopt_ms(req, NNG_OPT_REQ_RESENDTIME, retry) == 0); + So(nng_setopt_int(req, NNG_OPT_SENDBUF, 16) == 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_listen(rep, addr, NULL, 0) == 0); + So(nng_dial(req, addr, NULL, 0) == 0); + + // Send req #1 (abc). + So(nng_sendmsg(req, abc, 0) == 0); + + // Sleep a bit. This is so that we ensure that our + // request gets to the far side. (If we cancel too + // fast, then our outgoing send will be canceled before + // it gets to the wire.) + nng_msleep(20); + + nng_aio_set_timeout(aio, 1000); // an entire second + nng_recv_aio(req, aio); + + // Give time for this recv to post properly. + nng_msleep(20); + + // Send the next next request ("def"). Note that + // the REP side server will have already buffered the receive + // request, and should simply be waiting for us to reply to + // abc. + So(nng_sendmsg(req, def, 0) == 0); + + nng_aio_wait(aio); + So(nng_aio_result(aio) == NNG_ECANCELED); + + // Receive the first request (should be abc) on the REP server. + So(nng_recvmsg(rep, &cmd, 0) == 0); + So(nng_msg_len(cmd) == 4); + So(strcmp(nng_msg_body(cmd), "abc") == 0); + + // REP sends the reply to first command. This will be + // discarded by the REQ server. + So(nng_sendmsg(rep, cmd, 0) == 0); + + // Now get the next command from the REP; should be "def". + So(nng_recvmsg(rep, &cmd, 0) == 0); + So(nng_msg_len(cmd) == 4); + So(strcmp(nng_msg_body(cmd), "def") == 0); + + // And send it back to REQ. + So(nng_sendmsg(rep, cmd, 0) == 0); + + // Try a req command. This should give back "def" + So(nng_recvmsg(req, &cmd, 0) == 0); + So(nng_msg_len(cmd) == 4); + So(strcmp(nng_msg_body(cmd), "def") == 0); + nng_msg_free(cmd); + }); + nng_fini(); }) |
