test_router_mandatory.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 
6 #include <string.h>
7 
9 
10 #ifdef ZMQ_BUILD_DRAFT_API
11 bool send_msg_to_peer_if_ready (void *router_, const char *peer_routing_id_)
12 {
14  zmq_socket_get_peer_state (router_, peer_routing_id_, 1),
15  peer_routing_id_);
16  if (rc & ZMQ_POLLOUT) {
17  send_string_expect_success (router_, peer_routing_id_,
19  send_string_expect_success (router_, "Hello", ZMQ_DONTWAIT);
20 
21  return true;
22  }
23  return false;
24 }
25 #endif
26 
28 {
29 #ifdef ZMQ_BUILD_DRAFT_API
30  void *router = test_context_socket (ZMQ_ROUTER);
31 
32  int mandatory = 1;
34  &mandatory, sizeof (mandatory)));
35 
36  const char *my_endpoint = "inproc://test_get_peer_state";
38 
39  void *dealer1 = test_context_socket (ZMQ_DEALER);
40  void *dealer2 = test_context_socket (ZMQ_DEALER);
41 
42  // Lower HWMs to allow doing the test with fewer messages
43  const int hwm = 100;
45  zmq_setsockopt (router, ZMQ_SNDHWM, &hwm, sizeof (int)));
47  zmq_setsockopt (dealer1, ZMQ_RCVHWM, &hwm, sizeof (int)));
49  zmq_setsockopt (dealer2, ZMQ_RCVHWM, &hwm, sizeof (int)));
50 
51  const char *dealer1_routing_id = "X";
52  const char *dealer2_routing_id = "Y";
53 
54  // Name dealer1 "X" and connect it to our router
56  zmq_setsockopt (dealer1, ZMQ_ROUTING_ID, dealer1_routing_id, 1));
58 
59  // Name dealer2 "Y" and connect it to our router
61  zmq_setsockopt (dealer2, ZMQ_ROUTING_ID, dealer2_routing_id, 1));
63 
64  // Get message from both dealers to know when connection is ready
65  send_string_expect_success (dealer1, "Hello", 0);
66  recv_string_expect_success (router, dealer1_routing_id, 0);
67  recv_string_expect_success (router, "Hello", 0);
68 
69  send_string_expect_success (dealer2, "Hello", 0);
70  recv_string_expect_success (router, dealer2_routing_id, 0);
71  recv_string_expect_success (router, "Hello", 0);
72 
73  void *poller = zmq_poller_new ();
74  TEST_ASSERT_NOT_NULL (poller);
75 
76  // Poll on router and dealer1, but not on dealer2
78  zmq_poller_add (poller, router, NULL, ZMQ_POLLOUT));
80  zmq_poller_add (poller, dealer1, NULL, ZMQ_POLLIN));
81 
82  const unsigned int count = 10000;
83  const unsigned int event_size = 2;
84  bool dealer2_blocked = false;
85  unsigned int dealer1_sent = 0, dealer2_sent = 0, dealer1_received = 0;
86  zmq_poller_event_t events[event_size];
87  for (unsigned int iteration = 0; iteration < count; ++iteration) {
89  zmq_poller_wait_all (poller, events, event_size, -1));
90  for (unsigned int event_no = 0; event_no < event_size; ++event_no) {
91  const zmq_poller_event_t &current_event = events[event_no];
92  if (current_event.socket == router
93  && current_event.events & ZMQ_POLLOUT) {
94  if (send_msg_to_peer_if_ready (router, dealer1_routing_id))
95  ++dealer1_sent;
96 
97  if (send_msg_to_peer_if_ready (router, dealer2_routing_id))
98  ++dealer2_sent;
99  else
100  dealer2_blocked = true;
101  }
102  if (current_event.socket == dealer1
103  && current_event.events & ZMQ_POLLIN) {
104  recv_string_expect_success (dealer1, "Hello", ZMQ_DONTWAIT);
105  int more;
106  size_t more_size = sizeof (more);
108  zmq_getsockopt (dealer1, ZMQ_RCVMORE, &more, &more_size));
109  TEST_ASSERT_FALSE (more);
110 
111  ++dealer1_received;
112  }
113  // never read from dealer2, so its pipe becomes full eventually
114  }
115  }
116  printf ("dealer1_sent = %u, dealer2_sent = %u, dealer1_received = %u\n",
117  dealer1_sent, dealer2_sent, dealer1_received);
118  TEST_ASSERT_TRUE (dealer2_blocked);
119  zmq_poller_destroy (&poller);
120 
121  test_context_socket_close (router);
122  test_context_socket_close (dealer1);
123  test_context_socket_close (dealer2);
124 #endif
125 }
126 
128 {
129 #ifdef ZMQ_BUILD_DRAFT_API
130  const char peer_routing_id[] = "foo";
131 
132  // call get_peer_state with NULL socket
133  int rc = zmq_socket_get_peer_state (NULL, peer_routing_id,
134  strlen (peer_routing_id));
135  TEST_ASSERT_EQUAL_INT (-1, rc);
137 
138  void *dealer = test_context_socket (ZMQ_DEALER);
139  void *router = test_context_socket (ZMQ_ROUTER);
140 
141  // call get_peer_state with a non-ROUTER socket
142  rc = zmq_socket_get_peer_state (dealer, peer_routing_id,
143  strlen (peer_routing_id));
144  TEST_ASSERT_EQUAL_INT (-1, rc);
146 
147  // call get_peer_state for an unknown routing id
148  rc = zmq_socket_get_peer_state (router, peer_routing_id,
149  strlen (peer_routing_id));
150  TEST_ASSERT_EQUAL_INT (-1, rc);
152 
153  test_context_socket_close (router);
154  test_context_socket_close (dealer);
155 #endif
156 }
157 
158 void test_basic ()
159 {
161  void *router = test_context_socket (ZMQ_ROUTER);
162  bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
163 
164  // Send a message to an unknown peer with the default setting
165  // This will not report any error
166  send_string_expect_success (router, "UNKNOWN", ZMQ_SNDMORE);
167  send_string_expect_success (router, "DATA", 0);
168 
169  // Send a message to an unknown peer with mandatory routing
170  // This will fail
171  int mandatory = 1;
173  &mandatory, sizeof (mandatory)));
174  int rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
175  TEST_ASSERT_EQUAL_INT (-1, rc);
177 
178  // Create dealer called "X" and connect it to our router
179  void *dealer = test_context_socket (ZMQ_DEALER);
182 
183  // Get message from dealer to know when connection is ready
184  send_string_expect_success (dealer, "Hello", 0);
185  recv_string_expect_success (router, "X", 0);
186 
187  // Send a message to connected dealer now
188  // It should work
190  send_string_expect_success (router, "Hello", 0);
191 
192  test_context_socket_close (router);
193  test_context_socket_close (dealer);
194 }
195 
196 int main (void)
197 {
199 
200  UNITY_BEGIN ();
204 
205  return UNITY_END ();
206 }
test_basic
void test_basic()
Definition: test_router_mandatory.cpp:158
ENOTSUP
#define ENOTSUP
Definition: zmq.h:104
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
TEST_ASSERT_TRUE
#define TEST_ASSERT_TRUE(condition)
Definition: unity.h:121
test_get_peer_state
SETUP_TEARDOWN_TESTCONTEXT void test_get_peer_state()
Definition: test_router_mandatory.cpp:27
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
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
TEST_ASSERT_FALSE
#define TEST_ASSERT_FALSE(condition)
Definition: unity.h:123
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
zmq_socket_get_peer_state
int zmq_socket_get_peer_state(void *s_, const void *routing_id_, size_t routing_id_size_)
Definition: zmq.cpp:1627
ZMQ_POLLIN
#define ZMQ_POLLIN
Definition: zmq.h:482
zmq_poller_event_t::events
short events
Definition: zmq_draft.h:119
testutil_unity.hpp
ZMQ_POLLOUT
#define ZMQ_POLLOUT
Definition: zmq.h:483
zmq_poller_event_t::socket
void * socket
Definition: zmq_draft.h:116
ZMQ_RCVHWM
#define ZMQ_RCVHWM
Definition: zmq.h:294
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
testutil.hpp
ZMQ_ROUTER
#define ZMQ_ROUTER
Definition: zmq.h:264
zmq_poller_event_t
Definition: zmq_draft.h:114
zmq_poller_new
void * zmq_poller_new(void)
Definition: zmq.cpp:1429
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
ENOTSOCK
#define ENOTSOCK
Definition: zmq.h:128
test_get_peer_state_corner_cases
void test_get_peer_state_corner_cases()
Definition: test_router_mandatory.cpp:127
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
ZMQ_ROUTER_MANDATORY
#define ZMQ_ROUTER_MANDATORY
Definition: zmq.h:299
ZMQ_SNDHWM
#define ZMQ_SNDHWM
Definition: zmq.h:293
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
zmq_poller_add
int zmq_poller_add(void *poller_, void *s_, void *user_data_, short events_)
Definition: zmq.cpp:1509
main
int main(void)
Definition: test_router_mandatory.cpp:196
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
TEST_ASSERT_SUCCESS_MESSAGE_ERRNO
#define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg)
Definition: testutil_unity.hpp:48
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
count
GLint GLsizei count
Definition: glcorearb.h:2830
TEST_ASSERT_NOT_NULL
#define TEST_ASSERT_NOT_NULL(pointer)
Definition: unity.h:125
ZMQ_RCVMORE
#define ZMQ_RCVMORE
Definition: zmq.h:284
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
zmq_poller_destroy
int zmq_poller_destroy(void **poller_p_)
Definition: zmq.cpp:1438
zmq_getsockopt
ZMQ_EXPORT int zmq_getsockopt(void *s_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:261
zmq_poller_wait_all
int zmq_poller_wait_all(void *poller_, zmq_poller_event_t *events_, int n_events_, long timeout_)
Definition: zmq.cpp:1590


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