test_spec_dealer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. SETUP_TEARDOWN_TESTCONTEXT
  27. // SHALL route outgoing messages to available peers using a round-robin
  28. // strategy.
  29. void test_round_robin_out (const char *bind_address_)
  30. {
  31. void *dealer = test_context_socket (ZMQ_DEALER);
  32. char connect_address[MAX_SOCKET_STRING];
  33. test_bind (dealer, bind_address_, connect_address,
  34. sizeof (connect_address));
  35. const size_t services = 5;
  36. void *rep[services];
  37. for (size_t peer = 0; peer < services; ++peer) {
  38. rep[peer] = test_context_socket (ZMQ_REP);
  39. int timeout = 250;
  40. TEST_ASSERT_SUCCESS_ERRNO (
  41. zmq_setsockopt (rep[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  42. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rep[peer], connect_address));
  43. }
  44. // Wait for connections.
  45. msleep (SETTLE_TIME);
  46. // Send all requests
  47. for (size_t i = 0; i < services; ++i)
  48. s_send_seq (dealer, 0, "ABC", SEQ_END);
  49. // Expect every REP got one message
  50. zmq_msg_t msg;
  51. zmq_msg_init (&msg);
  52. for (size_t peer = 0; peer < services; ++peer)
  53. s_recv_seq (rep[peer], "ABC", SEQ_END);
  54. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  55. test_context_socket_close_zero_linger (dealer);
  56. for (size_t peer = 0; peer < services; ++peer)
  57. test_context_socket_close_zero_linger (rep[peer]);
  58. }
  59. // SHALL receive incoming messages from its peers using a fair-queuing
  60. // strategy.
  61. void test_fair_queue_in (const char *bind_address_)
  62. {
  63. void *receiver = test_context_socket (ZMQ_DEALER);
  64. int timeout = 250;
  65. TEST_ASSERT_SUCCESS_ERRNO (
  66. zmq_setsockopt (receiver, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  67. char connect_address[MAX_SOCKET_STRING];
  68. test_bind (receiver, bind_address_, connect_address,
  69. sizeof (connect_address));
  70. const size_t services = 5;
  71. void *senders[services];
  72. for (size_t peer = 0; peer < services; ++peer) {
  73. senders[peer] = test_context_socket (ZMQ_DEALER);
  74. TEST_ASSERT_SUCCESS_ERRNO (
  75. zmq_setsockopt (senders[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  76. TEST_ASSERT_SUCCESS_ERRNO (
  77. zmq_connect (senders[peer], connect_address));
  78. }
  79. zmq_msg_t msg;
  80. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  81. s_send_seq (senders[0], "A", SEQ_END);
  82. s_recv_seq (receiver, "A", SEQ_END);
  83. s_send_seq (senders[0], "A", SEQ_END);
  84. s_recv_seq (receiver, "A", SEQ_END);
  85. // send our requests
  86. for (size_t peer = 0; peer < services; ++peer)
  87. s_send_seq (senders[peer], "B", SEQ_END);
  88. // Wait for data.
  89. msleep (SETTLE_TIME);
  90. // handle the requests
  91. for (size_t peer = 0; peer < services; ++peer)
  92. s_recv_seq (receiver, "B", SEQ_END);
  93. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  94. test_context_socket_close_zero_linger (receiver);
  95. for (size_t peer = 0; peer < services; ++peer)
  96. test_context_socket_close_zero_linger (senders[peer]);
  97. }
  98. // SHALL create a double queue when a peer connects to it. If this peer
  99. // disconnects, the DEALER socket SHALL destroy its double queue and SHALL
  100. // discard any messages it contains.
  101. void test_destroy_queue_on_disconnect (const char *bind_address_)
  102. {
  103. void *a = test_context_socket (ZMQ_DEALER);
  104. char connect_address[MAX_SOCKET_STRING];
  105. test_bind (a, bind_address_, connect_address, sizeof (connect_address));
  106. void *b = test_context_socket (ZMQ_DEALER);
  107. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
  108. // Send a message in both directions
  109. s_send_seq (a, "ABC", SEQ_END);
  110. s_send_seq (b, "DEF", SEQ_END);
  111. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (b, connect_address));
  112. // Disconnect may take time and need command processing.
  113. zmq_pollitem_t poller[2] = {{a, 0, 0, 0}, {b, 0, 0, 0}};
  114. TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (poller, 2, 100));
  115. TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (poller, 2, 100));
  116. // No messages should be available, sending should fail.
  117. zmq_msg_t msg;
  118. zmq_msg_init (&msg);
  119. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (a, 0, 0, ZMQ_DONTWAIT));
  120. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
  121. // After a reconnect of B, the messages should still be gone
  122. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
  123. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
  124. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, b, ZMQ_DONTWAIT));
  125. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  126. test_context_socket_close_zero_linger (a);
  127. test_context_socket_close_zero_linger (b);
  128. }
  129. // SHALL block on sending, or return a suitable error, when it has no connected peers.
  130. void test_block_on_send_no_peers (const char *bind_address_)
  131. {
  132. void *sc = test_context_socket (ZMQ_DEALER);
  133. int timeout = 250;
  134. TEST_ASSERT_SUCCESS_ERRNO (
  135. zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (timeout)));
  136. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (sc, 0, 0, ZMQ_DONTWAIT));
  137. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (sc, 0, 0, 0));
  138. test_context_socket_close (sc);
  139. }
  140. #define TEST_CASES(name, bind_address) \
  141. void test_round_robin_out_##name () \
  142. { \
  143. test_round_robin_out (bind_address); \
  144. } \
  145. void test_fair_queue_in_##name () { test_fair_queue_in (bind_address); } \
  146. void test_block_on_send_no_peers_##name () \
  147. { \
  148. test_block_on_send_no_peers (bind_address); \
  149. }
  150. TEST_CASES (inproc, "inproc://a")
  151. TEST_CASES (tcp, "tcp://127.0.0.1:*")
  152. int main (void)
  153. {
  154. setup_test_environment ();
  155. UNITY_BEGIN ();
  156. RUN_TEST (test_round_robin_out_inproc);
  157. RUN_TEST (test_fair_queue_in_inproc);
  158. RUN_TEST (test_block_on_send_no_peers_inproc);
  159. RUN_TEST (test_round_robin_out_tcp);
  160. RUN_TEST (test_fair_queue_in_tcp);
  161. RUN_TEST (test_block_on_send_no_peers_tcp);
  162. // TODO *** Test disabled until libzmq does this properly ***
  163. // test_destroy_queue_on_disconnect (ctx);
  164. return UNITY_END ();
  165. }