test_pub_invert_matching.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <string.h>
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. void test ()
  29. {
  30. // Create a publisher
  31. void *pub = test_context_socket (ZMQ_PUB);
  32. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pub, "inproc://soname"));
  33. // Create two subscribers
  34. void *sub1 = test_context_socket (ZMQ_SUB);
  35. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub1, "inproc://soname"));
  36. void *sub2 = test_context_socket (ZMQ_SUB);
  37. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub2, "inproc://soname"));
  38. // Subscribe pub1 to one prefix
  39. // and pub2 to another prefix.
  40. const char prefi_x1[] = "prefix1";
  41. const char prefi_x2[] = "p2";
  42. TEST_ASSERT_SUCCESS_ERRNO (
  43. zmq_setsockopt (sub1, ZMQ_SUBSCRIBE, prefi_x1, strlen (prefi_x1)));
  44. TEST_ASSERT_SUCCESS_ERRNO (
  45. zmq_setsockopt (sub2, ZMQ_SUBSCRIBE, prefi_x2, strlen (prefi_x2)));
  46. // Send a message with the first prefix
  47. send_string_expect_success (pub, prefi_x1, 0);
  48. msleep (SETTLE_TIME);
  49. // sub1 should receive it, but not sub2
  50. recv_string_expect_success (sub1, prefi_x1, ZMQ_DONTWAIT);
  51. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT));
  52. // Send a message with the second prefix
  53. send_string_expect_success (pub, prefi_x2, 0);
  54. msleep (SETTLE_TIME);
  55. // sub2 should receive it, but not sub1
  56. recv_string_expect_success (sub2, prefi_x2, ZMQ_DONTWAIT);
  57. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT));
  58. // Now invert the matching
  59. int invert = 1;
  60. TEST_ASSERT_SUCCESS_ERRNO (
  61. zmq_setsockopt (pub, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
  62. // ... on both sides, otherwise the SUB socket will filter the messages out
  63. TEST_ASSERT_SUCCESS_ERRNO (
  64. zmq_setsockopt (sub1, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
  65. TEST_ASSERT_SUCCESS_ERRNO (
  66. zmq_setsockopt (sub2, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
  67. // Send a message with the first prefix
  68. send_string_expect_success (pub, prefi_x1, 0);
  69. msleep (SETTLE_TIME);
  70. // sub2 should receive it, but not sub1
  71. recv_string_expect_success (sub2, prefi_x1, ZMQ_DONTWAIT);
  72. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (sub1, NULL, 0, ZMQ_DONTWAIT));
  73. // Send a message with the second prefix
  74. send_string_expect_success (pub, prefi_x2, 0);
  75. msleep (SETTLE_TIME);
  76. // sub1 should receive it, but not sub2
  77. recv_string_expect_success (sub1, prefi_x2, ZMQ_DONTWAIT);
  78. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (sub2, NULL, 0, ZMQ_DONTWAIT));
  79. // Clean up.
  80. test_context_socket_close (pub);
  81. test_context_socket_close (sub1);
  82. test_context_socket_close (sub2);
  83. }
  84. int main ()
  85. {
  86. setup_test_environment ();
  87. UNITY_BEGIN ();
  88. RUN_TEST (test);
  89. return UNITY_END ();
  90. }