2
Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
4
This file is part of 0MQ.
6
0MQ is free software; you can redistribute it and/or modify it under
7
the terms of the GNU Lesser General Public License as published by
8
the Free Software Foundation; either version 3 of the License, or
9
(at your option) any later version.
11
0MQ is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public License
17
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include "testutil.hpp"
22
static void pusher (void *ctx)
25
void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
26
assert (connectSocket);
27
int rc = zmq_connect (connectSocket, "inproc://a");
31
rc = zmq_send_const (connectSocket, "foobar", 6, 0);
35
rc = zmq_close (connectSocket);
39
void test_bind_before_connect()
41
void *ctx = zmq_ctx_new ();
45
void *bindSocket = zmq_socket (ctx, ZMQ_PAIR);
47
int rc = zmq_bind (bindSocket, "inproc://a");
51
void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
52
assert (connectSocket);
53
rc = zmq_connect (connectSocket, "inproc://a");
57
rc = zmq_send_const (connectSocket, "foobar", 6, 0);
60
// Read pending message
62
rc = zmq_msg_init (&msg);
64
rc = zmq_msg_recv (&msg, bindSocket, 0);
66
void *data = zmq_msg_data (&msg);
67
assert (memcmp ("foobar", data, 6) == 0);
70
rc = zmq_close (connectSocket);
73
rc = zmq_close (bindSocket);
76
rc = zmq_ctx_term (ctx);
80
void test_connect_before_bind()
82
void *ctx = zmq_ctx_new ();
86
void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
87
assert (connectSocket);
88
int rc = zmq_connect (connectSocket, "inproc://a");
92
rc = zmq_send_const (connectSocket, "foobar", 6, 0);
96
void *bindSocket = zmq_socket (ctx, ZMQ_PAIR);
98
rc = zmq_bind (bindSocket, "inproc://a");
101
// Read pending message
103
rc = zmq_msg_init (&msg);
105
rc = zmq_msg_recv (&msg, bindSocket, 0);
107
void *data = zmq_msg_data (&msg);
108
assert (memcmp ("foobar", data, 6) == 0);
111
rc = zmq_close (connectSocket);
114
rc = zmq_close (bindSocket);
117
rc = zmq_ctx_term (ctx);
121
void test_connect_before_bind_pub_sub()
123
void *ctx = zmq_ctx_new ();
127
void *connectSocket = zmq_socket (ctx, ZMQ_PUB);
128
assert (connectSocket);
129
int rc = zmq_connect (connectSocket, "inproc://a");
132
// Queue up some data, this will be dropped
133
rc = zmq_send_const (connectSocket, "before", 6, 0);
137
void *bindSocket = zmq_socket (ctx, ZMQ_SUB);
139
rc = zmq_setsockopt (bindSocket, ZMQ_SUBSCRIBE, "", 0);
141
rc = zmq_bind (bindSocket, "inproc://a");
144
// Wait for pub-sub connection to happen
145
msleep (SETTLE_TIME);
147
// Queue up some data, this not will be dropped
148
rc = zmq_send_const (connectSocket, "after", 6, 0);
151
// Read pending message
153
rc = zmq_msg_init (&msg);
155
rc = zmq_msg_recv (&msg, bindSocket, 0);
157
void *data = zmq_msg_data (&msg);
158
assert (memcmp ("after", data, 5) == 0);
161
rc = zmq_close (connectSocket);
164
rc = zmq_close (bindSocket);
167
rc = zmq_ctx_term (ctx);
171
void test_multiple_connects()
173
const unsigned int no_of_connects = 10;
174
void *ctx = zmq_ctx_new ();
178
void *connectSocket[no_of_connects];
181
for (unsigned int i = 0; i < no_of_connects; ++i)
183
connectSocket [i] = zmq_socket (ctx, ZMQ_PUSH);
184
assert (connectSocket [i]);
185
rc = zmq_connect (connectSocket [i], "inproc://a");
188
// Queue up some data
189
rc = zmq_send_const (connectSocket [i], "foobar", 6, 0);
194
void *bindSocket = zmq_socket (ctx, ZMQ_PULL);
196
rc = zmq_bind (bindSocket, "inproc://a");
199
for (unsigned int i = 0; i < no_of_connects; ++i)
201
// Read pending message
203
rc = zmq_msg_init (&msg);
205
rc = zmq_msg_recv (&msg, bindSocket, 0);
207
void *data = zmq_msg_data (&msg);
208
assert (memcmp ("foobar", data, 6) == 0);
212
for (unsigned int i = 0; i < no_of_connects; ++i)
214
rc = zmq_close (connectSocket [i]);
218
rc = zmq_close (bindSocket);
221
rc = zmq_ctx_term (ctx);
225
void test_multiple_threads()
227
const unsigned int no_of_threads = 30;
228
void *ctx = zmq_ctx_new ();
232
void *threads [no_of_threads];
235
for (unsigned int i = 0; i < no_of_threads; ++i)
237
threads [i] = zmq_threadstart (&pusher, ctx);
241
void *bindSocket = zmq_socket (ctx, ZMQ_PULL);
243
rc = zmq_bind (bindSocket, "inproc://a");
246
for (unsigned int i = 0; i < no_of_threads; ++i)
248
// Read pending message
250
rc = zmq_msg_init (&msg);
252
rc = zmq_msg_recv (&msg, bindSocket, 0);
254
void *data = zmq_msg_data (&msg);
255
assert (memcmp ("foobar", data, 6) == 0);
259
for (unsigned int i = 0; i < no_of_threads; ++i)
261
zmq_threadclose (threads [i]);
264
rc = zmq_close (bindSocket);
267
rc = zmq_ctx_term (ctx);
273
// Create the infrastructure
274
void *ctx = zmq_ctx_new ();
277
void *sc = zmq_socket (ctx, ZMQ_DEALER);
280
int rc = zmq_connect (sc, "inproc://a");
283
void *sb = zmq_socket (ctx, ZMQ_ROUTER);
286
rc = zmq_bind (sb, "inproc://a");
289
// Send 2-part message.
290
rc = zmq_send (sc, "A", 1, ZMQ_SNDMORE);
292
rc = zmq_send (sc, "B", 1, 0);
295
// Identity comes first.
297
rc = zmq_msg_init (&msg);
299
rc = zmq_msg_recv (&msg, sb, 0);
301
int more = zmq_msg_more (&msg);
304
// Then the first part of the message body.
305
rc = zmq_msg_recv (&msg, sb, 0);
307
more = zmq_msg_more (&msg);
310
// And finally, the second part of the message body.
311
rc = zmq_msg_recv (&msg, sb, 0);
313
more = zmq_msg_more (&msg);
316
// Deallocate the infrastructure.
323
rc = zmq_ctx_term (ctx);
327
void test_connect_only ()
329
void *ctx = zmq_ctx_new ();
332
void *connectSocket = zmq_socket (ctx, ZMQ_PUSH);
333
assert (connectSocket);
334
int rc = zmq_connect (connectSocket, "inproc://a");
337
rc = zmq_close (connectSocket);
340
rc = zmq_ctx_term (ctx);
346
setup_test_environment();
348
test_bind_before_connect ();
349
test_connect_before_bind ();
350
test_connect_before_bind_pub_sub ();
351
test_multiple_connects ();
352
test_multiple_threads ();
354
test_connect_only ();