test_xpub_verbose.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 
7 
8 const uint8_t unsubscribe_a_msg[] = {0, 'A'};
9 const uint8_t subscribe_a_msg[] = {1, 'A'};
10 const uint8_t subscribe_b_msg[] = {1, 'B'};
11 
12 const char test_endpoint[] = "inproc://soname";
13 const char topic_a[] = "A";
14 const char topic_b[] = "B";
15 
17 {
18  void *pub = test_context_socket (ZMQ_XPUB);
20 
21  void *sub = test_context_socket (ZMQ_SUB);
23 
24  // Subscribe for A
26 
27  // Receive subscriptions from subscriber
29 
30  // Subscribe socket for B instead
32 
33  // Receive subscriptions from subscriber
35 
36  // Subscribe again for A again
38 
39  // This time it is duplicated, so it will be filtered out
41 
42  int verbose = 1;
44  zmq_setsockopt (pub, ZMQ_XPUB_VERBOSE, &verbose, sizeof (int)));
45 
46  // Subscribe socket for A again
48 
49  // This time with VERBOSE the duplicated sub will be received
51 
52  // Sending A message and B Message
55 
58 
59  // Clean up.
62 }
63 
64 void create_xpub_with_2_subs (void **pub_, void **sub0_, void **sub1_)
65 {
66  *pub_ = test_context_socket (ZMQ_XPUB);
68 
69  *sub0_ = test_context_socket (ZMQ_SUB);
71 
72  *sub1_ = test_context_socket (ZMQ_SUB);
74 }
75 
76 void create_duplicate_subscription (void *pub_, void *sub0_, void *sub1_)
77 {
78  // Subscribe for A
80  zmq_setsockopt (sub0_, ZMQ_SUBSCRIBE, topic_a, 1));
81 
82  // Receive subscriptions from subscriber
84 
85  // Subscribe again for A on the other socket
87  zmq_setsockopt (sub1_, ZMQ_SUBSCRIBE, topic_a, 1));
88 
89  // This time it is duplicated, so it will be filtered out by XPUB
91 }
92 
94 {
95  void *pub, *sub0, *sub1;
96  create_xpub_with_2_subs (&pub, &sub0, &sub1);
97  create_duplicate_subscription (pub, sub0, sub1);
98 
99  // Subscribe socket for B instead
101  zmq_setsockopt (sub0, ZMQ_SUBSCRIBE, topic_b, 1));
102 
103  // Receive subscriptions from subscriber
105 
106  int verbose = 1;
108  zmq_setsockopt (pub, ZMQ_XPUB_VERBOSE, &verbose, sizeof (int)));
109 
110  // Subscribe socket for A again
112  zmq_setsockopt (sub1, ZMQ_SUBSCRIBE, topic_a, 1));
113 
114  // This time with VERBOSE the duplicated sub will be received
116 
117  // Sending A message and B Message
119 
121 
125 
126  // Clean up.
130 }
131 
133 {
134  // Create a publisher
135  void *pub = test_context_socket (ZMQ_XPUB);
137 
138  // Create a subscriber
139  void *sub = test_context_socket (ZMQ_SUB);
141 
142  // Unsubscribe for A, does not exist yet
145 
146  // Does not exist, so it will be filtered out by XSUB
148 
149  // Subscribe for A
151 
152  // Receive subscriptions from subscriber
154 
155  // Subscribe again for A again, XSUB will increase refcount
157 
158  // This time it is duplicated, so it will be filtered out by XPUB
160 
161  // Unsubscribe for A, this time it exists in XPUB
164 
165  // XSUB refcounts and will not actually send unsub to PUB until the number
166  // of unsubs match the earlier subs
169 
170  // Receive unsubscriptions from subscriber
172 
173  // XSUB only sends the last and final unsub, so XPUB will only receive 1
175 
176  // Unsubscribe for A, does not exist anymore
179 
180  // Does not exist, so it will be filtered out by XSUB
182 
183  int verbose = 1;
185  zmq_setsockopt (pub, ZMQ_XPUB_VERBOSER, &verbose, sizeof (int)));
186 
187  // Subscribe socket for A again
189 
190  // Receive subscriptions from subscriber, did not exist anymore
192 
193  // Sending A message to make sure everything still works
195 
197 
198  // Unsubscribe for A, this time it exists
201 
202  // Receive unsubscriptions from subscriber
204 
205  // Unsubscribe for A again, it does not exist anymore so XSUB will filter
208 
209  // XSUB only sends unsub if it matched it in its trie, IOW: it will only
210  // send it if it existed in the first place even with XPUB_VERBBOSER
212 
213  // Clean up.
216 }
217 
219 {
220  void *pub, *sub0, *sub1;
221  create_xpub_with_2_subs (&pub, &sub0, &sub1);
222  create_duplicate_subscription (pub, sub0, sub1);
223 
224  // Unsubscribe for A, this time it exists in XPUB
227 
228  // sub1 is still subscribed, so no notification
230 
231  // Unsubscribe the second socket to trigger the notification
234 
235  // Receive unsubscriptions since all sockets are gone
237 
238  // Make really sure there is only one notification
240 
241  int verbose = 1;
243  zmq_setsockopt (pub, ZMQ_XPUB_VERBOSER, &verbose, sizeof (int)));
244 
245  // Subscribe socket for A again
247  zmq_setsockopt (sub0, ZMQ_SUBSCRIBE, topic_a, 1));
248 
249  // Subscribe socket for A again
251  zmq_setsockopt (sub1, ZMQ_SUBSCRIBE, topic_a, 1));
252 
253  // Receive subscriptions from subscriber, did not exist anymore
255 
256  // VERBOSER is set, so subs from both sockets are received
258 
259  // Sending A message to make sure everything still works
261 
264 
265  // Unsubscribe for A
268 
269  // Receive unsubscriptions from first subscriber due to VERBOSER
271 
272  // Unsubscribe for A again from the other socket
275 
276  // Receive unsubscriptions from first subscriber due to VERBOSER
278 
279  // Unsubscribe again to make sure it gets filtered now
282 
283  // Unmatched, so XSUB filters even with VERBOSER
285 
286  // Clean up.
290 }
291 
292 int main ()
293 {
295 
296  UNITY_BEGIN ();
301 
302  return UNITY_END ();
303 }
recv_array_expect_success
void recv_array_expect_success(void *socket_, const uint8_t(&array_)[SIZE], int flags_)
Definition: testutil_unity.hpp:148
topic_b
const char topic_b[]
Definition: test_xpub_verbose.cpp:14
subscribe_a_msg
const uint8_t subscribe_a_msg[]
Definition: test_xpub_verbose.cpp:9
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
ZMQ_XPUB
#define ZMQ_XPUB
Definition: zmq.h:267
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
topic_a
const char topic_a[]
Definition: test_xpub_verbose.cpp:13
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
test_endpoint
const char test_endpoint[]
Definition: test_xpub_verbose.cpp:12
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
testutil.hpp
main
int main()
Definition: test_xpub_verbose.cpp:292
unsubscribe_a_msg
const SETUP_TEARDOWN_TESTCONTEXT uint8_t unsubscribe_a_msg[]
Definition: test_xpub_verbose.cpp:8
create_duplicate_subscription
void create_duplicate_subscription(void *pub_, void *sub0_, void *sub1_)
Definition: test_xpub_verbose.cpp:76
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
create_xpub_with_2_subs
void create_xpub_with_2_subs(void **pub_, void **sub0_, void **sub1_)
Definition: test_xpub_verbose.cpp:64
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
test_xpub_verboser_one_sub
void test_xpub_verboser_one_sub()
Definition: test_xpub_verbose.cpp:132
test_xpub_verboser_two_subs
void test_xpub_verboser_two_subs()
Definition: test_xpub_verbose.cpp:218
ZMQ_XPUB_VERBOSE
#define ZMQ_XPUB_VERBOSE
Definition: zmq.h:305
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
ZMQ_XPUB_VERBOSER
#define ZMQ_XPUB_VERBOSER
Definition: zmq.h:339
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
ZMQ_UNSUBSCRIBE
#define ZMQ_UNSUBSCRIBE
Definition: zmq.h:279
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
subscribe_b_msg
const uint8_t subscribe_b_msg[]
Definition: test_xpub_verbose.cpp:10
test_xpub_verbose_two_subs
void test_xpub_verbose_two_subs()
Definition: test_xpub_verbose.cpp:93
test_xpub_verbose_one_sub
void test_xpub_verbose_one_sub()
Definition: test_xpub_verbose.cpp:16
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
verbose
bool verbose
Definition: conformance_cpp.cc:96
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47


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