1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/*
Copyright (c) 2012 Martin Sustrik All rights reserved.
Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
Copyright 2018 Capitar IT Group BV <info@capitar.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <nng/compat/nanomsg/nn.h>
#include <nng/compat/nanomsg/reqrep.h>
#include "compat_testutil.h"
#define SOCKET_ADDRESS "inproc://test"
int main ()
{
int rc;
int rep1;
int rep2;
int req1;
int req2;
int resend_ivl;
char buf [7];
int timeo;
/* Test req/rep with full socket types. */
rep1 = test_socket (AF_SP, NN_REP);
test_bind (rep1, SOCKET_ADDRESS);
req1 = test_socket (AF_SP, NN_REQ);
test_connect (req1, SOCKET_ADDRESS);
req2 = test_socket (AF_SP, NN_REQ);
test_connect (req2, SOCKET_ADDRESS);
/* Check invalid sequence of sends and recvs. */
rc = nn_send (rep1, "ABC", 3, 0);
nn_assert (rc == -1 && nn_errno () == EFSM);
rc = nn_recv (req1, buf, sizeof (buf), 0);
nn_assert (rc == -1 && nn_errno () == EFSM);
/* Check fair queueing the requests. */
test_send (req2, "ABC");
test_recv (rep1, "ABC");
test_send (rep1, "ABC");
test_recv (req2, "ABC");
test_send (req1, "ABC");
test_recv (rep1, "ABC");
test_send (rep1, "ABC");
test_recv (req1, "ABC");
test_close (rep1);
test_close (req1);
test_close (req2);
/* Check load-balancing of requests. */
req1 = test_socket (AF_SP, NN_REQ);
test_bind (req1, SOCKET_ADDRESS);
rep1 = test_socket (AF_SP, NN_REP);
test_connect (rep1, SOCKET_ADDRESS);
nn_sleep(10); // ensure rep1 binds before rep2
rep2 = test_socket (AF_SP, NN_REP);
test_connect (rep2, SOCKET_ADDRESS);
timeo = 500;
test_setsockopt (req1, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo));
test_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo));
test_setsockopt (rep2, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo));
// Give time for connections to settle -- required for LB stuff.
nn_sleep(100);
test_send (req1, "ABC");
test_recv (rep1, "ABC");
test_send (rep1, "ABC");
test_recv (req1, "ABC");
test_send (req1, "ABC");
test_recv (rep2, "ABC");
test_send (rep2, "ABC");
test_recv (req1, "ABC");
test_close (rep2);
test_close (rep1);
test_close (req1);
/* Test re-sending of the request. */
rep1 = test_socket (AF_SP, NN_REP);
test_bind (rep1, SOCKET_ADDRESS);
req1 = test_socket (AF_SP, NN_REQ);
test_connect (req1, SOCKET_ADDRESS);
resend_ivl = 100;
rc = nn_setsockopt (req1, NN_REQ, NN_REQ_RESEND_IVL,
&resend_ivl, sizeof (resend_ivl));
errno_assert (rc == 0);
test_send (req1, "ABC");
test_recv (rep1, "ABC");
/* The following waits for request to be resent */
test_recv (rep1, "ABC");
test_close (req1);
test_close (rep1);
/* Check sending a request when the peer is not available. (It should
be sent immediately when the peer comes online rather than relying
on the resend algorithm.) */
req1 = test_socket (AF_SP, NN_REQ);
timeo = 10;
test_setsockopt (req1, NN_SOL_SOCKET, NN_RECONNECT_IVL, &timeo, sizeof (timeo));
test_connect (req1, SOCKET_ADDRESS);
rep1 = test_socket (AF_SP, NN_REP);
test_bind (rep1, SOCKET_ADDRESS);
timeo = 500;
test_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo));
errno_assert (rc == 0);
test_send (req1, "ABC");
/* Now close the peer, and reopen it. This should still work. */
test_close(rep1);
rep1 = test_socket (AF_SP, NN_REP);
test_bind (rep1, SOCKET_ADDRESS);
test_recv (rep1, "ABC");
test_close (req1);
test_close (rep1);
/* Check removing socket request sent to (It should
be sent immediatelly to other peer rather than relying
on the resend algorithm). */
req1 = test_socket (AF_SP, NN_REQ);
test_bind (req1, SOCKET_ADDRESS);
rep1 = test_socket (AF_SP, NN_REP);
test_connect (rep1, SOCKET_ADDRESS);
rep2 = test_socket (AF_SP, NN_REP);
nn_sleep (10); // give time for rep1 to connect
test_connect (rep2, SOCKET_ADDRESS);
timeo = 500; // Was 200, but Windows occasionally fails at that rate.
test_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo));
test_setsockopt (rep2, NN_SOL_SOCKET, NN_RCVTIMEO, &timeo, sizeof (timeo));
test_send (req1, "ABC");
/* We got request through rep1 */
test_recv (rep1, "ABC");
/* But instead replying we simulate crash */
test_close (rep1);
/* The rep2 should get request immediately */
test_recv (rep2, "ABC");
/* Let's check it's delivered well */
test_send (rep2, "REPLY");
test_recv (req1, "REPLY");
test_close (req1);
test_close (rep2);
/* Test cancelling delayed request */
req1 = test_socket (AF_SP, NN_REQ);
test_connect (req1, SOCKET_ADDRESS);
rep1 = test_socket (AF_SP, NN_REP);
test_bind (rep1, SOCKET_ADDRESS);
test_send (req1, "ABC");
test_send (req1, "DEF");
test_close (rep1);
rep1 = test_socket (AF_SP, NN_REP);
test_bind (rep1, SOCKET_ADDRESS);
timeo = 100;
test_recv (rep1, "DEF");
test_close (req1);
test_close (rep1);
return 0;
}
|