test_pub_invert_matching.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "testutil.hpp"
4 #include "testutil_unity.hpp"
5 
6 #include <string.h>
7 
9 
10 void test ()
11 {
12  // Create a publisher
13  void *pub = test_context_socket (ZMQ_PUB);
14  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pub, "inproc://soname"));
15 
16  // Create two subscribers
17  void *sub1 = test_context_socket (ZMQ_SUB);
18  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub1, "inproc://soname"));
19 
20  void *sub2 = test_context_socket (ZMQ_SUB);
21  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub2, "inproc://soname"));
22 
23  // Subscribe pub1 to one prefix
24  // and pub2 to another prefix.
25  const char prefi_x1[] = "prefix1";
26  const char prefi_x2[] = "p2";
27 
29  zmq_setsockopt (sub1, ZMQ_SUBSCRIBE, prefi_x1, strlen (prefi_x1)));
31  zmq_setsockopt (sub2, ZMQ_SUBSCRIBE, prefi_x2, strlen (prefi_x2)));
32 
33  // Send a message with the first prefix
34  send_string_expect_success (pub, prefi_x1, 0);
36 
37  // sub1 should receive it, but not sub2
38  recv_string_expect_success (sub1, prefi_x1, ZMQ_DONTWAIT);
39 
41 
42  // Send a message with the second prefix
43  send_string_expect_success (pub, prefi_x2, 0);
45 
46  // sub2 should receive it, but not sub1
47  recv_string_expect_success (sub2, prefi_x2, ZMQ_DONTWAIT);
48 
50 
51  // Now invert the matching
52  int invert = 1;
54  zmq_setsockopt (pub, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
55 
56  // ... on both sides, otherwise the SUB socket will filter the messages out
58  zmq_setsockopt (sub1, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
60  zmq_setsockopt (sub2, ZMQ_INVERT_MATCHING, &invert, sizeof (invert)));
61 
62  // Send a message with the first prefix
63  send_string_expect_success (pub, prefi_x1, 0);
65 
66  // sub2 should receive it, but not sub1
67  recv_string_expect_success (sub2, prefi_x1, ZMQ_DONTWAIT);
68 
70 
71  // Send a message with the second prefix
72  send_string_expect_success (pub, prefi_x2, 0);
74 
75  // sub1 should receive it, but not sub2
76  recv_string_expect_success (sub1, prefi_x2, ZMQ_DONTWAIT);
77 
79 
80  // Clean up.
84 }
85 
86 int main ()
87 {
89 
90  UNITY_BEGIN ();
91  RUN_TEST (test);
92  return UNITY_END ();
93 }
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
ZMQ_PUB
#define ZMQ_PUB
Definition: zmq.h:259
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
EAGAIN
#define EAGAIN
Definition: errno.hpp:14
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
ZMQ_SUBSCRIBE
#define ZMQ_SUBSCRIBE
Definition: zmq.h:278
ZMQ_SUB
#define ZMQ_SUB
Definition: zmq.h:260
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
testutil_unity.hpp
invert
GLboolean invert
Definition: glcorearb.h:2878
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
testutil.hpp
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
ZMQ_INVERT_MATCHING
#define ZMQ_INVERT_MATCHING
Definition: zmq.h:335
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
test
SETUP_TEARDOWN_TESTCONTEXT void test()
Definition: test_pub_invert_matching.cpp:10
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
recv_string_expect_success
void recv_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:101
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
main
int main()
Definition: test_pub_invert_matching.cpp:86


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59