test_sockopt_hwm.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. void test_change_before_connected ()
  29. {
  30. int rc;
  31. void *bind_socket = test_context_socket (ZMQ_PUSH);
  32. void *connect_socket = test_context_socket (ZMQ_PULL);
  33. int val = 2;
  34. rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
  35. TEST_ASSERT_EQUAL_INT (0, rc);
  36. rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
  37. TEST_ASSERT_EQUAL_INT (0, rc);
  38. zmq_connect (connect_socket, "inproc://a");
  39. zmq_bind (bind_socket, "inproc://a");
  40. size_t placeholder = sizeof (val);
  41. val = 0;
  42. rc = zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &val, &placeholder);
  43. TEST_ASSERT_EQUAL_INT (0, rc);
  44. TEST_ASSERT_EQUAL_INT (2, val);
  45. int send_count = 0;
  46. while (send_count < MAX_SENDS
  47. && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  48. ++send_count;
  49. TEST_ASSERT_EQUAL_INT (4, send_count);
  50. test_context_socket_close (bind_socket);
  51. test_context_socket_close (connect_socket);
  52. }
  53. void test_change_after_connected ()
  54. {
  55. int rc;
  56. void *bind_socket = test_context_socket (ZMQ_PUSH);
  57. void *connect_socket = test_context_socket (ZMQ_PULL);
  58. int val = 1;
  59. rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
  60. TEST_ASSERT_EQUAL_INT (0, rc);
  61. rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
  62. TEST_ASSERT_EQUAL_INT (0, rc);
  63. zmq_connect (connect_socket, "inproc://a");
  64. zmq_bind (bind_socket, "inproc://a");
  65. val = 5;
  66. rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
  67. TEST_ASSERT_EQUAL_INT (0, rc);
  68. size_t placeholder = sizeof (val);
  69. val = 0;
  70. rc = zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &val, &placeholder);
  71. TEST_ASSERT_EQUAL_INT (0, rc);
  72. TEST_ASSERT_EQUAL_INT (5, val);
  73. int send_count = 0;
  74. while (send_count < MAX_SENDS
  75. && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
  76. ++send_count;
  77. TEST_ASSERT_EQUAL_INT (6, send_count);
  78. test_context_socket_close (bind_socket);
  79. test_context_socket_close (connect_socket);
  80. }
  81. int send_until_wouldblock (void *socket_)
  82. {
  83. int send_count = 0;
  84. while (send_count < MAX_SENDS
  85. && zmq_send (socket_, &send_count, sizeof (send_count), ZMQ_DONTWAIT)
  86. == sizeof (send_count)) {
  87. ++send_count;
  88. }
  89. return send_count;
  90. }
  91. int test_fill_up_to_hwm (void *socket_, int sndhwm_)
  92. {
  93. int send_count = send_until_wouldblock (socket_);
  94. fprintf (stderr, "sndhwm==%i, send_count==%i\n", sndhwm_, send_count);
  95. TEST_ASSERT_LESS_OR_EQUAL_INT (sndhwm_ + 1, send_count);
  96. TEST_ASSERT_GREATER_THAN_INT (sndhwm_ / 10, send_count);
  97. return send_count;
  98. }
  99. void test_decrease_when_full ()
  100. {
  101. int rc;
  102. void *bind_socket = test_context_socket (ZMQ_PUSH);
  103. void *connect_socket = test_context_socket (ZMQ_PULL);
  104. int val = 1;
  105. rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
  106. TEST_ASSERT_EQUAL_INT (0, rc);
  107. int sndhwm = 100;
  108. rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
  109. TEST_ASSERT_EQUAL_INT (0, rc);
  110. zmq_bind (bind_socket, "inproc://a");
  111. zmq_connect (connect_socket, "inproc://a");
  112. // we must wait for the connect to succeed here, unfortunately we don't
  113. // have monitoring events for inproc, so we just hope SETTLE_TIME suffices
  114. msleep (SETTLE_TIME);
  115. // Fill up to hwm
  116. int send_count = test_fill_up_to_hwm (bind_socket, sndhwm);
  117. // Decrease snd hwm
  118. sndhwm = 70;
  119. rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
  120. TEST_ASSERT_EQUAL_INT (0, rc);
  121. int sndhwm_read = 0;
  122. size_t sndhwm_read_size = sizeof (sndhwm_read);
  123. rc =
  124. zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm_read, &sndhwm_read_size);
  125. TEST_ASSERT_EQUAL_INT (0, rc);
  126. TEST_ASSERT_EQUAL_INT (sndhwm, sndhwm_read);
  127. msleep (SETTLE_TIME);
  128. // Read out all data (should get up to previous hwm worth so none were dropped)
  129. int read_count = 0;
  130. int read_data = 0;
  131. while (
  132. read_count < MAX_SENDS
  133. && zmq_recv (connect_socket, &read_data, sizeof (read_data), ZMQ_DONTWAIT)
  134. == sizeof (read_data)) {
  135. TEST_ASSERT_EQUAL_INT (read_data, read_count);
  136. ++read_count;
  137. }
  138. TEST_ASSERT_EQUAL_INT (send_count, read_count);
  139. // Give io thread some time to catch up
  140. msleep (SETTLE_TIME);
  141. // Fill up to new hwm
  142. test_fill_up_to_hwm (bind_socket, sndhwm);
  143. test_context_socket_close (bind_socket);
  144. test_context_socket_close (connect_socket);
  145. }
  146. int main ()
  147. {
  148. setup_test_environment ();
  149. UNITY_BEGIN ();
  150. RUN_TEST (test_change_before_connected);
  151. RUN_TEST (test_change_after_connected);
  152. RUN_TEST (test_decrease_when_full);
  153. return UNITY_END ();
  154. }