test_sockopt_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 const int MAX_SENDS = 10000;
9 
11 {
12  int rc;
13 
14  void *bind_socket = test_context_socket (ZMQ_PUSH);
16 
17  int val = 2;
18  rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
19  TEST_ASSERT_EQUAL_INT (0, rc);
20  rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
21  TEST_ASSERT_EQUAL_INT (0, rc);
22 
23  zmq_connect (connect_socket, "inproc://a");
24  zmq_bind (bind_socket, "inproc://a");
25 
26  size_t placeholder = sizeof (val);
27  val = 0;
28  rc = zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &val, &placeholder);
29  TEST_ASSERT_EQUAL_INT (0, rc);
31 
32  int send_count = 0;
33  while (send_count < MAX_SENDS
34  && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
35  ++send_count;
36 
37  TEST_ASSERT_EQUAL_INT (4, send_count);
38 
39  test_context_socket_close (bind_socket);
41 }
42 
44 {
45  int rc;
46 
47  void *bind_socket = test_context_socket (ZMQ_PUSH);
49 
50  int val = 1;
51  rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
52  TEST_ASSERT_EQUAL_INT (0, rc);
53  rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
54  TEST_ASSERT_EQUAL_INT (0, rc);
55 
56  zmq_connect (connect_socket, "inproc://a");
57  zmq_bind (bind_socket, "inproc://a");
58 
59  val = 5;
60  rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
61  TEST_ASSERT_EQUAL_INT (0, rc);
62 
63  size_t placeholder = sizeof (val);
64  val = 0;
65  rc = zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &val, &placeholder);
66  TEST_ASSERT_EQUAL_INT (0, rc);
68 
69  int send_count = 0;
70  while (send_count < MAX_SENDS
71  && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
72  ++send_count;
73 
74  TEST_ASSERT_EQUAL_INT (6, send_count);
75 
76  test_context_socket_close (bind_socket);
78 }
79 
80 int send_until_wouldblock (void *socket_)
81 {
82  int send_count = 0;
83  while (send_count < MAX_SENDS
84  && zmq_send (socket_, &send_count, sizeof (send_count), ZMQ_DONTWAIT)
85  == sizeof (send_count)) {
86  ++send_count;
87  }
88  return send_count;
89 }
90 
91 int test_fill_up_to_hwm (void *socket_, int sndhwm_)
92 {
93  int send_count = send_until_wouldblock (socket_);
94  fprintf (stderr, "sndhwm==%i, send_count==%i\n", sndhwm_, send_count);
95  TEST_ASSERT_LESS_OR_EQUAL_INT (sndhwm_ + 1, send_count);
96  TEST_ASSERT_GREATER_THAN_INT (sndhwm_ / 10, send_count);
97  return send_count;
98 }
99 
101 {
102  int rc;
103 
104  void *bind_socket = test_context_socket (ZMQ_PUSH);
106 
107  int val = 1;
108  rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
109  TEST_ASSERT_EQUAL_INT (0, rc);
110 
111  int sndhwm = 100;
112  rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
113  TEST_ASSERT_EQUAL_INT (0, rc);
114 
115  zmq_bind (bind_socket, "inproc://a");
116  zmq_connect (connect_socket, "inproc://a");
117 
118  // we must wait for the connect to succeed here, unfortunately we don't
119  // have monitoring events for inproc, so we just hope SETTLE_TIME suffices
121 
122  // Fill up to hwm
123  int send_count = test_fill_up_to_hwm (bind_socket, sndhwm);
124 
125  // Decrease snd hwm
126  sndhwm = 70;
127  rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
128  TEST_ASSERT_EQUAL_INT (0, rc);
129 
130  int sndhwm_read = 0;
131  size_t sndhwm_read_size = sizeof (sndhwm_read);
132  rc =
133  zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm_read, &sndhwm_read_size);
134  TEST_ASSERT_EQUAL_INT (0, rc);
135  TEST_ASSERT_EQUAL_INT (sndhwm, sndhwm_read);
136 
138 
139  // Read out all data (should get up to previous hwm worth so none were dropped)
140  int read_count = 0;
141  int read_data = 0;
142  while (
143  read_count < MAX_SENDS
144  && zmq_recv (connect_socket, &read_data, sizeof (read_data), ZMQ_DONTWAIT)
145  == sizeof (read_data)) {
146  TEST_ASSERT_EQUAL_INT (read_data, read_count);
147  ++read_count;
148  }
149 
150  TEST_ASSERT_EQUAL_INT (send_count, read_count);
151 
152  // Give io thread some time to catch up
154 
155  // Fill up to new hwm
156  test_fill_up_to_hwm (bind_socket, sndhwm);
157 
158  test_context_socket_close (bind_socket);
160 }
161 
162 
163 int main ()
164 {
166 
167  UNITY_BEGIN ();
171 
172  return UNITY_END ();
173 }
TEST_ASSERT_LESS_OR_EQUAL_INT
#define TEST_ASSERT_LESS_OR_EQUAL_INT(threshold, actual)
Definition: unity.h:201
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
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_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
TEST_ASSERT_GREATER_THAN_INT
#define TEST_ASSERT_GREATER_THAN_INT(threshold, actual)
Definition: unity.h:153
testutil_unity.hpp
ZMQ_RCVHWM
#define ZMQ_RCVHWM
Definition: zmq.h:294
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
test_change_after_connected
void test_change_after_connected()
Definition: test_sockopt_hwm.cpp:43
ZMQ_PUSH
#define ZMQ_PUSH
Definition: zmq.h:266
testutil.hpp
connect_socket
fd_t connect_socket(const char *endpoint_, const int af_, const int protocol_)
Definition: testutil.cpp:353
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
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_sockopt_hwm.cpp:163
ZMQ_SNDHWM
#define ZMQ_SNDHWM
Definition: zmq.h:293
zmq_recv
ZMQ_EXPORT int zmq_recv(void *s_, void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:487
test_change_before_connected
void test_change_before_connected()
Definition: test_sockopt_hwm.cpp:10
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
MAX_SENDS
const SETUP_TEARDOWN_TESTCONTEXT int MAX_SENDS
Definition: test_sockopt_hwm.cpp:8
test_fill_up_to_hwm
int test_fill_up_to_hwm(void *socket_, int sndhwm_)
Definition: test_sockopt_hwm.cpp:91
send_until_wouldblock
int send_until_wouldblock(void *socket_)
Definition: test_sockopt_hwm.cpp:80
val
GLuint GLfloat * val
Definition: glcorearb.h:3604
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
ZMQ_PULL
#define ZMQ_PULL
Definition: zmq.h:265
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
test_decrease_when_full
void test_decrease_when_full()
Definition: test_sockopt_hwm.cpp:100
zmq_getsockopt
ZMQ_EXPORT int zmq_getsockopt(void *s_, int option_, void *optval_, size_t *optvallen_)
Definition: zmq.cpp:261


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