test_xpub_nodrop.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 void test ()
9 {
10  // Create a publisher
11  void *pub = test_context_socket (ZMQ_XPUB);
12 
13  int hwm = 2000;
15 
16  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pub, "inproc://soname"));
17 
18  // set pub socket options
19  int wait = 1;
21 
22  // Create a subscriber
23  void *sub = test_context_socket (ZMQ_SUB);
24  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub, "inproc://soname"));
25 
26  // Subscribe for all messages.
28 
29  // we must wait for the subscription to be processed here, otherwise some
30  // or all published messages might be lost
31  recv_string_expect_success (pub, "\1", 0);
32 
33  int hwmlimit = hwm - 1;
34  int send_count = 0;
35 
36  // Send an empty message
37  for (int i = 0; i < hwmlimit; i++) {
39  send_count++;
40  }
41 
42  int recv_count = 0;
43  do {
44  // Receive the message in the subscriber
45  int rc = zmq_recv (sub, NULL, 0, 0);
46  if (rc == -1) {
48  break;
49  }
50  TEST_ASSERT_EQUAL_INT (0, rc);
51  recv_count++;
52 
53  if (recv_count == 1) {
54  const int sub_rcvtimeo = 250;
56  sub, ZMQ_RCVTIMEO, &sub_rcvtimeo, sizeof (sub_rcvtimeo)));
57  }
58 
59  } while (true);
60 
61  TEST_ASSERT_EQUAL_INT (send_count, recv_count);
62 
63  // Now test real blocking behavior
64  // Set a timeout, default is infinite
65  int timeout = 0;
67 
68  send_count = 0;
69  recv_count = 0;
70  hwmlimit = hwm;
71 
72  // Send an empty message until we get an error, which must be EAGAIN
73  while (zmq_send (pub, "", 0, 0) == 0)
74  send_count++;
76 
77  if (send_count > 0) {
78  // Receive first message with blocking
80  recv_count++;
81 
82  while (zmq_recv (sub, NULL, 0, ZMQ_DONTWAIT) == 0)
83  recv_count++;
84  }
85 
86  TEST_ASSERT_EQUAL_INT (send_count, recv_count);
87 
88  // Clean up.
91 }
92 
93 int main ()
94 {
96  UNITY_BEGIN ();
97  RUN_TEST (test);
98  return UNITY_END ();
99 }
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
main
int main()
Definition: test_xpub_nodrop.cpp:93
errno
int errno
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
test
SETUP_TEARDOWN_TESTCONTEXT void test()
Definition: test_xpub_nodrop.cpp:8
testutil_unity.hpp
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
ZMQ_SNDTIMEO
#define ZMQ_SNDTIMEO
Definition: zmq.h:297
testutil.hpp
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
timeout
GLbitfield GLuint64 timeout
Definition: glcorearb.h:3588
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
i
int i
Definition: gmock-matchers_test.cc:764
ZMQ_SNDHWM
#define ZMQ_SNDHWM
Definition: zmq.h:293
ZMQ_RCVTIMEO
#define ZMQ_RCVTIMEO
Definition: zmq.h:296
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()
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
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_XPUB_NODROP
#define ZMQ_XPUB_NODROP
Definition: zmq.h:330


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