test_router_handover.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 
9 {
11  void *router = test_context_socket (ZMQ_ROUTER);
12  bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
13 
14  // Enable the handover flag
15  int handover = 1;
17  &handover, sizeof (handover)));
18 
19  // Create dealer called "X" and connect it to our router
20  void *dealer_one = test_context_socket (ZMQ_DEALER);
22  zmq_setsockopt (dealer_one, ZMQ_ROUTING_ID, "X", 1));
24 
25  // Get message from dealer to know when connection is ready
26  char buffer[255];
27  send_string_expect_success (dealer_one, "Hello", 0);
28 
29  recv_string_expect_success (router, "X", 0);
30  recv_string_expect_success (router, "Hello", 0);
31 
32  // Now create a second dealer that uses the same routing id
33  void *dealer_two = test_context_socket (ZMQ_DEALER);
35  zmq_setsockopt (dealer_two, ZMQ_ROUTING_ID, "X", 1));
37 
38  // Get message from dealer to know when connection is ready
39  send_string_expect_success (dealer_two, "Hello", 0);
40 
41  recv_string_expect_success (router, "X", 0);
42  recv_string_expect_success (router, "Hello", 0);
43 
44  // Send a message to 'X' routing id. This should be delivered
45  // to the second dealer, instead of the first because of the handover.
47  send_string_expect_success (router, "Hello", 0);
48 
49  // Ensure that the first dealer doesn't receive the message
50  // but the second one does
51  const int timeout = SETTLE_TIME;
53  zmq_setsockopt (dealer_one, ZMQ_RCVTIMEO, &timeout, sizeof timeout));
54  TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (dealer_one, buffer, 255, 0));
55 
56  recv_string_expect_success (dealer_two, "Hello", 0);
57 
59  test_context_socket_close (dealer_one);
60  test_context_socket_close (dealer_two);
61 }
62 
64 {
65  size_t len = MAX_SOCKET_STRING;
67  void *router = test_context_socket (ZMQ_ROUTER);
68 
69  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, "tcp://127.0.0.1:*"));
70 
73 
74  // Create dealer called "X" and connect it to our router
75  void *dealer_one = test_context_socket (ZMQ_DEALER);
77  zmq_setsockopt (dealer_one, ZMQ_ROUTING_ID, "X", 1));
79 
80  // Get message from dealer to know when connection is ready
81  char buffer[255];
82  send_string_expect_success (dealer_one, "Hello", 0);
83 
84  recv_string_expect_success (router, "X", 0);
85  recv_string_expect_success (router, "Hello", 0);
86 
87  // Now create a second dealer that uses the same routing id
88  void *dealer_two = test_context_socket (ZMQ_DEALER);
90  zmq_setsockopt (dealer_two, ZMQ_ROUTING_ID, "X", 1));
92 
93  // Send message from second dealer
94  send_string_expect_success (dealer_two, "Hello", 0);
95 
96  // This should be ignored by the router
97  const int timeout = SETTLE_TIME;
99  zmq_setsockopt (router, ZMQ_RCVTIMEO, &timeout, sizeof timeout));
100  TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (router, buffer, 255, 0));
101 
102  // Send a message to 'X' routing id. This should be delivered
103  // to the second dealer, instead of the first because of the handover.
105  send_string_expect_success (router, "Hello", 0);
106 
107  // Ensure that the second dealer doesn't receive the message
108  // but the first one does
110  zmq_setsockopt (dealer_two, ZMQ_RCVTIMEO, &timeout, sizeof timeout));
111  TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (dealer_two, buffer, 255, 0));
112 
113  recv_string_expect_success (dealer_one, "Hello", 0);
114 
115  test_context_socket_close (router);
116  test_context_socket_close (dealer_one);
117  test_context_socket_close (dealer_two);
118 }
119 
120 int main ()
121 {
123 
124  UNITY_BEGIN ();
127  return UNITY_END ();
128 }
UNITY_END
return UNITY_END()
EAGAIN
#define EAGAIN
Definition: errno.hpp:14
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
ZMQ_LAST_ENDPOINT
#define ZMQ_LAST_ENDPOINT
Definition: zmq.h:298
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
testutil_unity.hpp
ZMQ_DEALER
#define ZMQ_DEALER
Definition: zmq.h:263
ZMQ_ROUTER_HANDOVER
#define ZMQ_ROUTER_HANDOVER
Definition: zmq.h:321
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
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_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
timeout
GLbitfield GLuint64 timeout
Definition: glcorearb.h:3588
test_with_handover
SETUP_TEARDOWN_TESTCONTEXT void test_with_handover()
Definition: test_router_handover.cpp:8
test_without_handover
void test_without_handover()
Definition: test_router_handover.cpp:63
buffer
Definition: buffer_processor.h:43
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
main
int main()
Definition: test_router_handover.cpp:120
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
len
int len
Definition: php/ext/google/protobuf/map.c:206
ZMQ_RCVTIMEO
#define ZMQ_RCVTIMEO
Definition: zmq.h:296
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
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
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
ZMQ_ROUTING_ID
#define ZMQ_ROUTING_ID
Definition: zmq.h:277
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
zmq_getsockopt
ZMQ_EXPORT int zmq_getsockopt(void *s_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:261


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