test_hwm.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. SETUP_TEARDOWN_TESTCONTEXT
  27. const int MAX_SENDS = 10000;
  28. enum TestType
  29. {
  30. BIND_FIRST,
  31. CONNECT_FIRST
  32. };
  33. void test_defaults ()
  34. {
  35. // Set up bind socket
  36. void *bind_socket = test_context_socket (ZMQ_PULL);
  37. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
  38. // Set up connect socket
  39. void *connect_socket = test_context_socket (ZMQ_PUSH);
  40. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
  41. // Send until we block
  42. int send_count = 0;
  43. while (send_count < MAX_SENDS
  44. && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  45. ++send_count;
  46. msleep (SETTLE_TIME);
  47. // Now receive all sent messages
  48. int recv_count = 0;
  49. while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  50. ++recv_count;
  51. TEST_ASSERT_EQUAL_INT (send_count, recv_count);
  52. // Clean up
  53. test_context_socket_close (connect_socket);
  54. test_context_socket_close (bind_socket);
  55. // Default values are 1000 on send and 1000 one receive, so 2000 total
  56. TEST_ASSERT_EQUAL_INT (2000, send_count);
  57. }
  58. int count_msg (int send_hwm_, int recv_hwm_, TestType test_type_)
  59. {
  60. void *bind_socket;
  61. void *connect_socket;
  62. if (test_type_ == BIND_FIRST) {
  63. // Set up bind socket
  64. bind_socket = test_context_socket (ZMQ_PULL);
  65. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  66. bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
  67. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
  68. // Set up connect socket
  69. connect_socket = test_context_socket (ZMQ_PUSH);
  70. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  71. connect_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
  72. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
  73. // we must wait for the connect to succeed here, unfortunately we don't
  74. // have monitoring events for inproc, so we just hope SETTLE_TIME suffices
  75. msleep (SETTLE_TIME);
  76. } else {
  77. // Set up connect socket
  78. connect_socket = test_context_socket (ZMQ_PUSH);
  79. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  80. connect_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
  81. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
  82. // Set up bind socket
  83. bind_socket = test_context_socket (ZMQ_PULL);
  84. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  85. bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
  86. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
  87. }
  88. // Send until we block
  89. int send_count = 0;
  90. while (send_count < MAX_SENDS
  91. && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  92. ++send_count;
  93. // Now receive all sent messages
  94. int recv_count = 0;
  95. while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  96. ++recv_count;
  97. TEST_ASSERT_EQUAL_INT (send_count, recv_count);
  98. // Now it should be possible to send one more.
  99. send_string_expect_success (connect_socket, NULL, 0);
  100. // Consume the remaining message.
  101. recv_string_expect_success (bind_socket, NULL, 0);
  102. // Clean up
  103. test_context_socket_close (connect_socket);
  104. test_context_socket_close (bind_socket);
  105. return send_count;
  106. }
  107. int test_inproc_bind_first (int send_hwm_, int recv_hwm_)
  108. {
  109. return count_msg (send_hwm_, recv_hwm_, BIND_FIRST);
  110. }
  111. int test_inproc_connect_first (int send_hwm_, int recv_hwm_)
  112. {
  113. return count_msg (send_hwm_, recv_hwm_, CONNECT_FIRST);
  114. }
  115. int test_inproc_connect_and_close_first (int send_hwm_, int recv_hwm_)
  116. {
  117. // Set up connect socket
  118. void *connect_socket = test_context_socket (ZMQ_PUSH);
  119. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_SNDHWM,
  120. &send_hwm_, sizeof (send_hwm_)));
  121. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
  122. // Send until we block
  123. int send_count = 0;
  124. while (send_count < MAX_SENDS
  125. && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  126. ++send_count;
  127. // Close connect
  128. test_context_socket_close (connect_socket);
  129. // Set up bind socket
  130. void *bind_socket = test_context_socket (ZMQ_PULL);
  131. TEST_ASSERT_SUCCESS_ERRNO (
  132. zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
  133. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
  134. // Now receive all sent messages
  135. int recv_count = 0;
  136. while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  137. ++recv_count;
  138. TEST_ASSERT_EQUAL_INT (send_count, recv_count);
  139. // Clean up
  140. test_context_socket_close (bind_socket);
  141. return send_count;
  142. }
  143. int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */)
  144. {
  145. // Set up bind socket
  146. void *bind_socket = test_context_socket (ZMQ_PUSH);
  147. TEST_ASSERT_SUCCESS_ERRNO (
  148. zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
  149. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
  150. // Send until we block
  151. int send_count = 0;
  152. while (send_count < MAX_SENDS
  153. && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  154. ++send_count;
  155. // Close bind
  156. test_context_socket_close (bind_socket);
  157. /* TODO Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
  158. // Set up connect socket
  159. void *connect_socket = test_context_socket (ZMQ_PULL);
  160. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
  161. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
  162. // Now receive all sent messages
  163. int recv_count = 0;
  164. while (zmq_recv (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  165. ++recv_count;
  166. TEST_ASSERT_EQUAL_INT(send_count, recv_count);
  167. */
  168. // Clean up
  169. //test_context_socket_close (connect_socket);
  170. return send_count;
  171. }
  172. void test_infinite_both_inproc_bind_first ()
  173. {
  174. int count = test_inproc_bind_first (0, 0);
  175. TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
  176. }
  177. void test_infinite_both_inproc_connect_first ()
  178. {
  179. int count = test_inproc_connect_first (0, 0);
  180. TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
  181. }
  182. void test_infinite_receive_inproc_bind_first ()
  183. {
  184. int count = test_inproc_bind_first (1, 0);
  185. TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
  186. }
  187. void test_infinite_receive_inproc_connect_first ()
  188. {
  189. int count = test_inproc_connect_first (1, 0);
  190. TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
  191. }
  192. void test_infinite_send_inproc_bind_first ()
  193. {
  194. int count = test_inproc_bind_first (0, 1);
  195. TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
  196. }
  197. void test_infinite_send_inproc_connect_first ()
  198. {
  199. int count = test_inproc_connect_first (0, 1);
  200. TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
  201. }
  202. void test_finite_both_bind_first ()
  203. {
  204. // Send and recv buffers hwm 1, so total that can be queued is 2
  205. int count = test_inproc_bind_first (1, 1);
  206. TEST_ASSERT_EQUAL_INT (2, count);
  207. }
  208. void test_finite_both_connect_first ()
  209. {
  210. // Send and recv buffers hwm 1, so total that can be queued is 2
  211. int count = test_inproc_connect_first (1, 1);
  212. TEST_ASSERT_EQUAL_INT (2, count);
  213. }
  214. void test_infinite_recv_connect_and_close_first ()
  215. {
  216. // Send hwm of 1, send before bind so total that can be queued is 1
  217. int count = test_inproc_connect_and_close_first (1, 0);
  218. TEST_ASSERT_EQUAL_INT (1, count);
  219. }
  220. void test_infinite_recv_bind_and_close_first ()
  221. {
  222. // Send hwm of 1, send from bind side before connect so total that can be queued should be 1,
  223. // however currently all messages get thrown away before the connect. BUG?
  224. /*int count = */ test_inproc_bind_and_close_first (1, 0);
  225. // TEST_ASSERT_EQUAL_INT (1, count);
  226. }
  227. int main (void)
  228. {
  229. setup_test_environment ();
  230. UNITY_BEGIN ();
  231. RUN_TEST (test_defaults);
  232. RUN_TEST (test_infinite_both_inproc_bind_first);
  233. RUN_TEST (test_infinite_both_inproc_connect_first);
  234. RUN_TEST (test_infinite_receive_inproc_bind_first);
  235. RUN_TEST (test_infinite_receive_inproc_connect_first);
  236. RUN_TEST (test_infinite_send_inproc_bind_first);
  237. RUN_TEST (test_infinite_send_inproc_connect_first);
  238. RUN_TEST (test_finite_both_bind_first);
  239. RUN_TEST (test_finite_both_connect_first);
  240. RUN_TEST (test_infinite_recv_connect_and_close_first);
  241. RUN_TEST (test_infinite_recv_bind_and_close_first);
  242. return UNITY_END ();
  243. }