test_spec_router.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <stdlib.h>
  27. #include <string.h>
  28. SETUP_TEARDOWN_TESTCONTEXT
  29. // SHALL receive incoming messages from its peers using a fair-queuing
  30. // strategy.
  31. void test_fair_queue_in (const char *bind_address_)
  32. {
  33. char connect_address[MAX_SOCKET_STRING];
  34. void *receiver = test_context_socket (ZMQ_ROUTER);
  35. int timeout = 250;
  36. TEST_ASSERT_SUCCESS_ERRNO (
  37. zmq_setsockopt (receiver, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  38. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (receiver, bind_address_));
  39. size_t len = MAX_SOCKET_STRING;
  40. TEST_ASSERT_SUCCESS_ERRNO (
  41. zmq_getsockopt (receiver, ZMQ_LAST_ENDPOINT, connect_address, &len));
  42. const unsigned char services = 5;
  43. void *senders[services];
  44. for (unsigned char peer = 0; peer < services; ++peer) {
  45. senders[peer] = test_context_socket (ZMQ_DEALER);
  46. TEST_ASSERT_SUCCESS_ERRNO (
  47. zmq_setsockopt (senders[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  48. char *str = strdup ("A");
  49. str[0] += peer;
  50. TEST_ASSERT_SUCCESS_ERRNO (
  51. zmq_setsockopt (senders[peer], ZMQ_ROUTING_ID, str, 2));
  52. free (str);
  53. TEST_ASSERT_SUCCESS_ERRNO (
  54. zmq_connect (senders[peer], connect_address));
  55. }
  56. msleep (SETTLE_TIME);
  57. zmq_msg_t msg;
  58. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  59. s_send_seq (senders[0], "M", SEQ_END);
  60. s_recv_seq (receiver, "A", "M", SEQ_END);
  61. s_send_seq (senders[0], "M", SEQ_END);
  62. s_recv_seq (receiver, "A", "M", SEQ_END);
  63. int sum = 0;
  64. // send N requests
  65. for (unsigned char peer = 0; peer < services; ++peer) {
  66. s_send_seq (senders[peer], "M", SEQ_END);
  67. sum += 'A' + peer;
  68. }
  69. TEST_ASSERT_EQUAL_INT (services * 'A' + services * (services - 1) / 2, sum);
  70. // handle N requests
  71. for (unsigned char peer = 0; peer < services; ++peer) {
  72. TEST_ASSERT_EQUAL_INT (
  73. 2, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, receiver, 0)));
  74. const char *id = static_cast<const char *> (zmq_msg_data (&msg));
  75. sum -= id[0];
  76. s_recv_seq (receiver, "M", SEQ_END);
  77. }
  78. TEST_ASSERT_EQUAL_INT (0, sum);
  79. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  80. test_context_socket_close_zero_linger (receiver);
  81. for (size_t peer = 0; peer < services; ++peer)
  82. test_context_socket_close_zero_linger (senders[peer]);
  83. // Wait for disconnects.
  84. msleep (SETTLE_TIME);
  85. }
  86. // SHALL create a double queue when a peer connects to it. If this peer
  87. // disconnects, the ROUTER socket SHALL destroy its double queue and SHALL
  88. // discard any messages it contains.
  89. void test_destroy_queue_on_disconnect (const char *bind_address_)
  90. {
  91. void *a = test_context_socket (ZMQ_ROUTER);
  92. int enabled = 1;
  93. TEST_ASSERT_SUCCESS_ERRNO (
  94. zmq_setsockopt (a, ZMQ_ROUTER_MANDATORY, &enabled, sizeof (enabled)));
  95. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (a, bind_address_));
  96. size_t len = MAX_SOCKET_STRING;
  97. char connect_address[MAX_SOCKET_STRING];
  98. TEST_ASSERT_SUCCESS_ERRNO (
  99. zmq_getsockopt (a, ZMQ_LAST_ENDPOINT, connect_address, &len));
  100. void *b = test_context_socket (ZMQ_DEALER);
  101. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (b, ZMQ_ROUTING_ID, "B", 2));
  102. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
  103. // Wait for connection.
  104. msleep (SETTLE_TIME);
  105. // Send a message in both directions
  106. s_send_seq (a, "B", "ABC", SEQ_END);
  107. s_send_seq (b, "DEF", SEQ_END);
  108. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (b, connect_address));
  109. // Disconnect may take time and need command processing.
  110. zmq_pollitem_t poller[2] = {{a, 0, 0, 0}, {b, 0, 0, 0}};
  111. TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (poller, 2, 100));
  112. TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (poller, 2, 100));
  113. // No messages should be available, sending should fail.
  114. zmq_msg_t msg;
  115. zmq_msg_init (&msg);
  116. TEST_ASSERT_FAILURE_ERRNO (
  117. EHOSTUNREACH, zmq_send (a, "B", 2, ZMQ_SNDMORE | ZMQ_DONTWAIT));
  118. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
  119. // After a reconnect of B, the messages should still be gone
  120. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
  121. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
  122. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, b, ZMQ_DONTWAIT));
  123. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  124. test_context_socket_close_zero_linger (a);
  125. test_context_socket_close_zero_linger (b);
  126. // Wait for disconnects.
  127. msleep (SETTLE_TIME);
  128. }
  129. #define TEST_SUITE(name, bind_address) \
  130. void test_fair_queue_in_##name () { test_fair_queue_in (bind_address); } \
  131. void test_destroy_queue_on_disconnect_##name () \
  132. { \
  133. test_destroy_queue_on_disconnect (bind_address); \
  134. }
  135. TEST_SUITE (inproc, "inproc://a")
  136. TEST_SUITE (tcp, "tcp://127.0.0.1:*")
  137. int main ()
  138. {
  139. setup_test_environment ();
  140. UNITY_BEGIN ();
  141. RUN_TEST (test_fair_queue_in_tcp);
  142. RUN_TEST (test_fair_queue_in_inproc);
  143. // TODO commented out until libzmq implements this properly
  144. // RUN_TEST (test_destroy_queue_on_disconnect_tcp);
  145. // RUN_TEST (test_destroy_queue_on_disconnect_inproc);
  146. return UNITY_END ();
  147. }