1/*
2 Copyright (c) 2007-2017 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#include "testutil_unity.hpp"
32
33#include <limits.h>
34
35#ifndef _WIN32
36#include <sys/socket.h>
37#include <netinet/in.h>
38#include <arpa/inet.h>
39#include <unistd.h>
40#endif
41
42SETUP_TEARDOWN_TESTCONTEXT
43
44fd_t get_fd (void *socket_)
45{
46 fd_t fd;
47 size_t fd_size = sizeof fd;
48 TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (socket_, ZMQ_FD, &fd, &fd_size));
49 return fd;
50}
51
52void test_null_poller_pointers_destroy_direct ()
53{
54 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_destroy (NULL));
55}
56
57void test_null_poller_pointers_destroy_indirect ()
58{
59 void *null_poller = NULL;
60 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_destroy (&null_poller));
61}
62
63void test_null_poller_pointers_add_direct ()
64{
65 void *socket = test_context_socket (ZMQ_PAIR);
66 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
67 zmq_poller_add (NULL, socket, NULL, ZMQ_POLLIN));
68 test_context_socket_close (socket);
69}
70
71void test_null_poller_pointers_add_indirect ()
72{
73 void *null_poller = NULL;
74 void *socket = test_context_socket (ZMQ_PAIR);
75 TEST_ASSERT_FAILURE_ERRNO (
76 EFAULT, zmq_poller_add (&null_poller, socket, NULL, ZMQ_POLLIN));
77 test_context_socket_close (socket);
78}
79
80void test_null_poller_pointers_modify_direct ()
81{
82 void *socket = test_context_socket (ZMQ_PAIR);
83 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
84 zmq_poller_modify (NULL, socket, ZMQ_POLLIN));
85 test_context_socket_close (socket);
86}
87
88void test_null_poller_pointers_modify_indirect ()
89{
90 void *null_poller = NULL;
91 void *socket = test_context_socket (ZMQ_PAIR);
92 TEST_ASSERT_FAILURE_ERRNO (
93 EFAULT, zmq_poller_modify (&null_poller, socket, ZMQ_POLLIN));
94 test_context_socket_close (socket);
95}
96
97void test_null_poller_pointers_remove_direct ()
98{
99 void *socket = test_context_socket (ZMQ_PAIR);
100 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_remove (NULL, socket));
101 test_context_socket_close (socket);
102}
103
104void test_null_poller_pointers_remove_indirect ()
105{
106 void *null_poller = NULL;
107 void *socket = test_context_socket (ZMQ_PAIR);
108 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
109 zmq_poller_remove (&null_poller, socket));
110 test_context_socket_close (socket);
111}
112
113void test_null_poller_pointers_add_fd_direct ()
114{
115 void *socket = test_context_socket (ZMQ_PAIR);
116 const fd_t fd = get_fd (socket);
117
118 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
119 zmq_poller_add_fd (NULL, fd, NULL, ZMQ_POLLIN));
120 test_context_socket_close (socket);
121}
122
123void test_null_poller_pointers_add_fd_indirect ()
124{
125 void *socket = test_context_socket (ZMQ_PAIR);
126 const fd_t fd = get_fd (socket);
127 void *null_poller = NULL;
128 TEST_ASSERT_FAILURE_ERRNO (
129 EFAULT, zmq_poller_add_fd (&null_poller, fd, NULL, ZMQ_POLLIN));
130 test_context_socket_close (socket);
131}
132
133void test_null_poller_pointers_modify_fd_direct ()
134{
135 void *socket = test_context_socket (ZMQ_PAIR);
136 const fd_t fd = get_fd (socket);
137 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
138 zmq_poller_modify_fd (NULL, fd, ZMQ_POLLIN));
139 test_context_socket_close (socket);
140}
141
142void test_null_poller_pointers_modify_fd_indirect ()
143{
144 void *socket = test_context_socket (ZMQ_PAIR);
145 const fd_t fd = get_fd (socket);
146 void *null_poller = NULL;
147 TEST_ASSERT_FAILURE_ERRNO (
148 EFAULT, zmq_poller_modify_fd (&null_poller, fd, ZMQ_POLLIN));
149 test_context_socket_close (socket);
150}
151
152void test_null_poller_pointers_remove_fd_direct ()
153{
154 void *socket = test_context_socket (ZMQ_PAIR);
155 const fd_t fd = get_fd (socket);
156 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_remove_fd (NULL, fd));
157 test_context_socket_close (socket);
158}
159
160void test_null_poller_pointers_remove_fd_indirect ()
161{
162 void *socket = test_context_socket (ZMQ_PAIR);
163 const fd_t fd = get_fd (socket);
164 void *null_poller = NULL;
165 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_remove_fd (&null_poller, fd));
166 test_context_socket_close (socket);
167}
168
169void test_null_poller_pointers_wait_direct ()
170{
171 zmq_poller_event_t event;
172 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_wait (NULL, &event, 0));
173}
174
175void test_null_poller_pointers_wait_indirect ()
176{
177 zmq_poller_event_t event;
178 void *null_poller = NULL;
179 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
180 zmq_poller_wait (&null_poller, &event, 0));
181}
182
183void test_null_poller_pointers_wait_all_direct ()
184{
185 zmq_poller_event_t event;
186 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
187 zmq_poller_wait_all (NULL, &event, 1, 0));
188}
189
190void test_null_poller_pointers_wait_all_indirect ()
191{
192 zmq_poller_event_t event;
193 void *null_poller = NULL;
194 TEST_ASSERT_FAILURE_ERRNO (
195 EFAULT, zmq_poller_wait_all (&null_poller, &event, 1, 0));
196}
197
198void test_null_poller_pointer_poller_fd ()
199{
200 void *null_poller = NULL;
201 zmq_fd_t fd;
202 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_fd (&null_poller, &fd));
203}
204
205void test_null_socket_pointers ()
206{
207 void *poller = zmq_poller_new ();
208 TEST_ASSERT_NOT_NULL (poller);
209
210 TEST_ASSERT_FAILURE_ERRNO (ENOTSOCK,
211 zmq_poller_add (poller, NULL, NULL, ZMQ_POLLIN));
212
213 TEST_ASSERT_FAILURE_ERRNO (ENOTSOCK,
214 zmq_poller_modify (poller, NULL, ZMQ_POLLIN));
215
216 TEST_ASSERT_FAILURE_ERRNO (ENOTSOCK, zmq_poller_remove (poller, NULL));
217
218 fd_t null_socket_fd = retired_fd;
219
220 TEST_ASSERT_FAILURE_ERRNO (
221 EBADF, zmq_poller_add_fd (poller, null_socket_fd, NULL, ZMQ_POLLIN));
222
223 TEST_ASSERT_FAILURE_ERRNO (
224 EBADF, zmq_poller_modify_fd (poller, null_socket_fd, ZMQ_POLLIN));
225
226 TEST_ASSERT_FAILURE_ERRNO (EBADF,
227 zmq_poller_remove_fd (poller, null_socket_fd));
228
229 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
230}
231
232typedef void (*extra_poller_socket_func_t) (void *poller_, void *socket_);
233
234void test_with_empty_poller (extra_poller_socket_func_t extra_func_)
235{
236 void *socket = test_context_socket (ZMQ_PAIR);
237
238 void *poller = zmq_poller_new ();
239 TEST_ASSERT_NOT_NULL (poller);
240
241 extra_func_ (poller, socket);
242
243 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
244
245 test_context_socket_close (socket);
246}
247
248typedef void (*extra_poller_func_t) (void *poller_);
249
250void test_with_valid_poller (extra_poller_func_t extra_func_)
251{
252 void *socket = test_context_socket (ZMQ_PAIR);
253
254 void *poller = zmq_poller_new ();
255 TEST_ASSERT_NOT_NULL (poller);
256
257 TEST_ASSERT_SUCCESS_ERRNO (
258 zmq_poller_add (poller, socket, NULL, ZMQ_POLLIN));
259
260 extra_func_ (poller);
261
262 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
263
264 test_context_socket_close (socket);
265}
266
267void test_call_poller_fd_no_signaler ()
268{
269 void *socket = test_context_socket (ZMQ_PAIR);
270
271 void *poller = zmq_poller_new ();
272 TEST_ASSERT_NOT_NULL (poller);
273
274 TEST_ASSERT_SUCCESS_ERRNO (
275 zmq_poller_add (poller, socket, NULL, ZMQ_POLLIN));
276
277 zmq_fd_t fd;
278 TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_poller_fd (poller, &fd));
279
280 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
281
282 test_context_socket_close (socket);
283}
284
285void test_call_poller_fd ()
286{
287 void *socket = test_context_socket (ZMQ_CLIENT);
288
289 void *poller = zmq_poller_new ();
290 TEST_ASSERT_NOT_NULL (poller);
291
292 TEST_ASSERT_SUCCESS_ERRNO (
293 zmq_poller_add (poller, socket, NULL, ZMQ_POLLIN));
294
295 zmq_fd_t fd;
296 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_fd (poller, &fd));
297
298 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
299
300 test_context_socket_close (socket);
301}
302
303void call_poller_wait_null_event_fails (void *poller_)
304{
305 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_wait (poller_, NULL, 0));
306}
307
308void call_poller_wait_all_null_event_fails_event_count_nonzero (void *poller_)
309{
310 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
311 zmq_poller_wait_all (poller_, NULL, 1, 0));
312}
313
314void call_poller_wait_all_null_event_fails_event_count_zero (void *poller_)
315{
316#if 0
317 // TODO this causes an assertion, which is not consistent if the number
318 // of events may be 0, the pointer should be allowed to by NULL in that
319 // case too
320 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_wait_all (poller, NULL, 0, 0));
321#endif
322}
323
324#define TEST_CASE_FUNC_PARAM(name, func) \
325 void test_##name () { func (name); }
326
327TEST_CASE_FUNC_PARAM (call_poller_wait_null_event_fails, test_with_valid_poller)
328TEST_CASE_FUNC_PARAM (call_poller_wait_all_null_event_fails_event_count_nonzero,
329 test_with_valid_poller)
330TEST_CASE_FUNC_PARAM (call_poller_wait_all_null_event_fails_event_count_zero,
331 test_with_valid_poller)
332
333void call_poller_add_twice_fails (void *poller_, void *socket_)
334{
335 TEST_ASSERT_SUCCESS_ERRNO (
336 zmq_poller_add (poller_, socket_, NULL, ZMQ_POLLIN));
337
338 // attempt to add the same socket twice
339 TEST_ASSERT_FAILURE_ERRNO (
340 EINVAL, zmq_poller_add (poller_, socket_, NULL, ZMQ_POLLIN));
341
342 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_remove (poller_, socket_));
343}
344
345void call_poller_remove_unregistered_fails (void *poller_, void *socket_)
346{
347 // attempt to remove socket that is not present
348 TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_poller_remove (poller_, socket_));
349}
350
351void call_poller_modify_unregistered_fails (void *poller_, void *socket_)
352{
353 // attempt to modify socket that is not present
354 TEST_ASSERT_FAILURE_ERRNO (
355 EINVAL, zmq_poller_modify (poller_, socket_, ZMQ_POLLIN));
356}
357
358void call_poller_add_no_events (void *poller_, void *socket_)
359{
360 // add a socket with no events initially (may be activated later with
361 // zmq_poller_modify)
362 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_add (poller_, socket_, NULL, 0));
363 // TODO test that no events are signalled
364}
365
366void call_poller_modify_no_events (void *poller_, void *socket_)
367{
368 // deactivates all events for a socket temporarily (may be activated again
369 // later with zmq_poller_modify)
370 zmq_poller_add (poller_, socket_, NULL, ZMQ_POLLIN);
371 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_modify (poller_, socket_, 0));
372 // TODO test that no events are signalled
373}
374
375void call_poller_add_fd_twice_fails (void *poller_, void * /*zeromq_socket*/)
376{
377 fd_t plain_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
378 TEST_ASSERT_SUCCESS_ERRNO (
379 zmq_poller_add_fd (poller_, plain_socket, NULL, ZMQ_POLLIN));
380
381 // attempt to add the same plain socket twice
382 TEST_ASSERT_FAILURE_ERRNO (
383 EINVAL, zmq_poller_add_fd (poller_, plain_socket, NULL, ZMQ_POLLIN));
384
385 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_remove_fd (poller_, plain_socket));
386
387 TEST_ASSERT_SUCCESS_ERRNO (close (plain_socket));
388}
389
390void call_poller_remove_fd_unregistered_fails (void *poller_,
391 void * /*zeromq_socket*/)
392{
393 fd_t plain_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
394
395 // attempt to remove plain socket that is not present
396 TEST_ASSERT_FAILURE_ERRNO (EINVAL,
397 zmq_poller_remove_fd (poller_, plain_socket));
398
399 TEST_ASSERT_SUCCESS_ERRNO (close (plain_socket));
400}
401
402void call_poller_modify_fd_unregistered_fails (void *poller_,
403 void * /*zeromq_socket*/)
404{
405 fd_t plain_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
406
407 // attempt to remove plain socket that is not present
408 TEST_ASSERT_FAILURE_ERRNO (
409 EINVAL, zmq_poller_modify_fd (poller_, plain_socket, ZMQ_POLLIN));
410
411 TEST_ASSERT_SUCCESS_ERRNO (close (plain_socket));
412}
413
414void call_poller_add_invalid_events_fails (void *poller_, void *zeromq_socket_)
415{
416 TEST_ASSERT_FAILURE_ERRNO (
417 EINVAL, zmq_poller_add (poller_, zeromq_socket_, NULL, SHRT_MAX));
418}
419
420void call_poller_modify_invalid_events_fails (void *poller_,
421 void *zeromq_socket_)
422{
423 TEST_ASSERT_SUCCESS_ERRNO (
424 zmq_poller_add (poller_, zeromq_socket_, NULL, 0));
425
426 TEST_ASSERT_FAILURE_ERRNO (
427 EINVAL, zmq_poller_modify (poller_, zeromq_socket_, SHRT_MAX));
428}
429
430void call_poller_add_fd_invalid_events_fails (void *poller_,
431 void * /*zeromq_socket*/)
432{
433 fd_t plain_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
434 TEST_ASSERT_FAILURE_ERRNO (
435 EINVAL, zmq_poller_add_fd (poller_, plain_socket, NULL, SHRT_MAX));
436
437 TEST_ASSERT_SUCCESS_ERRNO (close (plain_socket));
438}
439
440void call_poller_modify_fd_invalid_events_fails (void *poller_,
441 void * /*zeromq_socket*/)
442{
443 fd_t plain_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
444 TEST_ASSERT_SUCCESS_ERRNO (
445 zmq_poller_add_fd (poller_, plain_socket, NULL, 0));
446 TEST_ASSERT_FAILURE_ERRNO (
447 EINVAL, zmq_poller_modify_fd (poller_, plain_socket, SHRT_MAX));
448
449 TEST_ASSERT_SUCCESS_ERRNO (close (plain_socket));
450}
451
452TEST_CASE_FUNC_PARAM (call_poller_add_twice_fails, test_with_empty_poller)
453TEST_CASE_FUNC_PARAM (call_poller_remove_unregistered_fails,
454 test_with_empty_poller)
455TEST_CASE_FUNC_PARAM (call_poller_modify_unregistered_fails,
456 test_with_empty_poller)
457TEST_CASE_FUNC_PARAM (call_poller_add_no_events, test_with_empty_poller)
458TEST_CASE_FUNC_PARAM (call_poller_modify_no_events, test_with_empty_poller)
459TEST_CASE_FUNC_PARAM (call_poller_add_fd_twice_fails, test_with_empty_poller)
460TEST_CASE_FUNC_PARAM (call_poller_remove_fd_unregistered_fails,
461 test_with_empty_poller)
462TEST_CASE_FUNC_PARAM (call_poller_modify_fd_unregistered_fails,
463 test_with_empty_poller)
464
465TEST_CASE_FUNC_PARAM (call_poller_add_invalid_events_fails,
466 test_with_empty_poller)
467TEST_CASE_FUNC_PARAM (call_poller_modify_invalid_events_fails,
468 test_with_empty_poller)
469TEST_CASE_FUNC_PARAM (call_poller_add_fd_invalid_events_fails,
470 test_with_empty_poller)
471TEST_CASE_FUNC_PARAM (call_poller_modify_fd_invalid_events_fails,
472 test_with_empty_poller)
473
474void call_poller_wait_empty_with_timeout_fails (void *poller_,
475 void * /*socket*/)
476{
477 zmq_poller_event_t event;
478 // waiting on poller with no registered sockets should report error
479 TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_poller_wait (poller_, &event, 0));
480}
481
482void call_poller_wait_empty_without_timeout_fails (void *poller_,
483 void * /*socket*/)
484{
485 zmq_poller_event_t event;
486 // this would never be able to return since no socket was registered, and should yield an error
487 TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_poller_wait (poller_, &event, -1));
488}
489
490void call_poller_wait_all_empty_negative_count_fails (void *poller_,
491 void * /*socket*/)
492{
493 zmq_poller_event_t event;
494 TEST_ASSERT_FAILURE_ERRNO (EINVAL,
495 zmq_poller_wait_all (poller_, &event, -1, 0));
496}
497
498void call_poller_wait_all_empty_without_timeout_fails (void *poller_,
499 void * /*socket*/)
500{
501 zmq_poller_event_t event;
502 TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
503 zmq_poller_wait_all (poller_, &event, 0, 0));
504}
505
506void call_poller_wait_all_empty_with_timeout_fails (void *poller_,
507 void * /*socket*/)
508{
509 zmq_poller_event_t event;
510 // this would never be able to return since no socket was registered, and should yield an error
511 TEST_ASSERT_FAILURE_ERRNO (EFAULT,
512 zmq_poller_wait_all (poller_, &event, 0, -1));
513}
514
515TEST_CASE_FUNC_PARAM (call_poller_wait_empty_with_timeout_fails,
516 test_with_empty_poller)
517TEST_CASE_FUNC_PARAM (call_poller_wait_empty_without_timeout_fails,
518 test_with_empty_poller)
519TEST_CASE_FUNC_PARAM (call_poller_wait_all_empty_negative_count_fails,
520 test_with_empty_poller)
521TEST_CASE_FUNC_PARAM (call_poller_wait_all_empty_without_timeout_fails,
522 test_with_empty_poller)
523TEST_CASE_FUNC_PARAM (call_poller_wait_all_empty_with_timeout_fails,
524 test_with_empty_poller)
525
526void test_poll_basic ()
527{
528 // Create few sockets
529 void *vent = test_context_socket (ZMQ_PUSH);
530
531 size_t len = MAX_SOCKET_STRING;
532 char my_endpoint[MAX_SOCKET_STRING];
533 bind_loopback_ipv4 (vent, my_endpoint, len);
534
535 void *sink = test_context_socket (ZMQ_PULL);
536 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sink, my_endpoint));
537
538 // Set up poller
539 void *poller = zmq_poller_new ();
540
541 // register sink
542 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_add (poller, sink, sink, ZMQ_POLLIN));
543
544 // Send a message
545 const char *vent_sink_msg = "H";
546 send_string_expect_success (vent, vent_sink_msg, 0);
547
548 // We expect a message only on the sink
549 zmq_poller_event_t event;
550 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_wait (poller, &event, -1));
551 TEST_ASSERT_EQUAL_PTR (sink, event.socket);
552 TEST_ASSERT_EQUAL_PTR (sink, event.user_data);
553 recv_string_expect_success (sink, vent_sink_msg, 0);
554
555 // We expect timed out
556 TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_poller_wait (poller, &event, 0));
557
558 // Stop polling sink
559 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_remove (poller, sink));
560
561 // Clean up
562 test_context_socket_close (vent);
563 test_context_socket_close (sink);
564 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
565}
566
567void test_poll_fd ()
568{
569 // Create sockets
570 void *vent = test_context_socket (ZMQ_PUSH);
571
572 size_t len = MAX_SOCKET_STRING;
573 char my_endpoint[MAX_SOCKET_STRING];
574 bind_loopback_ipv4 (vent, my_endpoint, len);
575
576 void *bowl = test_context_socket (ZMQ_PULL);
577 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (bowl, my_endpoint));
578
579 // Set up poller
580 void *poller = zmq_poller_new ();
581
582 // Check we can poll an FD
583 const fd_t fd = get_fd (bowl);
584 TEST_ASSERT_SUCCESS_ERRNO (
585 zmq_poller_add_fd (poller, fd, bowl, ZMQ_POLLIN));
586
587 zmq_poller_event_t event;
588 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_wait (poller, &event, 500));
589 TEST_ASSERT_NULL (event.socket);
590 TEST_ASSERT_EQUAL (fd, event.fd);
591 TEST_ASSERT_EQUAL_PTR (bowl, event.user_data);
592 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_remove_fd (poller, fd));
593
594 // Clean up
595 test_context_socket_close (vent);
596 test_context_socket_close (bowl);
597
598 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
599}
600
601void test_poll_client_server ()
602{
603#if defined(ZMQ_SERVER) && defined(ZMQ_CLIENT)
604 // Create sockets
605 void *server = test_context_socket (ZMQ_SERVER);
606
607 size_t len = MAX_SOCKET_STRING;
608 char my_endpoint[MAX_SOCKET_STRING];
609 bind_loopback_ipv4 (server, my_endpoint, len);
610
611 void *client = test_context_socket (ZMQ_CLIENT);
612
613 // Set up poller
614 void *poller = zmq_poller_new ();
615
616 // Polling on thread safe sockets
617 TEST_ASSERT_SUCCESS_ERRNO (
618 zmq_poller_add (poller, server, NULL, ZMQ_POLLIN));
619 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
620
621 const char *client_server_msg = "I";
622 send_string_expect_success (client, client_server_msg, 0);
623
624 zmq_poller_event_t event;
625 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_wait (poller, &event, 500));
626 TEST_ASSERT_EQUAL_PTR (server, event.socket);
627 TEST_ASSERT_NULL (event.user_data);
628 recv_string_expect_success (server, client_server_msg, 0);
629
630 // Polling on pollout
631 TEST_ASSERT_SUCCESS_ERRNO (
632 zmq_poller_modify (poller, server, ZMQ_POLLOUT | ZMQ_POLLIN));
633 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_wait (poller, &event, 0));
634 TEST_ASSERT_EQUAL_PTR (server, event.socket);
635 TEST_ASSERT_NULL (event.user_data);
636 TEST_ASSERT_EQUAL_INT (ZMQ_POLLOUT, event.events);
637
638 // Stop polling server
639 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_remove (poller, server));
640
641 // Clean up
642 test_context_socket_close (server);
643 test_context_socket_close (client);
644
645 TEST_ASSERT_SUCCESS_ERRNO (zmq_poller_destroy (&poller));
646#endif
647}
648
649int main (void)
650{
651 setup_test_environment ();
652
653 UNITY_BEGIN ();
654 RUN_TEST (test_null_poller_pointers_destroy_direct);
655 RUN_TEST (test_null_poller_pointers_destroy_indirect);
656 RUN_TEST (test_null_poller_pointers_add_direct);
657 RUN_TEST (test_null_poller_pointers_add_indirect);
658 RUN_TEST (test_null_poller_pointers_modify_direct);
659 RUN_TEST (test_null_poller_pointers_modify_indirect);
660 RUN_TEST (test_null_poller_pointers_remove_direct);
661 RUN_TEST (test_null_poller_pointers_remove_indirect);
662 RUN_TEST (test_null_poller_pointers_add_fd_direct);
663 RUN_TEST (test_null_poller_pointers_add_fd_indirect);
664 RUN_TEST (test_null_poller_pointers_modify_fd_direct);
665 RUN_TEST (test_null_poller_pointers_modify_fd_indirect);
666 RUN_TEST (test_null_poller_pointers_remove_fd_direct);
667 RUN_TEST (test_null_poller_pointers_remove_fd_indirect);
668 RUN_TEST (test_null_poller_pointers_wait_direct);
669 RUN_TEST (test_null_poller_pointers_wait_indirect);
670 RUN_TEST (test_null_poller_pointers_wait_all_direct);
671 RUN_TEST (test_null_poller_pointers_wait_all_indirect);
672 RUN_TEST (test_null_poller_pointer_poller_fd);
673
674 RUN_TEST (test_null_socket_pointers);
675
676 RUN_TEST (test_call_poller_wait_null_event_fails);
677 RUN_TEST (test_call_poller_wait_all_null_event_fails_event_count_nonzero);
678 RUN_TEST (test_call_poller_wait_all_null_event_fails_event_count_zero);
679
680 RUN_TEST (test_call_poller_add_twice_fails);
681 RUN_TEST (test_call_poller_remove_unregistered_fails);
682 RUN_TEST (test_call_poller_modify_unregistered_fails);
683 RUN_TEST (test_call_poller_add_no_events);
684 RUN_TEST (test_call_poller_modify_no_events);
685 RUN_TEST (test_call_poller_add_fd_twice_fails);
686 RUN_TEST (test_call_poller_remove_fd_unregistered_fails);
687 RUN_TEST (test_call_poller_modify_fd_unregistered_fails);
688 RUN_TEST (test_call_poller_add_invalid_events_fails);
689 RUN_TEST (test_call_poller_modify_invalid_events_fails);
690 RUN_TEST (test_call_poller_add_fd_invalid_events_fails);
691 RUN_TEST (test_call_poller_modify_fd_invalid_events_fails);
692
693 RUN_TEST (test_call_poller_wait_empty_with_timeout_fails);
694 RUN_TEST (test_call_poller_wait_empty_without_timeout_fails);
695 RUN_TEST (test_call_poller_wait_all_empty_negative_count_fails);
696 RUN_TEST (test_call_poller_wait_all_empty_without_timeout_fails);
697 RUN_TEST (test_call_poller_wait_all_empty_with_timeout_fails);
698
699 RUN_TEST (test_call_poller_fd_no_signaler);
700 RUN_TEST (test_call_poller_fd);
701
702 RUN_TEST (test_poll_basic);
703 RUN_TEST (test_poll_fd);
704 RUN_TEST (test_poll_client_server);
705
706 return UNITY_END ();
707}
708