test_pubsub_topics_count.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 #include <string.h>
6 
8 
9 
10 void settle_subscriptions (void *skt)
11 {
12  // To kick the application thread, do a dummy getsockopt - users here
13  // should use the monitor and the other sockets in a poll.
14  unsigned long int dummy;
15  size_t dummy_size = sizeof (dummy);
17  zmq_getsockopt (skt, ZMQ_EVENTS, &dummy, &dummy_size);
18 }
19 
20 int get_subscription_count (void *skt)
21 {
22  int num_subs = 0;
23  size_t num_subs_len = sizeof (num_subs);
24 
27  zmq_getsockopt (skt, ZMQ_TOPICS_COUNT, &num_subs, &num_subs_len));
28 
29  return num_subs;
30 }
31 
33 {
34  // Create a publisher
35  void *publisher = test_context_socket (ZMQ_PUB);
37 
38  // Bind publisher
39  test_bind (publisher, "inproc://soname", my_endpoint, MAX_SOCKET_STRING);
40 
41  // Create a subscriber
42  void *subscriber = test_context_socket (ZMQ_SUB);
44 
45  // Subscribe to 3 topics
47  subscriber, ZMQ_SUBSCRIBE, "topicprefix1", strlen ("topicprefix1")));
49  subscriber, ZMQ_SUBSCRIBE, "topicprefix2", strlen ("topicprefix2")));
51  subscriber, ZMQ_SUBSCRIBE, "topicprefix3", strlen ("topicprefix3")));
54 
55  // Remove first subscription and check subscriptions went 3 -> 2
57  subscriber, ZMQ_UNSUBSCRIBE, "topicprefix3", strlen ("topicprefix3")));
60 
61  // Remove other 2 subscriptions and check we're back to 0 subscriptions
63  subscriber, ZMQ_UNSUBSCRIBE, "topicprefix1", strlen ("topicprefix1")));
65  subscriber, ZMQ_UNSUBSCRIBE, "topicprefix2", strlen ("topicprefix2")));
68 
69  // Clean up.
70  test_context_socket_close (publisher);
71  test_context_socket_close (subscriber);
72 }
73 
75 {
76  // Create a publisher
77  void *publisher = test_context_socket (ZMQ_PUB);
79 
80  // Bind publisher
81  test_bind (publisher, "inproc://soname", my_endpoint, MAX_SOCKET_STRING);
82 
83  // Create a subscriber
84  void *subscriber = test_context_socket (ZMQ_SUB);
86 
87  // Subscribe to 3 (nested) topics
89  zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "a", strlen ("a")));
91  zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "ab", strlen ("ab")));
93  zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "abc", strlen ("abc")));
94 
95  // Even if the subscriptions are nested one into the other, the number of subscriptions
96  // received on the subscriber/publisher socket will be 3:
99 
100  // Subscribe to other 3 (nested) topics
102  zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "xyz", strlen ("xyz")));
104  zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "xy", strlen ("xy")));
106  zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "x", strlen ("x")));
107 
110 
111  // Clean up.
112  test_context_socket_close (publisher);
113  test_context_socket_close (subscriber);
114 }
115 
116 int main ()
117 {
119 
120  UNITY_BEGIN ();
123  return UNITY_END ();
124 }
UNITY_END
return UNITY_END()
ZMQ_PUB
#define ZMQ_PUB
Definition: zmq.h:259
ZMQ_EVENTS
#define ZMQ_EVENTS
Definition: zmq.h:286
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
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
dummy
ReturnVal dummy
Definition: register_benchmark_test.cc:68
ZMQ_SUB
#define ZMQ_SUB
Definition: zmq.h:260
ZMQ_TOPICS_COUNT
#define ZMQ_TOPICS_COUNT
Definition: zmq_draft.h:48
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
settle_subscriptions
SETUP_TEARDOWN_TESTCONTEXT void settle_subscriptions(void *skt)
Definition: test_pubsub_topics_count.cpp:10
test_independent_topic_prefixes
void test_independent_topic_prefixes()
Definition: test_pubsub_topics_count.cpp:32
testutil_unity.hpp
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
testutil.hpp
my_endpoint
char my_endpoint[MAX_SOCKET_STRING]
Definition: test_security_curve.cpp:31
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
get_subscription_count
int get_subscription_count(void *skt)
Definition: test_pubsub_topics_count.cpp:20
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
main
int main()
Definition: test_pubsub_topics_count.cpp:116
ZMQ_UNSUBSCRIBE
#define ZMQ_UNSUBSCRIBE
Definition: zmq.h:279
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
test_nested_topic_prefixes
void test_nested_topic_prefixes()
Definition: test_pubsub_topics_count.cpp:74
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
zmq_getsockopt
ZMQ_EXPORT int zmq_getsockopt(void *s_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:261
test_bind
void test_bind(void *socket_, const char *bind_address_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:223


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