test_hwm_pubsub.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 <string.h>
  27. // NOTE: on OSX the endpoint returned by ZMQ_LAST_ENDPOINT may be quite long,
  28. // ensure we have extra space for that:
  29. #define SOCKET_STRING_LEN (MAX_SOCKET_STRING * 4)
  30. SETUP_TEARDOWN_TESTCONTEXT
  31. int test_defaults (int send_hwm_, int msg_cnt_, const char *endpoint_)
  32. {
  33. char pub_endpoint[SOCKET_STRING_LEN];
  34. // Set up and bind XPUB socket
  35. void *pub_socket = test_context_socket (ZMQ_XPUB);
  36. test_bind (pub_socket, endpoint_, pub_endpoint, sizeof pub_endpoint);
  37. // Set up and connect SUB socket
  38. void *sub_socket = test_context_socket (ZMQ_SUB);
  39. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub_socket, pub_endpoint));
  40. //set a hwm on publisher
  41. TEST_ASSERT_SUCCESS_ERRNO (
  42. zmq_setsockopt (pub_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
  43. TEST_ASSERT_SUCCESS_ERRNO (
  44. zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, 0, 0));
  45. // Wait before starting TX operations till 1 subscriber has subscribed
  46. // (in this test there's 1 subscriber only)
  47. const char subscription_to_all_topics[] = {1, 0};
  48. recv_string_expect_success (pub_socket, subscription_to_all_topics, 0);
  49. // Send until we reach "mute" state
  50. int send_count = 0;
  51. while (send_count < msg_cnt_
  52. && zmq_send (pub_socket, "test message", 13, ZMQ_DONTWAIT) == 13)
  53. ++send_count;
  54. TEST_ASSERT_EQUAL_INT (send_hwm_, send_count);
  55. msleep (SETTLE_TIME);
  56. // Now receive all sent messages
  57. int recv_count = 0;
  58. char dummybuff[64];
  59. while (13 == zmq_recv (sub_socket, &dummybuff, 64, ZMQ_DONTWAIT)) {
  60. ++recv_count;
  61. }
  62. TEST_ASSERT_EQUAL_INT (send_hwm_, recv_count);
  63. // Clean up
  64. test_context_socket_close (sub_socket);
  65. test_context_socket_close (pub_socket);
  66. return recv_count;
  67. }
  68. int receive (void *socket_, int *is_termination_)
  69. {
  70. int recv_count = 0;
  71. *is_termination_ = 0;
  72. // Now receive all sent messages
  73. char buffer[255];
  74. int len;
  75. while ((len = zmq_recv (socket_, buffer, sizeof (buffer), 0)) >= 0) {
  76. ++recv_count;
  77. if (len == 3 && strncmp (buffer, "end", len) == 0) {
  78. *is_termination_ = 1;
  79. return recv_count;
  80. }
  81. }
  82. return recv_count;
  83. }
  84. int test_blocking (int send_hwm_, int msg_cnt_, const char *endpoint_)
  85. {
  86. char pub_endpoint[SOCKET_STRING_LEN];
  87. // Set up bind socket
  88. void *pub_socket = test_context_socket (ZMQ_XPUB);
  89. test_bind (pub_socket, endpoint_, pub_endpoint, sizeof pub_endpoint);
  90. // Set up connect socket
  91. void *sub_socket = test_context_socket (ZMQ_SUB);
  92. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub_socket, pub_endpoint));
  93. //set a hwm on publisher
  94. TEST_ASSERT_SUCCESS_ERRNO (
  95. zmq_setsockopt (pub_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
  96. int wait = 1;
  97. TEST_ASSERT_SUCCESS_ERRNO (
  98. zmq_setsockopt (pub_socket, ZMQ_XPUB_NODROP, &wait, sizeof (wait)));
  99. int timeout_ms = 10;
  100. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  101. sub_socket, ZMQ_RCVTIMEO, &timeout_ms, sizeof (timeout_ms)));
  102. TEST_ASSERT_SUCCESS_ERRNO (
  103. zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, 0, 0));
  104. // Wait before starting TX operations till 1 subscriber has subscribed
  105. // (in this test there's 1 subscriber only)
  106. const uint8_t subscription_to_all_topics[] = {1};
  107. recv_array_expect_success (pub_socket, subscription_to_all_topics, 0);
  108. // Send until we block
  109. int send_count = 0;
  110. int recv_count = 0;
  111. int blocked_count = 0;
  112. int is_termination = 0;
  113. while (send_count < msg_cnt_) {
  114. const int rc = zmq_send (pub_socket, NULL, 0, ZMQ_DONTWAIT);
  115. if (rc == 0) {
  116. ++send_count;
  117. } else if (-1 == rc) {
  118. // if the PUB socket blocks due to HWM, errno should be EAGAIN:
  119. blocked_count++;
  120. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, -1);
  121. recv_count += receive (sub_socket, &is_termination);
  122. }
  123. }
  124. // if send_hwm_ < msg_cnt_, we should block at least once:
  125. char counts_string[128];
  126. snprintf (counts_string, sizeof counts_string - 1,
  127. "sent = %i, received = %i", send_count, recv_count);
  128. TEST_ASSERT_GREATER_THAN_INT_MESSAGE (0, blocked_count, counts_string);
  129. // dequeue SUB socket again, to make sure XPUB has space to send the termination message
  130. recv_count += receive (sub_socket, &is_termination);
  131. // send termination message
  132. send_string_expect_success (pub_socket, "end", 0);
  133. // now block on the SUB side till we get the termination message
  134. while (is_termination == 0)
  135. recv_count += receive (sub_socket, &is_termination);
  136. // remove termination message from the count:
  137. recv_count--;
  138. TEST_ASSERT_EQUAL_INT (send_count, recv_count);
  139. // Clean up
  140. test_context_socket_close (sub_socket);
  141. test_context_socket_close (pub_socket);
  142. return recv_count;
  143. }
  144. // hwm should apply to the messages that have already been received
  145. // with hwm 11024: send 9999 msg, receive 9999, send 1100, receive 1100
  146. void test_reset_hwm ()
  147. {
  148. const int first_count = 9999;
  149. const int second_count = 1100;
  150. int hwm = 11024;
  151. char my_endpoint[SOCKET_STRING_LEN];
  152. // Set up bind socket
  153. void *pub_socket = test_context_socket (ZMQ_PUB);
  154. TEST_ASSERT_SUCCESS_ERRNO (
  155. zmq_setsockopt (pub_socket, ZMQ_SNDHWM, &hwm, sizeof (hwm)));
  156. bind_loopback_ipv4 (pub_socket, my_endpoint, MAX_SOCKET_STRING);
  157. // Set up connect socket
  158. void *sub_socket = test_context_socket (ZMQ_SUB);
  159. TEST_ASSERT_SUCCESS_ERRNO (
  160. zmq_setsockopt (sub_socket, ZMQ_RCVHWM, &hwm, sizeof (hwm)));
  161. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub_socket, my_endpoint));
  162. TEST_ASSERT_SUCCESS_ERRNO (
  163. zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, 0, 0));
  164. msleep (SETTLE_TIME);
  165. // Send messages
  166. int send_count = 0;
  167. while (send_count < first_count
  168. && zmq_send (pub_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  169. ++send_count;
  170. TEST_ASSERT_EQUAL_INT (first_count, send_count);
  171. msleep (SETTLE_TIME);
  172. // Now receive all sent messages
  173. int recv_count = 0;
  174. while (0 == zmq_recv (sub_socket, NULL, 0, ZMQ_DONTWAIT)) {
  175. ++recv_count;
  176. }
  177. TEST_ASSERT_EQUAL_INT (first_count, recv_count);
  178. msleep (SETTLE_TIME);
  179. // Send messages
  180. send_count = 0;
  181. while (send_count < second_count
  182. && zmq_send (pub_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  183. ++send_count;
  184. TEST_ASSERT_EQUAL_INT (second_count, send_count);
  185. msleep (SETTLE_TIME);
  186. // Now receive all sent messages
  187. recv_count = 0;
  188. while (0 == zmq_recv (sub_socket, NULL, 0, ZMQ_DONTWAIT)) {
  189. ++recv_count;
  190. }
  191. TEST_ASSERT_EQUAL_INT (second_count, recv_count);
  192. // Clean up
  193. test_context_socket_close (sub_socket);
  194. test_context_socket_close (pub_socket);
  195. }
  196. void test_defaults_large (const char *bind_endpoint_)
  197. {
  198. // send 1000 msg on hwm 1000, receive 1000
  199. TEST_ASSERT_EQUAL_INT (1000, test_defaults (1000, 1000, bind_endpoint_));
  200. }
  201. void test_defaults_small (const char *bind_endpoint_)
  202. {
  203. // send 1000 msg on hwm 100, receive 100
  204. TEST_ASSERT_EQUAL_INT (100, test_defaults (100, 100, bind_endpoint_));
  205. }
  206. void test_blocking (const char *bind_endpoint_)
  207. {
  208. // send 6000 msg on hwm 2000, drops above hwm, only receive hwm:
  209. TEST_ASSERT_EQUAL_INT (6000, test_blocking (2000, 6000, bind_endpoint_));
  210. }
  211. #define DEFINE_REGULAR_TEST_CASES(name, bind_endpoint) \
  212. void test_defaults_large_##name () \
  213. { \
  214. test_defaults_large (bind_endpoint); \
  215. } \
  216. \
  217. void test_defaults_small_##name () \
  218. { \
  219. test_defaults_small (bind_endpoint); \
  220. } \
  221. \
  222. void test_blocking_##name () { test_blocking (bind_endpoint); }
  223. #define RUN_REGULAR_TEST_CASES(name) \
  224. RUN_TEST (test_defaults_large_##name); \
  225. RUN_TEST (test_defaults_small_##name); \
  226. RUN_TEST (test_blocking_##name)
  227. DEFINE_REGULAR_TEST_CASES (tcp, "tcp://127.0.0.1:*")
  228. DEFINE_REGULAR_TEST_CASES (inproc, "inproc://a")
  229. #if !defined(ZMQ_HAVE_WINDOWS) && !defined(ZMQ_HAVE_GNU)
  230. DEFINE_REGULAR_TEST_CASES (ipc, "ipc://*")
  231. #endif
  232. int main ()
  233. {
  234. setup_test_environment ();
  235. UNITY_BEGIN ();
  236. RUN_REGULAR_TEST_CASES (tcp);
  237. RUN_REGULAR_TEST_CASES (inproc);
  238. #if !defined(ZMQ_HAVE_WINDOWS) && !defined(ZMQ_HAVE_GNU)
  239. RUN_REGULAR_TEST_CASES (ipc);
  240. #endif
  241. RUN_TEST (test_reset_hwm);
  242. return UNITY_END ();
  243. }