test_router_mandatory_hwm.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 // DEBUG shouldn't be defined in sources as it will cause a redefined symbol
9 // error when it is defined in the build configuration. It appears that the
10 // intent here is to semi-permanently disable DEBUG tracing statements, so the
11 // implementation is changed to accommodate that intent.
12 //#define DEBUG 0
13 #define TRACE_ENABLED 0
14 
16 {
17  if (TRACE_ENABLED)
18  fprintf (stderr, "Staring router mandatory HWM test ...\n");
20  void *router = test_context_socket (ZMQ_ROUTER);
21 
22  // Configure router socket to mandatory routing and set HWM and linger
23  int mandatory = 1;
25  &mandatory, sizeof (mandatory)));
26  int sndhwm = 1;
28  zmq_setsockopt (router, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm)));
29  int linger = 1;
31  zmq_setsockopt (router, ZMQ_LINGER, &linger, sizeof (linger)));
32 
33  bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
34 
35  // Create dealer called "X" and connect it to our router, configure HWM
36  void *dealer = test_context_socket (ZMQ_DEALER);
38  int rcvhwm = 1;
40  zmq_setsockopt (dealer, ZMQ_RCVHWM, &rcvhwm, sizeof (rcvhwm)));
41 
43 
44  // Get message from dealer to know when connection is ready
45  send_string_expect_success (dealer, "Hello", 0);
46  recv_string_expect_success (router, "X", 0);
47 
48  int i;
49  const int buf_size = 65536;
50  const uint8_t buf[buf_size] = {0};
51  // Send first batch of messages
52  for (i = 0; i < 100000; ++i) {
53  if (TRACE_ENABLED)
54  fprintf (stderr, "Sending message %d ...\n", i);
55  const int rc = zmq_send (router, "X", 1, ZMQ_DONTWAIT | ZMQ_SNDMORE);
56  if (rc == -1 && zmq_errno () == EAGAIN)
57  break;
58  TEST_ASSERT_EQUAL_INT (1, rc);
60  }
61  // This should fail after one message but kernel buffering could
62  // skew results
64  msleep (1000);
65  // Send second batch of messages
66  for (; i < 100000; ++i) {
67  if (TRACE_ENABLED)
68  fprintf (stderr, "Sending message %d (part 2) ...\n", i);
69  const int rc = zmq_send (router, "X", 1, ZMQ_DONTWAIT | ZMQ_SNDMORE);
70  if (rc == -1 && zmq_errno () == EAGAIN)
71  break;
72  TEST_ASSERT_EQUAL_INT (1, rc);
74  }
75  // This should fail after two messages but kernel buffering could
76  // skew results
78 
79  if (TRACE_ENABLED)
80  fprintf (stderr, "Done sending messages.\n");
81 
84 }
85 
86 int main ()
87 {
89 
90  UNITY_BEGIN ();
92  return UNITY_END ();
93 }
main
int main()
Definition: test_router_mandatory_hwm.cpp:86
UNITY_END
return UNITY_END()
zmq_errno
ZMQ_EXPORT int zmq_errno(void)
Definition: zmq.cpp:101
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
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
testutil_unity.hpp
ZMQ_RCVHWM
#define ZMQ_RCVHWM
Definition: zmq.h:294
ZMQ_DEALER
#define ZMQ_DEALER
Definition: zmq.h:263
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
testutil.hpp
ZMQ_ROUTER
#define ZMQ_ROUTER
Definition: zmq.h:264
send_array_expect_success
void send_array_expect_success(void *socket_, const uint8_t(&array_)[SIZE], int flags_)
Definition: testutil_unity.hpp:132
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
test_router_mandatory_hwm
void test_router_mandatory_hwm()
Definition: test_router_mandatory_hwm.cpp:15
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
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_ROUTER_MANDATORY
#define ZMQ_ROUTER_MANDATORY
Definition: zmq.h:299
TRACE_ENABLED
#define TRACE_ENABLED
Definition: test_router_mandatory_hwm.cpp:13
ZMQ_SNDHWM
#define ZMQ_SNDHWM
Definition: zmq.h:293
ZMQ_LINGER
#define ZMQ_LINGER
Definition: zmq.h:288
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
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_SNDMORE
#define ZMQ_SNDMORE
Definition: zmq.h:359
ZMQ_ROUTING_ID
#define ZMQ_ROUTING_ID
Definition: zmq.h:277
TEST_ASSERT_LESS_THAN_INT
#define TEST_ASSERT_LESS_THAN_INT(threshold, actual)
Definition: unity.h:169
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


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