2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2013-09-26 14:18:40 +02:00
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2018-12-25 13:20:04 +01:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2013-09-26 14:18:40 +02:00
|
|
|
|
|
|
|
// Issue 566 describes a problem in libzmq v4.0.0 where a dealer to router
|
|
|
|
// connection would fail randomly. The test works when the two sockets are
|
|
|
|
// on the same context, and failed when they were on separate contexts.
|
|
|
|
// Fixed by https://github.com/zeromq/libzmq/commit/be25cf.
|
2018-12-25 13:20:04 +01:00
|
|
|
void test_issue_566 ()
|
2013-09-26 14:18:40 +02:00
|
|
|
{
|
2017-05-01 13:11:11 +02:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2013-09-26 14:18:40 +02:00
|
|
|
void *ctx1 = zmq_ctx_new ();
|
2018-12-25 13:20:04 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (ctx1);
|
2013-09-26 14:18:40 +02:00
|
|
|
|
|
|
|
void *ctx2 = zmq_ctx_new ();
|
2018-12-25 13:20:04 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (ctx2);
|
2013-09-26 14:18:40 +02:00
|
|
|
|
|
|
|
void *router = zmq_socket (ctx1, ZMQ_ROUTER);
|
|
|
|
int on = 1;
|
2018-12-25 13:20:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on)));
|
|
|
|
bind_loopback_ipv4 (router, my_endpoint, sizeof (my_endpoint));
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2013-09-26 14:18:40 +02:00
|
|
|
// Repeat often enough to be sure this works as it should
|
|
|
|
for (int cycle = 0; cycle < 100; cycle++) {
|
2017-09-07 11:21:13 +02:00
|
|
|
// Create dealer with unique explicit routing id
|
2013-09-26 14:18:40 +02:00
|
|
|
// We assume the router learns this out-of-band
|
|
|
|
void *dealer = zmq_socket (ctx2, ZMQ_DEALER);
|
2019-01-15 00:49:28 +01:00
|
|
|
// Leave space for NULL char from sprintf, gcc warning
|
|
|
|
char routing_id[11];
|
2023-01-24 12:54:48 +01:00
|
|
|
snprintf (routing_id, 11 * sizeof (char), "%09d", cycle);
|
2018-12-25 13:20:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10));
|
2013-09-26 14:18:40 +02:00
|
|
|
int rcvtimeo = 1000;
|
2018-12-25 13:20:04 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
|
2013-09-26 14:18:40 +02:00
|
|
|
|
|
|
|
// Router will try to send to dealer, at short intervals.
|
|
|
|
// It typically takes 2-5 msec for the connection to establish
|
|
|
|
// on a loopback interface, but we'll allow up to one second
|
|
|
|
// before failing the test (e.g. for running on a debugger or
|
|
|
|
// a very slow system).
|
|
|
|
for (int attempt = 0; attempt < 500; attempt++) {
|
2018-12-25 13:20:04 +01:00
|
|
|
zmq_poll (NULL, 0, 2);
|
|
|
|
int rc = zmq_send (router, routing_id, 10, ZMQ_SNDMORE);
|
2013-09-26 14:18:40 +02:00
|
|
|
if (rc == -1 && errno == EHOSTUNREACH)
|
|
|
|
continue;
|
2018-12-25 13:20:04 +01:00
|
|
|
TEST_ASSERT_EQUAL (10, rc);
|
|
|
|
send_string_expect_success (router, "HELLO", 0);
|
2013-09-26 14:18:40 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-12-25 13:20:04 +01:00
|
|
|
recv_string_expect_success (dealer, "HELLO", 0);
|
2013-09-26 14:18:40 +02:00
|
|
|
close_zero_linger (dealer);
|
|
|
|
}
|
|
|
|
zmq_close (router);
|
|
|
|
zmq_ctx_destroy (ctx1);
|
|
|
|
zmq_ctx_destroy (ctx2);
|
2018-12-25 13:20:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
2013-09-26 14:18:40 +02:00
|
|
|
|
2018-12-25 13:20:04 +01:00
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_issue_566);
|
|
|
|
return UNITY_END ();
|
2013-09-26 14:18:40 +02:00
|
|
|
}
|