test_req_relaxed.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <unity.h>
  27. const size_t services = 5;
  28. void *req;
  29. void *rep[services];
  30. void setUp ()
  31. {
  32. setup_test_context ();
  33. char my_endpoint[MAX_SOCKET_STRING];
  34. req = test_context_socket (ZMQ_REQ);
  35. int enabled = 1;
  36. TEST_ASSERT_SUCCESS_ERRNO (
  37. zmq_setsockopt (req, ZMQ_REQ_RELAXED, &enabled, sizeof (int)));
  38. TEST_ASSERT_SUCCESS_ERRNO (
  39. zmq_setsockopt (req, ZMQ_REQ_CORRELATE, &enabled, sizeof (int)));
  40. bind_loopback_ipv4 (req, my_endpoint, sizeof (my_endpoint));
  41. for (size_t peer = 0; peer < services; peer++) {
  42. rep[peer] = test_context_socket (ZMQ_REP);
  43. int timeout = 500;
  44. TEST_ASSERT_SUCCESS_ERRNO (
  45. zmq_setsockopt (rep[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  46. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rep[peer], my_endpoint));
  47. // These tests require strict ordering, so wait for the connections to
  48. // happen before opening the next, so that messages flow in the
  49. // expected direction
  50. msleep (SETTLE_TIME);
  51. }
  52. }
  53. void tearDown ()
  54. {
  55. test_context_socket_close_zero_linger (req);
  56. for (size_t peer = 0; peer < services; peer++)
  57. test_context_socket_close_zero_linger (rep[peer]);
  58. teardown_test_context ();
  59. }
  60. static void bounce (void *socket_)
  61. {
  62. int more;
  63. size_t more_size = sizeof (more);
  64. do {
  65. zmq_msg_t recv_part, sent_part;
  66. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&recv_part));
  67. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&recv_part, socket_, 0));
  68. TEST_ASSERT_SUCCESS_ERRNO (
  69. zmq_getsockopt (socket_, ZMQ_RCVMORE, &more, &more_size));
  70. zmq_msg_init (&sent_part);
  71. zmq_msg_copy (&sent_part, &recv_part);
  72. TEST_ASSERT_SUCCESS_ERRNO (
  73. zmq_msg_send (&sent_part, socket_, more ? ZMQ_SNDMORE : 0));
  74. zmq_msg_close (&recv_part);
  75. } while (more);
  76. }
  77. static int get_events (void *socket_)
  78. {
  79. int events;
  80. size_t events_size = sizeof (events);
  81. TEST_ASSERT_SUCCESS_ERRNO (
  82. zmq_getsockopt (socket_, ZMQ_EVENTS, &events, &events_size));
  83. return events;
  84. }
  85. void test_case_1 ()
  86. {
  87. // Case 1: Second send() before a reply arrives in a pipe.
  88. int events = get_events (req);
  89. TEST_ASSERT_EQUAL_INT (ZMQ_POLLOUT, events);
  90. // Send a request, ensure it arrives, don't send a reply
  91. s_send_seq (req, "A", "B", SEQ_END);
  92. s_recv_seq (rep[0], "A", "B", SEQ_END);
  93. events = get_events (req);
  94. TEST_ASSERT_EQUAL_INT (ZMQ_POLLOUT, events);
  95. // Send another request on the REQ socket
  96. s_send_seq (req, "C", "D", SEQ_END);
  97. s_recv_seq (rep[1], "C", "D", SEQ_END);
  98. events = get_events (req);
  99. TEST_ASSERT_EQUAL_INT (ZMQ_POLLOUT, events);
  100. // Send a reply to the first request - that should be discarded by the REQ
  101. s_send_seq (rep[0], "WRONG", SEQ_END);
  102. // Send the expected reply
  103. s_send_seq (rep[1], "OK", SEQ_END);
  104. s_recv_seq (req, "OK", SEQ_END);
  105. // Another standard req-rep cycle, just to check
  106. s_send_seq (req, "E", SEQ_END);
  107. s_recv_seq (rep[2], "E", SEQ_END);
  108. s_send_seq (rep[2], "F", "G", SEQ_END);
  109. s_recv_seq (req, "F", "G", SEQ_END);
  110. }
  111. void test_case_2 ()
  112. {
  113. // Case 2: Second send() after a reply is already in a pipe on the REQ.
  114. // TODO instead of rerunning the previous test cases, only do the relevant parts (or change the peer)
  115. test_case_1 ();
  116. // Send a request, ensure it arrives, send a reply
  117. s_send_seq (req, "H", SEQ_END);
  118. s_recv_seq (rep[3], "H", SEQ_END);
  119. s_send_seq (rep[3], "BAD", SEQ_END);
  120. // Wait for message to be there.
  121. msleep (SETTLE_TIME);
  122. // Without receiving that reply, send another request on the REQ socket
  123. s_send_seq (req, "I", SEQ_END);
  124. s_recv_seq (rep[4], "I", SEQ_END);
  125. // Send the expected reply
  126. s_send_seq (rep[4], "GOOD", SEQ_END);
  127. s_recv_seq (req, "GOOD", SEQ_END);
  128. }
  129. void test_case_3 ()
  130. {
  131. // Case 3: Check issue #1690. Two send() in a row should not close the
  132. // communication pipes. For example pipe from req to rep[0] should not be
  133. // closed after executing Case 1. So rep[0] should be the next to receive,
  134. // not rep[1].
  135. // TODO instead of rerunning the previous test cases, only do the relevant parts (or change the peer)
  136. test_case_2 ();
  137. s_send_seq (req, "J", SEQ_END);
  138. s_recv_seq (rep[0], "J", SEQ_END);
  139. }
  140. void test_case_4 ()
  141. {
  142. // TODO this test case does not use the sockets from setUp
  143. // Case 4: Check issue #1695. As messages may pile up before a responder
  144. // is available, we check that responses to messages other than the last
  145. // sent one are correctly discarded by the REQ pipe
  146. // Setup REQ socket as client
  147. void *req = test_context_socket (ZMQ_REQ);
  148. int enabled = 1;
  149. TEST_ASSERT_SUCCESS_ERRNO (
  150. zmq_setsockopt (req, ZMQ_REQ_RELAXED, &enabled, sizeof (int)));
  151. TEST_ASSERT_SUCCESS_ERRNO (
  152. zmq_setsockopt (req, ZMQ_REQ_CORRELATE, &enabled, sizeof (int)));
  153. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req, ENDPOINT_0));
  154. // Setup ROUTER socket as server but do not bind it just yet
  155. void *router = test_context_socket (ZMQ_ROUTER);
  156. // Send two requests
  157. s_send_seq (req, "TO_BE_DISCARDED", SEQ_END);
  158. s_send_seq (req, "TO_BE_ANSWERED", SEQ_END);
  159. // Bind server allowing it to receive messages
  160. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, ENDPOINT_0));
  161. // Read the two messages and send them back as is
  162. bounce (router);
  163. bounce (router);
  164. // Read the expected correlated reply. As the ZMQ_REQ_CORRELATE is active,
  165. // the expected answer is "TO_BE_ANSWERED", not "TO_BE_DISCARDED".
  166. s_recv_seq (req, "TO_BE_ANSWERED", SEQ_END);
  167. test_context_socket_close_zero_linger (req);
  168. test_context_socket_close_zero_linger (router);
  169. }
  170. int main ()
  171. {
  172. setup_test_environment ();
  173. UNITY_BEGIN ();
  174. RUN_TEST (test_case_1);
  175. RUN_TEST (test_case_2);
  176. RUN_TEST (test_case_3);
  177. RUN_TEST (test_case_4);
  178. return UNITY_END ();
  179. }