libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_req_relaxed.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 
32 static void bounce (void *socket)
33 {
34  int more;
35  size_t more_size = sizeof (more);
36  do {
37  zmq_msg_t recv_part, sent_part;
38  int rc = zmq_msg_init (&recv_part);
39  assert (rc == 0);
40 
41  rc = zmq_msg_recv (&recv_part, socket, 0);
42  assert (rc != -1);
43 
44  rc = zmq_getsockopt (socket, ZMQ_RCVMORE, &more, &more_size);
45  assert (rc == 0);
46 
47  zmq_msg_init (&sent_part);
48  zmq_msg_copy (&sent_part, &recv_part);
49 
50  rc = zmq_msg_send (&sent_part, socket, more ? ZMQ_SNDMORE : 0);
51  assert (rc != -1);
52 
53  zmq_msg_close (&recv_part);
54  } while (more);
55 }
56 
57 int main (void)
58 {
60  void *ctx = zmq_ctx_new ();
61  assert (ctx);
62 
63  void *req = zmq_socket (ctx, ZMQ_REQ);
64  assert (req);
65 
66  int enabled = 1;
67  int rc = zmq_setsockopt (req, ZMQ_REQ_RELAXED, &enabled, sizeof (int));
68  assert (rc == 0);
69 
70  rc = zmq_setsockopt (req, ZMQ_REQ_CORRELATE, &enabled, sizeof (int));
71  assert (rc == 0);
72 
73  rc = zmq_bind (req, "tcp://127.0.0.1:5555");
74  assert (rc == 0);
75 
76  const size_t services = 5;
77  void *rep [services];
78  for (size_t peer = 0; peer < services; peer++) {
79  rep [peer] = zmq_socket (ctx, ZMQ_REP);
80  assert (rep [peer]);
81 
82  int timeout = 500;
83  rc = zmq_setsockopt (rep [peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
84  assert (rc == 0);
85 
86  rc = zmq_connect (rep [peer], "tcp://localhost:5555");
87  assert (rc == 0);
88  }
89  // We have to give the connects time to finish otherwise the requests
90  // will not properly round-robin. We could alternatively connect the
91  // REQ sockets to the REP sockets.
93 
94  // Case 1: Second send() before a reply arrives in a pipe.
95 
96  // Send a request, ensure it arrives, don't send a reply
97  s_send_seq (req, "A", "B", SEQ_END);
98  s_recv_seq (rep [0], "A", "B", SEQ_END);
99 
100  // Send another request on the REQ socket
101  s_send_seq (req, "C", "D", SEQ_END);
102  s_recv_seq (rep [1], "C", "D", SEQ_END);
103 
104  // Send a reply to the first request - that should be discarded by the REQ
105  s_send_seq (rep [0], "WRONG", SEQ_END);
106 
107  // Send the expected reply
108  s_send_seq (rep [1], "OK", SEQ_END);
109  s_recv_seq (req, "OK", SEQ_END);
110 
111 
112  // Another standard req-rep cycle, just to check
113  s_send_seq (req, "E", SEQ_END);
114  s_recv_seq (rep [2], "E", SEQ_END);
115  s_send_seq (rep [2], "F", "G", SEQ_END);
116  s_recv_seq (req, "F", "G", SEQ_END);
117 
118 
119  // Case 2: Second send() after a reply is already in a pipe on the REQ.
120 
121  // Send a request, ensure it arrives, send a reply
122  s_send_seq (req, "H", SEQ_END);
123  s_recv_seq (rep [3], "H", SEQ_END);
124  s_send_seq (rep [3], "BAD", SEQ_END);
125 
126  // Wait for message to be there.
128 
129  // Without receiving that reply, send another request on the REQ socket
130  s_send_seq (req, "I", SEQ_END);
131  s_recv_seq (rep [4], "I", SEQ_END);
132 
133  // Send the expected reply
134  s_send_seq (rep [4], "GOOD", SEQ_END);
135  s_recv_seq (req, "GOOD", SEQ_END);
136 
137  // Case 3: Check issue #1690. Two send() in a row should not close the
138  // communication pipes. For example pipe from req to rep[0] should not be
139  // closed after executing Case 1. So rep[0] should be the next to receive,
140  // not rep[1].
141  s_send_seq (req, "J", SEQ_END);
142  s_recv_seq (rep [0], "J", SEQ_END);
143 
144  close_zero_linger (req);
145  for (size_t peer = 0; peer < services; peer++)
146  close_zero_linger (rep [peer]);
147 
148  // Wait for disconnects.
150 
151  // Case 4: Check issue #1695. As messages may pile up before a responder
152  // is available, we check that responses to messages other than the last
153  // sent one are correctly discarded by the REQ pipe
154 
155  // Setup REQ socket as client
156  req = zmq_socket (ctx, ZMQ_REQ);
157  assert (req);
158 
159  rc = zmq_setsockopt (req, ZMQ_REQ_RELAXED, &enabled, sizeof (int));
160  assert (rc == 0);
161 
162  rc = zmq_setsockopt (req, ZMQ_REQ_CORRELATE, &enabled, sizeof (int));
163  assert (rc == 0);
164 
165  rc = zmq_connect (req, "tcp://localhost:5555");
166  assert (rc == 0);
167 
168  // Setup ROUTER socket as server but do not bind it just yet
169  void *router = zmq_socket (ctx, ZMQ_ROUTER);
170  assert (router);
171 
172  // Send two requests
173  s_send_seq (req, "TO_BE_DISCARDED", SEQ_END);
174  s_send_seq (req, "TO_BE_ANSWERED", SEQ_END);
175 
176  // Bind server allowing it to receive messages
177  rc = zmq_bind (router, "tcp://127.0.0.1:5555");
178  assert (rc == 0);
179 
180  // Read the two messages and send them back as is
181  bounce (router);
182  bounce (router);
183 
184  // Read the expected correlated reply. As the ZMQ_REQ_CORRELATE is active,
185  // the expected answer is "TO_BE_ANSWERED", not "TO_BE_DISCARDED".
186  s_recv_seq (req, "TO_BE_ANSWERED", SEQ_END);
187 
188  close_zero_linger (req);
189  close_zero_linger (router);
190 
191  // Wait for disconnects.
193 
194  rc = zmq_ctx_term (ctx);
195  assert (rc == 0);
196 
197  return 0 ;
198 }
void msleep(int milliseconds)
Definition: testutil.hpp:316
ZMQ_EXPORT int zmq_setsockopt(void *s, int option, const void *optval, size_t optvallen)
Definition: zmq.cpp:265
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_REP
Definition: zmq.h:250
void setup_test_environment(void)
Definition: testutil.hpp:285
ZMQ_EXPORT int zmq_msg_copy(zmq_msg_t *dest, zmq_msg_t *src)
Definition: zmq.cpp:661
#define SETTLE_TIME
Definition: testutil.hpp:44
#define ZMQ_ROUTER
Definition: zmq.h:252
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
ZMQ_EXPORT int zmq_getsockopt(void *s, int option, void *optval, size_t *optvallen)
Definition: zmq.cpp:277
#define ZMQ_REQ
Definition: zmq.h:249
ZMQ_EXPORT int zmq_msg_send(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:629
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
static void bounce(void *socket)
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
Definition: zmq.h:221
#define ZMQ_RCVMORE
Definition: zmq.h:272
void close_zero_linger(void *socket)
Definition: testutil.hpp:275
ZMQ_EXPORT int zmq_bind(void *s, const char *addr)
Definition: zmq.cpp:321
void s_recv_seq(void *socket,...)
Definition: testutil.hpp:236
ZMQ_EXPORT int zmq_ctx_term(void *context)
Definition: zmq.cpp:162
const char * SEQ_END
Definition: testutil.hpp:198
ZMQ_EXPORT int zmq_msg_close(zmq_msg_t *msg)
Definition: zmq.cpp:651
#define ZMQ_REQ_RELAXED
Definition: zmq.h:306
void s_send_seq(void *socket,...)
Definition: testutil.hpp:205
#define ZMQ_RCVTIMEO
Definition: zmq.h:284
#define ZMQ_REQ_CORRELATE
Definition: zmq.h:305
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613
int main(void)