test_router_mandatory.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_unity.hpp"
  26. #include <string.h>
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. #ifdef ZMQ_BUILD_DRAFT_API
  29. bool send_msg_to_peer_if_ready (void *router_, const char *peer_routing_id_)
  30. {
  31. int rc = TEST_ASSERT_SUCCESS_MESSAGE_ERRNO (
  32. zmq_socket_get_peer_state (router_, peer_routing_id_, 1),
  33. peer_routing_id_);
  34. if (rc & ZMQ_POLLOUT) {
  35. send_string_expect_success (router_, peer_routing_id_,
  36. ZMQ_SNDMORE | ZMQ_DONTWAIT);
  37. send_string_expect_success (router_, "Hello", ZMQ_DONTWAIT);
  38. return true;
  39. }
  40. return false;
  41. }
  42. #endif
  43. void test_get_peer_state ()
  44. {
  45. #ifdef ZMQ_BUILD_DRAFT_API
  46. void *router = test_context_socket (ZMQ_ROUTER);
  47. int mandatory = 1;
  48. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
  49. &mandatory, sizeof (mandatory)));
  50. const char *my_endpoint = "inproc://test_get_peer_state";
  51. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, my_endpoint));
  52. void *dealer1 = test_context_socket (ZMQ_DEALER);
  53. void *dealer2 = test_context_socket (ZMQ_DEALER);
  54. // Lower HWMs to allow doing the test with fewer messages
  55. const int hwm = 100;
  56. TEST_ASSERT_SUCCESS_ERRNO (
  57. zmq_setsockopt (router, ZMQ_SNDHWM, &hwm, sizeof (int)));
  58. TEST_ASSERT_SUCCESS_ERRNO (
  59. zmq_setsockopt (dealer1, ZMQ_RCVHWM, &hwm, sizeof (int)));
  60. TEST_ASSERT_SUCCESS_ERRNO (
  61. zmq_setsockopt (dealer2, ZMQ_RCVHWM, &hwm, sizeof (int)));
  62. const char *dealer1_routing_id = "X";
  63. const char *dealer2_routing_id = "Y";
  64. // Name dealer1 "X" and connect it to our router
  65. TEST_ASSERT_SUCCESS_ERRNO (
  66. zmq_setsockopt (dealer1, ZMQ_ROUTING_ID, dealer1_routing_id, 1));
  67. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer1, my_endpoint));
  68. // Name dealer2 "Y" and connect it to our router
  69. TEST_ASSERT_SUCCESS_ERRNO (
  70. zmq_setsockopt (dealer2, ZMQ_ROUTING_ID, dealer2_routing_id, 1));
  71. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer2, my_endpoint));
  72. // Get message from both dealers to know when connection is ready
  73. send_string_expect_success (dealer1, "Hello", 0);
  74. recv_string_expect_success (router, dealer1_routing_id, 0);
  75. recv_string_expect_success (router, "Hello", 0);
  76. send_string_expect_success (dealer2, "Hello", 0);
  77. recv_string_expect_success (router, dealer2_routing_id, 0);
  78. recv_string_expect_success (router, "Hello", 0);
  79. void *poller = zmq_poller_new ();
  80. TEST_ASSERT_NOT_NULL (poller);
  81. // Poll on router and dealer1, but not on dealer2
  82. TEST_ASSERT_SUCCESS_ERRNO (
  83. zmq_poller_add (poller, router, NULL, ZMQ_POLLOUT));
  84. TEST_ASSERT_SUCCESS_ERRNO (
  85. zmq_poller_add (poller, dealer1, NULL, ZMQ_POLLIN));
  86. const unsigned int count = 10000;
  87. const unsigned int event_size = 2;
  88. bool dealer2_blocked = false;
  89. unsigned int dealer1_sent = 0, dealer2_sent = 0, dealer1_received = 0;
  90. zmq_poller_event_t events[event_size];
  91. for (unsigned int iteration = 0; iteration < count; ++iteration) {
  92. TEST_ASSERT_SUCCESS_ERRNO (
  93. zmq_poller_wait_all (poller, events, event_size, -1));
  94. for (unsigned int event_no = 0; event_no < event_size; ++event_no) {
  95. const zmq_poller_event_t &current_event = events[event_no];
  96. if (current_event.socket == router
  97. && current_event.events & ZMQ_POLLOUT) {
  98. if (send_msg_to_peer_if_ready (router, dealer1_routing_id))
  99. ++dealer1_sent;
  100. if (send_msg_to_peer_if_ready (router, dealer2_routing_id))
  101. ++dealer2_sent;
  102. else
  103. dealer2_blocked = true;
  104. }
  105. if (current_event.socket == dealer1
  106. && current_event.events & ZMQ_POLLIN) {
  107. recv_string_expect_success (dealer1, "Hello", ZMQ_DONTWAIT);
  108. int more;
  109. size_t more_size = sizeof (more);
  110. TEST_ASSERT_SUCCESS_ERRNO (
  111. zmq_getsockopt (dealer1, ZMQ_RCVMORE, &more, &more_size));
  112. TEST_ASSERT_FALSE (more);
  113. ++dealer1_received;
  114. }
  115. // never read from dealer2, so its pipe becomes full eventually
  116. }
  117. }
  118. printf ("dealer1_sent = %u, dealer2_sent = %u, dealer1_received = %u\n",
  119. dealer1_sent, dealer2_sent, dealer1_received);
  120. TEST_ASSERT_TRUE (dealer2_blocked);
  121. zmq_poller_destroy (&poller);
  122. test_context_socket_close (router);
  123. test_context_socket_close (dealer1);
  124. test_context_socket_close (dealer2);
  125. #endif
  126. }
  127. void test_get_peer_state_corner_cases ()
  128. {
  129. #ifdef ZMQ_BUILD_DRAFT_API
  130. const char peer_routing_id[] = "foo";
  131. // call get_peer_state with NULL socket
  132. int rc = zmq_socket_get_peer_state (NULL, peer_routing_id,
  133. strlen (peer_routing_id));
  134. TEST_ASSERT_EQUAL_INT (-1, rc);
  135. TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno);
  136. void *dealer = test_context_socket (ZMQ_DEALER);
  137. void *router = test_context_socket (ZMQ_ROUTER);
  138. // call get_peer_state with a non-ROUTER socket
  139. rc = zmq_socket_get_peer_state (dealer, peer_routing_id,
  140. strlen (peer_routing_id));
  141. TEST_ASSERT_EQUAL_INT (-1, rc);
  142. TEST_ASSERT_EQUAL_INT (ENOTSUP, errno);
  143. // call get_peer_state for an unknown routing id
  144. rc = zmq_socket_get_peer_state (router, peer_routing_id,
  145. strlen (peer_routing_id));
  146. TEST_ASSERT_EQUAL_INT (-1, rc);
  147. TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
  148. test_context_socket_close (router);
  149. test_context_socket_close (dealer);
  150. #endif
  151. }
  152. void test_basic ()
  153. {
  154. char my_endpoint[MAX_SOCKET_STRING];
  155. void *router = test_context_socket (ZMQ_ROUTER);
  156. bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
  157. // Send a message to an unknown peer with the default setting
  158. // This will not report any error
  159. send_string_expect_success (router, "UNKNOWN", ZMQ_SNDMORE);
  160. send_string_expect_success (router, "DATA", 0);
  161. // Send a message to an unknown peer with mandatory routing
  162. // This will fail
  163. int mandatory = 1;
  164. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
  165. &mandatory, sizeof (mandatory)));
  166. int rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
  167. TEST_ASSERT_EQUAL_INT (-1, rc);
  168. TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
  169. // Create dealer called "X" and connect it to our router
  170. void *dealer = test_context_socket (ZMQ_DEALER);
  171. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
  172. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
  173. // Get message from dealer to know when connection is ready
  174. send_string_expect_success (dealer, "Hello", 0);
  175. recv_string_expect_success (router, "X", 0);
  176. // Send a message to connected dealer now
  177. // It should work
  178. send_string_expect_success (router, "X", ZMQ_SNDMORE);
  179. send_string_expect_success (router, "Hello", 0);
  180. test_context_socket_close (router);
  181. test_context_socket_close (dealer);
  182. }
  183. int main (void)
  184. {
  185. setup_test_environment ();
  186. UNITY_BEGIN ();
  187. RUN_TEST (test_basic);
  188. RUN_TEST (test_get_peer_state);
  189. RUN_TEST (test_get_peer_state_corner_cases);
  190. return UNITY_END ();
  191. }