libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
test_spec_router.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 const char *bind_address = 0;
33 const char *connect_address = 0;
34 
35 void test_fair_queue_in (void *ctx)
36 {
37  void *receiver = zmq_socket (ctx, ZMQ_ROUTER);
38  assert (receiver);
39 
40  int timeout = 250;
41  int rc = zmq_setsockopt (receiver, ZMQ_RCVTIMEO, &timeout, sizeof (int));
42  assert (rc == 0);
43 
44  rc = zmq_bind (receiver, bind_address);
45  assert (rc == 0);
46 
47  const size_t services = 5;
48  void *senders [services];
49  for (size_t peer = 0; peer < services; ++peer) {
50  senders [peer] = zmq_socket (ctx, ZMQ_DEALER);
51  assert (senders [peer]);
52 
53  rc = zmq_setsockopt (senders [peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
54  assert (rc == 0);
55 
56  char *str = strdup("A");
57  str [0] += peer;
58  rc = zmq_setsockopt (senders [peer], ZMQ_IDENTITY, str, 2);
59  assert (rc == 0);
60  free (str);
61 
62  rc = zmq_connect (senders [peer], connect_address);
63  assert (rc == 0);
64  }
65 
67 
68  zmq_msg_t msg;
69  rc = zmq_msg_init (&msg);
70  assert (rc == 0);
71 
72  s_send_seq (senders [0], "M", SEQ_END);
73  s_recv_seq (receiver, "A", "M", SEQ_END);
74 
75  s_send_seq (senders [0], "M", SEQ_END);
76  s_recv_seq (receiver, "A", "M", SEQ_END);
77 
78  int sum = 0;
79 
80  // send N requests
81  for (size_t peer = 0; peer < services; ++peer) {
82  s_send_seq (senders [peer], "M", SEQ_END);
83  sum += 'A' + peer;
84  }
85 
86  assert (sum == services * 'A' + services * (services - 1) / 2);
87 
88  // handle N requests
89  for (size_t peer = 0; peer < services; ++peer) {
90  rc = zmq_msg_recv (&msg, receiver, 0);
91  assert (rc == 2);
92  const char *id = (const char *)zmq_msg_data (&msg);
93  sum -= id [0];
94 
95  s_recv_seq (receiver, "M", SEQ_END);
96  }
97 
98  assert (sum == 0);
99 
100  rc = zmq_msg_close (&msg);
101  assert (rc == 0);
102 
103  close_zero_linger (receiver);
104 
105  for (size_t peer = 0; peer < services; ++peer)
106  close_zero_linger (senders [peer]);
107 
108  // Wait for disconnects.
110 }
111 
113 {
114  void *A = zmq_socket (ctx, ZMQ_ROUTER);
115  assert (A);
116 
117  int enabled = 1;
118  int rc = zmq_setsockopt (A, ZMQ_ROUTER_MANDATORY, &enabled, sizeof (enabled));
119  assert (rc == 0);
120 
121  rc = zmq_bind (A, bind_address);
122  assert (rc == 0);
123 
124  void *B = zmq_socket (ctx, ZMQ_DEALER);
125  assert (B);
126 
127  rc = zmq_setsockopt (B, ZMQ_IDENTITY, "B", 2);
128  assert (rc == 0);
129 
130  rc = zmq_connect (B, connect_address);
131  assert (rc == 0);
132 
133  // Wait for connection.
135 
136  // Send a message in both directions
137  s_send_seq (A, "B", "ABC", SEQ_END);
138  s_send_seq (B, "DEF", SEQ_END);
139 
141  assert (rc == 0);
142 
143  // Disconnect may take time and need command processing.
144  zmq_pollitem_t poller [2] = { { A, 0, 0, 0 }, { B, 0, 0, 0 } };
145  rc = zmq_poll (poller, 2, 100);
146  assert (rc == 0);
147  rc = zmq_poll (poller, 2, 100);
148  assert (rc == 0);
149 
150  // No messages should be available, sending should fail.
151  zmq_msg_t msg;
152  zmq_msg_init (&msg);
153 
154  rc = zmq_send (A, "B", 2, ZMQ_SNDMORE | ZMQ_DONTWAIT);
155  assert (rc == -1);
156  assert (errno == EHOSTUNREACH);
157 
158  rc = zmq_msg_recv (&msg, A, ZMQ_DONTWAIT);
159  assert (rc == -1);
160  assert (errno == EAGAIN);
161 
162  // After a reconnect of B, the messages should still be gone
163  rc = zmq_connect (B, connect_address);
164  assert (rc == 0);
165 
166  rc = zmq_msg_recv (&msg, A, ZMQ_DONTWAIT);
167  assert (rc == -1);
168  assert (errno == EAGAIN);
169 
170  rc = zmq_msg_recv (&msg, B, ZMQ_DONTWAIT);
171  assert (rc == -1);
172  assert (errno == EAGAIN);
173 
174  rc = zmq_msg_close (&msg);
175  assert (rc == 0);
176 
177  close_zero_linger (A);
178  close_zero_linger (B);
179 
180  // Wait for disconnects.
182 }
183 
184 
185 int main (void)
186 {
188  void *ctx = zmq_ctx_new ();
189  assert (ctx);
190 
191  const char *binds [] = { "inproc://a", "tcp://127.0.0.1:5555" };
192  const char *connects [] = { "inproc://a", "tcp://localhost:5555" };
193 
194  for (int transport = 0; transport < 2; ++transport) {
195  bind_address = binds [transport];
196  connect_address = connects [transport];
197 
198  // SHALL receive incoming messages from its peers using a fair-queuing
199  // strategy.
200  test_fair_queue_in (ctx);
201 
202  // SHALL create a double queue when a peer connects to it. If this peer
203  // disconnects, the ROUTER socket SHALL destroy its double queue and SHALL
204  // discard any messages it contains.
205  // *** Test disabled until libzmq does this properly ***
206  // test_destroy_queue_on_disconnect (ctx);
207  }
208 
209  int rc = zmq_ctx_term (ctx);
210  assert (rc == 0);
211 
212  return 0 ;
213 }
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
void test_destroy_queue_on_disconnect(void *ctx)
void test_fair_queue_in(void *ctx)
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:115
#define ZMQ_SNDMORE
Definition: zmq.h:346
#define ZMQ_DEALER
Definition: zmq.h:251
void setup_test_environment(void)
Definition: testutil.hpp:285
#define SETTLE_TIME
Definition: testutil.hpp:44
#define ZMQ_ROUTER
Definition: zmq.h:252
ZMQ_EXPORT void * zmq_msg_data(zmq_msg_t *msg)
Definition: zmq.cpp:666
ZMQ_EXPORT void * zmq_socket(void *, int type)
Definition: zmq.cpp:244
#define ZMQ_ROUTER_MANDATORY
Definition: zmq.h:287
#define ZMQ_IDENTITY
Definition: zmq.h:265
const char * bind_address
#define EHOSTUNREACH
Definition: zmq.h:160
ZMQ_EXPORT int zmq_connect(void *s, const char *addr)
Definition: zmq.cpp:332
static void receiver(void *socket)
int main(void)
ZMQ_EXPORT int zmq_msg_recv(zmq_msg_t *msg, void *s, int flags)
Definition: zmq.cpp:640
Definition: zmq.h:221
const char * connect_address
ZMQ_EXPORT int zmq_poll(zmq_pollitem_t *items, int nitems, long timeout)
Definition: zmq.cpp:748
ZMQ_EXPORT int zmq_send(void *s, const void *buf, size_t len, int flags)
Definition: zmq.cpp:387
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
void s_send_seq(void *socket,...)
Definition: testutil.hpp:205
#define ZMQ_RCVTIMEO
Definition: zmq.h:284
ZMQ_EXPORT int zmq_disconnect(void *s, const char *addr)
Definition: zmq.cpp:353
#define ZMQ_DONTWAIT
Definition: zmq.h:345
ZMQ_EXPORT int zmq_msg_init(zmq_msg_t *msg)
Definition: zmq.cpp:613