test_connect_delay_tipc.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (c) 2007-2016 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 "testutil_security.hpp"
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. void test_send_one_connected_one_unconnected ()
  29. {
  30. int val;
  31. // TEST 1.
  32. // First we're going to attempt to send messages to two
  33. // pipes, one connected, the other not. We should see
  34. // the PUSH load balancing to both pipes, and hence half
  35. // of the messages getting queued, as connect() creates a
  36. // pipe immediately.
  37. void *to = test_context_socket (ZMQ_PULL);
  38. int timeout = 5000;
  39. TEST_ASSERT_SUCCESS_ERRNO (
  40. zmq_setsockopt (to, ZMQ_LINGER, &timeout, sizeof (timeout)));
  41. // Bind the one valid receiver
  42. val = 0;
  43. TEST_ASSERT_SUCCESS_ERRNO (
  44. zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
  45. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (to, "tipc://{6555,0,0}"));
  46. // Create a socket pushing to two endpoints - only 1 message should arrive.
  47. void *from = test_context_socket (ZMQ_PUSH);
  48. val = 0;
  49. TEST_ASSERT_SUCCESS_ERRNO (
  50. zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
  51. TEST_ASSERT_SUCCESS_ERRNO (
  52. zmq_setsockopt (from, ZMQ_LINGER, &timeout, sizeof (timeout)));
  53. // This pipe will not connect
  54. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5556,0}@0.0.0"));
  55. // This pipe will
  56. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{6555,0}@0.0.0"));
  57. // We send 10 messages, 5 should just get stuck in the queue
  58. // for the not-yet-connected pipe
  59. const int send_count = 10;
  60. for (int i = 0; i < send_count; ++i) {
  61. send_string_expect_success (from, "Hello", 0);
  62. }
  63. // We now consume from the connected pipe
  64. // - we should see just 5
  65. timeout = SETTLE_TIME;
  66. TEST_ASSERT_SUCCESS_ERRNO (
  67. zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  68. int seen = 0;
  69. while (true) {
  70. char buffer[16];
  71. int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
  72. if (rc == -1) {
  73. TEST_ASSERT_EQUAL_INT (EAGAIN, zmq_errno ());
  74. break; // Break when we didn't get a message
  75. }
  76. seen++;
  77. }
  78. TEST_ASSERT_EQUAL_INT (send_count / 2, seen);
  79. test_context_socket_close (from);
  80. test_context_socket_close (to);
  81. }
  82. void test_send_one_connected_one_unconnected_with_delay ()
  83. {
  84. int val;
  85. // TEST 2
  86. // This time we will do the same thing, connect two pipes,
  87. // one of which will succeed in connecting to a bound
  88. // receiver, the other of which will fail. However, we will
  89. // also set the delay attach on connect flag, which should
  90. // cause the pipe attachment to be delayed until the connection
  91. // succeeds.
  92. // Bind the valid socket
  93. void *to = test_context_socket (ZMQ_PULL);
  94. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (to, "tipc://{5560,0,0}"));
  95. int timeout = 5000;
  96. TEST_ASSERT_SUCCESS_ERRNO (
  97. zmq_setsockopt (to, ZMQ_LINGER, &timeout, sizeof (timeout)));
  98. val = 0;
  99. TEST_ASSERT_SUCCESS_ERRNO (
  100. zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
  101. // Create a socket pushing to two endpoints - all messages should arrive.
  102. void *from = test_context_socket (ZMQ_PUSH);
  103. val = 0;
  104. TEST_ASSERT_SUCCESS_ERRNO (
  105. zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
  106. TEST_ASSERT_SUCCESS_ERRNO (
  107. zmq_setsockopt (from, ZMQ_LINGER, &timeout, sizeof (timeout)));
  108. // Set the key flag
  109. val = 1;
  110. TEST_ASSERT_SUCCESS_ERRNO (
  111. zmq_setsockopt (from, ZMQ_DELAY_ATTACH_ON_CONNECT, &val, sizeof (val)));
  112. // Connect to the invalid socket
  113. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5561,0}@0.0.0"));
  114. // Connect to the valid socket
  115. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5560,0}@0.0.0"));
  116. // Send 10 messages, all should be routed to the connected pipe
  117. const int send_count = 10;
  118. for (int i = 0; i < send_count; ++i) {
  119. send_string_expect_success (from, "Hello", 0);
  120. }
  121. timeout = SETTLE_TIME;
  122. TEST_ASSERT_SUCCESS_ERRNO (
  123. zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
  124. int seen = 0;
  125. while (true) {
  126. char buffer[16];
  127. int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
  128. if (rc == -1) {
  129. TEST_ASSERT_EQUAL_INT (EAGAIN, zmq_errno ());
  130. break; // Break when we didn't get a message
  131. }
  132. seen++;
  133. }
  134. TEST_ASSERT_EQUAL_INT (send_count, seen);
  135. test_context_socket_close (from);
  136. test_context_socket_close (to);
  137. }
  138. void test_send_disconnected_with_delay ()
  139. {
  140. // TEST 3
  141. // This time we want to validate that the same blocking behaviour
  142. // occurs with an existing connection that is broken. We will send
  143. // messages to a connected pipe, disconnect and verify the messages
  144. // block. Then we reconnect and verify messages flow again.
  145. void *backend = test_context_socket (ZMQ_DEALER);
  146. void *frontend = test_context_socket (ZMQ_DEALER);
  147. void *monitor = test_context_socket (ZMQ_PAIR);
  148. int rc;
  149. int zero = 0;
  150. TEST_ASSERT_SUCCESS_ERRNO (
  151. zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
  152. TEST_ASSERT_SUCCESS_ERRNO (
  153. zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero)));
  154. TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (frontend, "inproc://monitor",
  155. ZMQ_EVENT_DISCONNECTED));
  156. int timeout = 5000;
  157. TEST_ASSERT_SUCCESS_ERRNO (
  158. zmq_setsockopt (backend, ZMQ_LINGER, &timeout, sizeof (timeout)));
  159. TEST_ASSERT_SUCCESS_ERRNO (
  160. zmq_setsockopt (frontend, ZMQ_LINGER, &timeout, sizeof (timeout)));
  161. // Frontend connects to backend using DELAY_ATTACH_ON_CONNECT
  162. int on = 1;
  163. TEST_ASSERT_SUCCESS_ERRNO (
  164. zmq_setsockopt (frontend, ZMQ_DELAY_ATTACH_ON_CONNECT, &on, sizeof (on)));
  165. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tipc://{5560,0,0}"));
  166. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (monitor, "inproc://monitor"));
  167. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (frontend, "tipc://{5560,0}@0.0.0"));
  168. // Ping backend to frontend so we know when the connection is up
  169. send_string_expect_success (backend, "Hello", 0);
  170. recv_string_expect_success (frontend, "Hello", 0);
  171. // Send message from frontend to backend
  172. send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
  173. test_context_socket_close (backend);
  174. // Wait for disconnect to happen
  175. expect_monitor_event (monitor, ZMQ_EVENT_DISCONNECTED);
  176. // Send a message, might succeed depending on scheduling of the I/O thread
  177. do {
  178. rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
  179. TEST_ASSERT_TRUE (rc == 5 || (rc == -1 && zmq_errno () == EAGAIN));
  180. } while (rc == 5);
  181. // Recreate backend socket
  182. backend = test_context_socket (ZMQ_DEALER);
  183. TEST_ASSERT_SUCCESS_ERRNO (
  184. zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
  185. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tipc://{5560,0,0}"));
  186. // Ping backend to frontend so we know when the connection is up
  187. send_string_expect_success (backend, "Hello", 0);
  188. recv_string_expect_success (frontend, "Hello", 0);
  189. // After the reconnect, should succeed
  190. send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
  191. test_context_socket_close (monitor);
  192. test_context_socket_close (backend);
  193. test_context_socket_close (frontend);
  194. }
  195. int main (void)
  196. {
  197. if (!is_tipc_available ()) {
  198. printf ("TIPC environment unavailable, skipping test\n");
  199. return 77;
  200. }
  201. UNITY_BEGIN ();
  202. RUN_TEST (test_send_one_connected_one_unconnected);
  203. RUN_TEST (test_send_one_connected_one_unconnected_with_delay);
  204. RUN_TEST (test_send_disconnected_with_delay);
  205. return UNITY_END ();
  206. }