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
33SETUP_TEARDOWN_TESTCONTEXT
34
35// Issue 566 describes a problem in libzmq v4.0.0 where a dealer to router
36// connection would fail randomly. The test works when the two sockets are
37// on the same context, and failed when they were on separate contexts.
38// Fixed by https://github.com/zeromq/libzmq/commit/be25cf.
39void test_issue_566 ()
40{
41 char my_endpoint[MAX_SOCKET_STRING];
42 void *ctx1 = zmq_ctx_new ();
43 TEST_ASSERT_NOT_NULL (ctx1);
44
45 void *ctx2 = zmq_ctx_new ();
46 TEST_ASSERT_NOT_NULL (ctx2);
47
48 void *router = zmq_socket (ctx1, ZMQ_ROUTER);
49 int on = 1;
50 TEST_ASSERT_SUCCESS_ERRNO (
51 zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on)));
52 bind_loopback_ipv4 (router, my_endpoint, sizeof (my_endpoint));
53
54 // Repeat often enough to be sure this works as it should
55 for (int cycle = 0; cycle < 100; cycle++) {
56 // Create dealer with unique explicit routing id
57 // We assume the router learns this out-of-band
58 void *dealer = zmq_socket (ctx2, ZMQ_DEALER);
59 // Leave space for NULL char from sprintf, gcc warning
60 char routing_id[11];
61 sprintf (routing_id, "%09d", cycle);
62 TEST_ASSERT_SUCCESS_ERRNO (
63 zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10));
64 int rcvtimeo = 1000;
65 TEST_ASSERT_SUCCESS_ERRNO (
66 zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int)));
67 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
68
69 // Router will try to send to dealer, at short intervals.
70 // It typically takes 2-5 msec for the connection to establish
71 // on a loopback interface, but we'll allow up to one second
72 // before failing the test (e.g. for running on a debugger or
73 // a very slow system).
74 for (int attempt = 0; attempt < 500; attempt++) {
75 zmq_poll (NULL, 0, 2);
76 int rc = zmq_send (router, routing_id, 10, ZMQ_SNDMORE);
77 if (rc == -1 && errno == EHOSTUNREACH)
78 continue;
79 TEST_ASSERT_EQUAL (10, rc);
80 send_string_expect_success (router, "HELLO", 0);
81 break;
82 }
83 recv_string_expect_success (dealer, "HELLO", 0);
84 close_zero_linger (dealer);
85 }
86 zmq_close (router);
87 zmq_ctx_destroy (ctx1);
88 zmq_ctx_destroy (ctx2);
89}
90
91int main ()
92{
93 setup_test_environment ();
94
95 UNITY_BEGIN ();
96 RUN_TEST (test_issue_566);
97 return UNITY_END ();
98}
99