test_xpub_nodrop.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. void test ()
  28. {
  29. // Create a publisher
  30. void *pub = test_context_socket (ZMQ_XPUB);
  31. int hwm = 2000;
  32. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (pub, ZMQ_SNDHWM, &hwm, 4));
  33. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pub, "inproc://soname"));
  34. // set pub socket options
  35. int wait = 1;
  36. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (pub, ZMQ_XPUB_NODROP, &wait, 4));
  37. // Create a subscriber
  38. void *sub = test_context_socket (ZMQ_SUB);
  39. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub, "inproc://soname"));
  40. // Subscribe for all messages.
  41. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0));
  42. // we must wait for the subscription to be processed here, otherwise some
  43. // or all published messages might be lost
  44. recv_string_expect_success (pub, "\1", 0);
  45. int hwmlimit = hwm - 1;
  46. int send_count = 0;
  47. // Send an empty message
  48. for (int i = 0; i < hwmlimit; i++) {
  49. TEST_ASSERT_SUCCESS_ERRNO (zmq_send (pub, NULL, 0, 0));
  50. send_count++;
  51. }
  52. int recv_count = 0;
  53. do {
  54. // Receive the message in the subscriber
  55. int rc = zmq_recv (sub, NULL, 0, 0);
  56. if (rc == -1) {
  57. TEST_ASSERT_EQUAL_INT (EAGAIN, errno);
  58. break;
  59. }
  60. TEST_ASSERT_EQUAL_INT (0, rc);
  61. recv_count++;
  62. if (recv_count == 1) {
  63. const int sub_rcvtimeo = 250;
  64. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
  65. sub, ZMQ_RCVTIMEO, &sub_rcvtimeo, sizeof (sub_rcvtimeo)));
  66. }
  67. } while (true);
  68. TEST_ASSERT_EQUAL_INT (send_count, recv_count);
  69. // Now test real blocking behavior
  70. // Set a timeout, default is infinite
  71. int timeout = 0;
  72. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (pub, ZMQ_SNDTIMEO, &timeout, 4));
  73. send_count = 0;
  74. recv_count = 0;
  75. hwmlimit = hwm;
  76. // Send an empty message until we get an error, which must be EAGAIN
  77. while (zmq_send (pub, "", 0, 0) == 0)
  78. send_count++;
  79. TEST_ASSERT_EQUAL_INT (EAGAIN, errno);
  80. if (send_count > 0) {
  81. // Receive first message with blocking
  82. TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (sub, NULL, 0, 0));
  83. recv_count++;
  84. while (zmq_recv (sub, NULL, 0, ZMQ_DONTWAIT) == 0)
  85. recv_count++;
  86. }
  87. TEST_ASSERT_EQUAL_INT (send_count, recv_count);
  88. // Clean up.
  89. test_context_socket_close (pub);
  90. test_context_socket_close (sub);
  91. }
  92. int main ()
  93. {
  94. setup_test_environment ();
  95. UNITY_BEGIN ();
  96. RUN_TEST (test);
  97. return UNITY_END ();
  98. }