test_issue_566.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
7 
8 // Issue 566 describes a problem in libzmq v4.0.0 where a dealer to router
9 // connection would fail randomly. The test works when the two sockets are
10 // on the same context, and failed when they were on separate contexts.
11 // Fixed by https://github.com/zeromq/libzmq/commit/be25cf.
13 {
15  void *ctx1 = zmq_ctx_new ();
16  TEST_ASSERT_NOT_NULL (ctx1);
17 
18  void *ctx2 = zmq_ctx_new ();
19  TEST_ASSERT_NOT_NULL (ctx2);
20 
21  void *router = zmq_socket (ctx1, ZMQ_ROUTER);
22  int on = 1;
24  zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on)));
25  bind_loopback_ipv4 (router, my_endpoint, sizeof (my_endpoint));
26 
27  // Repeat often enough to be sure this works as it should
28  for (int cycle = 0; cycle < 100; cycle++) {
29  // Create dealer with unique explicit routing id
30  // We assume the router learns this out-of-band
31  void *dealer = zmq_socket (ctx2, ZMQ_DEALER);
32  // Leave space for NULL char from sprintf, gcc warning
33  char routing_id[11];
34  snprintf (routing_id, 11 * sizeof (char), "%09d", cycle);
36  zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10));
37  int rcvtimeo = 1000;
39  zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int)));
41 
42  // Router will try to send to dealer, at short intervals.
43  // It typically takes 2-5 msec for the connection to establish
44  // on a loopback interface, but we'll allow up to one second
45  // before failing the test (e.g. for running on a debugger or
46  // a very slow system).
47  for (int attempt = 0; attempt < 500; attempt++) {
48  zmq_poll (NULL, 0, 2);
49  int rc = zmq_send (router, routing_id, 10, ZMQ_SNDMORE);
50  if (rc == -1 && errno == EHOSTUNREACH)
51  continue;
52  TEST_ASSERT_EQUAL (10, rc);
53  send_string_expect_success (router, "HELLO", 0);
54  break;
55  }
56  recv_string_expect_success (dealer, "HELLO", 0);
57  close_zero_linger (dealer);
58  }
59  zmq_close (router);
60  zmq_ctx_destroy (ctx1);
61  zmq_ctx_destroy (ctx2);
62 }
63 
64 int main ()
65 {
67 
68  UNITY_BEGIN ();
70  return UNITY_END ();
71 }
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
zmq_poll
ZMQ_EXPORT int zmq_poll(zmq_pollitem_t *items_, int nitems_, long timeout_)
Definition: zmq.cpp:827
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
zmq_ctx_new
ZMQ_EXPORT void * zmq_ctx_new(void)
Definition: zmq.cpp:109
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
errno
int errno
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
main
int main()
Definition: test_issue_566.cpp:64
close_zero_linger
void close_zero_linger(void *socket_)
Definition: testutil.cpp:193
zmq_ctx_destroy
ZMQ_EXPORT int zmq_ctx_destroy(void *context_)
Definition: zmq.cpp:212
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
testutil_unity.hpp
snprintf
int snprintf(char *str, size_t size, const char *format,...)
Definition: port.cc:64
ZMQ_DEALER
#define ZMQ_DEALER
Definition: zmq.h:263
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
test_issue_566
SETUP_TEARDOWN_TESTCONTEXT void test_issue_566()
Definition: test_issue_566.cpp:12
testutil.hpp
ZMQ_ROUTER
#define ZMQ_ROUTER
Definition: zmq.h:264
my_endpoint
char my_endpoint[MAX_SOCKET_STRING]
Definition: test_security_curve.cpp:31
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
zmq_socket
ZMQ_EXPORT void * zmq_socket(void *, int type_)
Definition: zmq.cpp:230
TEST_ASSERT_EQUAL
#define TEST_ASSERT_EQUAL(expected, actual)
Definition: unity.h:133
zmq_close
ZMQ_EXPORT int zmq_close(void *s_)
Definition: zmq.cpp:241
ZMQ_ROUTER_MANDATORY
#define ZMQ_ROUTER_MANDATORY
Definition: zmq.h:299
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
ZMQ_RCVTIMEO
#define ZMQ_RCVTIMEO
Definition: zmq.h:296
recv_string_expect_success
void recv_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:101
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
ZMQ_SNDMORE
#define ZMQ_SNDMORE
Definition: zmq.h:359
ZMQ_ROUTING_ID
#define ZMQ_ROUTING_ID
Definition: zmq.h:277
EHOSTUNREACH
#define EHOSTUNREACH
Definition: zmq.h:152
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
TEST_ASSERT_NOT_NULL
#define TEST_ASSERT_NOT_NULL(pointer)
Definition: unity.h:125
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59