test_xpub_topic.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 char bind_address[] = "tcp://127.0.0.1:*";
10 
11 // 245 chars + 10 chars for subscribe command = 255 chars
12 const char short_topic[] =
13  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
14  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
15  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
16  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDE";
17 
18 // 246 chars + 10 chars for subscribe command = 256 chars
19 const char long_topic[] =
20  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
21  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
22  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
23  "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEF";
24 
25 
26 template <size_t SIZE>
27 void test_subscribe_cancel (void *xpub, void *sub, const char (&topic)[SIZE])
28 {
29  // Ignore '\0' terminating the topic string.
30  const size_t topic_len = SIZE - 1;
31 
32  // Subscribe for topic
34  zmq_setsockopt (sub, ZMQ_SUBSCRIBE, topic, topic_len));
35 
36  // Allow receiving more than the expected number of bytes
37  char buffer[topic_len + 5];
38 
39  // Receive subscription
40  int rc =
41  TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (xpub, buffer, sizeof (buffer), 0));
42  TEST_ASSERT_EQUAL_INT (topic_len + 1, rc);
44  TEST_ASSERT_EQUAL_UINT8_ARRAY (topic, buffer + 1, topic_len);
45 
46  // Unsubscribe from topic
48  zmq_setsockopt (sub, ZMQ_UNSUBSCRIBE, topic, topic_len));
49 
50  // Receive unsubscription
51  rc =
52  TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (xpub, buffer, sizeof (buffer), 0));
53  TEST_ASSERT_EQUAL_INT (topic_len + 1, rc);
55  TEST_ASSERT_EQUAL_UINT8_ARRAY (topic, buffer + 1, topic_len);
56 }
57 
59 {
60  void *xpub = test_context_socket (ZMQ_XPUB);
62  size_t len = MAX_SOCKET_STRING;
65 
66  void *sub = test_context_socket (ZMQ_SUB);
68 
70  test_subscribe_cancel (xpub, sub, long_topic);
71 
72  // Clean up.
75 }
76 
77 int main ()
78 {
80 
81  UNITY_BEGIN ();
83 
84  return UNITY_END ();
85 }
main
int main()
Definition: test_xpub_topic.cpp:77
bind_address
const SETUP_TEARDOWN_TESTCONTEXT char bind_address[]
Definition: test_xpub_topic.cpp:8
UNITY_END
return UNITY_END()
ZMQ_XPUB
#define ZMQ_XPUB
Definition: zmq.h:267
short_topic
const char short_topic[]
Definition: test_xpub_topic.cpp:12
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
ZMQ_LAST_ENDPOINT
#define ZMQ_LAST_ENDPOINT
Definition: zmq.h:298
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
TEST_ASSERT_EQUAL_UINT8_ARRAY
#define TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements)
Definition: unity.h:246
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
long_topic
const char long_topic[]
Definition: test_xpub_topic.cpp:19
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
MAX_SOCKET_STRING
#define MAX_SOCKET_STRING
Definition: libzmq/tests/testutil.hpp:35
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
buffer
Definition: buffer_processor.h:43
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
test_xpub_subscribe_long_topic
void test_xpub_subscribe_long_topic()
Definition: test_xpub_topic.cpp:58
len
int len
Definition: php/ext/google/protobuf/map.c:206
TEST_ASSERT_EQUAL_UINT8
#define TEST_ASSERT_EQUAL_UINT8(expected, actual)
Definition: unity.h:136
connect_address
char connect_address[MAX_SOCKET_STRING]
Definition: test_xpub_topic.cpp:9
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
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
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_subscribe_cancel
void test_subscribe_cancel(void *xpub, void *sub, const char(&topic)[SIZE])
Definition: test_xpub_topic.cpp:27


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