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