test_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 {
14 };
15 
17 {
18  // Set up bind socket
19  void *bind_socket = test_context_socket (ZMQ_PULL);
20  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
21 
22  // Set up connect socket
25 
26  // Send until we block
27  int send_count = 0;
28  while (send_count < MAX_SENDS
30  ++send_count;
31 
33 
34  // Now receive all sent messages
35  int recv_count = 0;
36  while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
37  ++recv_count;
38 
39  TEST_ASSERT_EQUAL_INT (send_count, recv_count);
40 
41  // Clean up
43  test_context_socket_close (bind_socket);
44 
45  // Default values are 1000 on send and 1000 one receive, so 2000 total
46  TEST_ASSERT_EQUAL_INT (2000, send_count);
47 }
48 
49 int count_msg (int send_hwm_, int recv_hwm_, TestType test_type_)
50 {
51  void *bind_socket;
52  void *connect_socket;
53  if (test_type_ == BIND_FIRST) {
54  // Set up bind socket
55  bind_socket = test_context_socket (ZMQ_PULL);
57  bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
58  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
59 
60  // Set up connect socket
63  connect_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
65 
66  // we must wait for the connect to succeed here, unfortunately we don't
67  // have monitoring events for inproc, so we just hope SETTLE_TIME suffices
69  } else {
70  // Set up connect socket
73  connect_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
75 
76  // Set up bind socket
77  bind_socket = test_context_socket (ZMQ_PULL);
79  bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
80  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
81  }
82 
83  // Send until we block
84  int send_count = 0;
85  while (send_count < MAX_SENDS
87  ++send_count;
88 
89  // Now receive all sent messages
90  int recv_count = 0;
91  while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
92  ++recv_count;
93 
94  TEST_ASSERT_EQUAL_INT (send_count, recv_count);
95 
96  // Now it should be possible to send one more.
98 
99  // Consume the remaining message.
100  recv_string_expect_success (bind_socket, NULL, 0);
101 
102  // Clean up
104  test_context_socket_close (bind_socket);
105 
106  return send_count;
107 }
108 
109 int test_inproc_bind_first (int send_hwm_, int recv_hwm_)
110 {
111  return count_msg (send_hwm_, recv_hwm_, BIND_FIRST);
112 }
113 
114 int test_inproc_connect_first (int send_hwm_, int recv_hwm_)
115 {
116  return count_msg (send_hwm_, recv_hwm_, CONNECT_FIRST);
117 }
118 
119 int test_inproc_connect_and_close_first (int send_hwm_, int recv_hwm_)
120 {
121  // Set up connect socket
124  &send_hwm_, sizeof (send_hwm_)));
126 
127  // Send until we block
128  int send_count = 0;
129  while (send_count < MAX_SENDS
130  && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
131  ++send_count;
132 
133  // Close connect
135 
136  // Set up bind socket
137  void *bind_socket = test_context_socket (ZMQ_PULL);
139  zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
140  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
141 
142  // Now receive all sent messages
143  int recv_count = 0;
144  while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
145  ++recv_count;
146 
147  TEST_ASSERT_EQUAL_INT (send_count, recv_count);
148 
149  // Clean up
150  test_context_socket_close (bind_socket);
151 
152  return send_count;
153 }
154 
155 int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */)
156 {
157  // Set up bind socket
158  void *bind_socket = test_context_socket (ZMQ_PUSH);
160  zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
161  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
162 
163  // Send until we block
164  int send_count = 0;
165  while (send_count < MAX_SENDS
166  && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
167  ++send_count;
168 
169  // Close bind
170  test_context_socket_close (bind_socket);
171 
172  /* TODO Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
173  // Set up connect socket
174  void *connect_socket = test_context_socket (ZMQ_PULL);
175  TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
176  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
177 
178  // Now receive all sent messages
179  int recv_count = 0;
180  while (zmq_recv (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
181  ++recv_count;
182 
183  TEST_ASSERT_EQUAL_INT(send_count, recv_count);
184  */
185 
186  // Clean up
187  //test_context_socket_close (connect_socket);
188 
189  return send_count;
190 }
191 
193 {
194  int count = test_inproc_bind_first (0, 0);
196 }
197 
199 {
200  int count = test_inproc_connect_first (0, 0);
202 }
203 
205 {
206  int count = test_inproc_bind_first (1, 0);
208 }
209 
211 {
212  int count = test_inproc_connect_first (1, 0);
214 }
215 
217 {
218  int count = test_inproc_bind_first (0, 1);
220 }
221 
223 {
224  int count = test_inproc_connect_first (0, 1);
226 }
227 
229 {
230  // Send and recv buffers hwm 1, so total that can be queued is 2
231  int count = test_inproc_bind_first (1, 1);
233 }
235 {
236  // Send and recv buffers hwm 1, so total that can be queued is 2
237  int count = test_inproc_connect_first (1, 1);
239 }
240 
242 {
243  // Send hwm of 1, send before bind so total that can be queued is 1
246 }
247 
249 {
250  // Send hwm of 1, send from bind side before connect so total that can be queued should be 1,
251  // however currently all messages get thrown away before the connect. BUG?
252  /*int count = */ test_inproc_bind_and_close_first (1, 0);
253  // TEST_ASSERT_EQUAL_INT (1, count);
254 }
255 
256 int main (void)
257 {
259 
260  UNITY_BEGIN ();
262 
265 
268 
271 
274 
277 
278  return UNITY_END ();
279 }
test_inproc_connect_first
int test_inproc_connect_first(int send_hwm_, int recv_hwm_)
Definition: test_hwm.cpp:114
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
BIND_FIRST
@ BIND_FIRST
Definition: test_hwm.cpp:12
test_infinite_send_inproc_connect_first
void test_infinite_send_inproc_connect_first()
Definition: test_hwm.cpp:222
SETUP_TEARDOWN_TESTCONTEXT
#define SETUP_TEARDOWN_TESTCONTEXT
Definition: testutil_unity.hpp:172
test_infinite_send_inproc_bind_first
void test_infinite_send_inproc_bind_first()
Definition: test_hwm.cpp:216
MAX_SENDS
const SETUP_TEARDOWN_TESTCONTEXT int MAX_SENDS
Definition: test_hwm.cpp:8
test_infinite_recv_connect_and_close_first
void test_infinite_recv_connect_and_close_first()
Definition: test_hwm.cpp:241
test_inproc_bind_and_close_first
int test_inproc_bind_and_close_first(int send_hwm_, int)
Definition: test_hwm.cpp:155
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
test_infinite_receive_inproc_connect_first
void test_infinite_receive_inproc_connect_first()
Definition: test_hwm.cpp:210
testutil_unity.hpp
TestType
TestType
Definition: test_hwm.cpp:10
ZMQ_RCVHWM
#define ZMQ_RCVHWM
Definition: zmq.h:294
test_infinite_receive_inproc_bind_first
void test_infinite_receive_inproc_bind_first()
Definition: test_hwm.cpp:204
zmq_setsockopt
ZMQ_EXPORT int zmq_setsockopt(void *s_, int option_, const void *optval_, size_t optvallen_)
Definition: zmq.cpp:250
ZMQ_PUSH
#define ZMQ_PUSH
Definition: zmq.h:266
testutil.hpp
test_inproc_connect_and_close_first
int test_inproc_connect_and_close_first(int send_hwm_, int recv_hwm_)
Definition: test_hwm.cpp:119
count_msg
int count_msg(int send_hwm_, int recv_hwm_, TestType test_type_)
Definition: test_hwm.cpp:49
connect_socket
fd_t connect_socket(const char *endpoint_, const int af_, const int protocol_)
Definition: testutil.cpp:353
main
int main(void)
Definition: test_hwm.cpp:256
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
CONNECT_FIRST
@ CONNECT_FIRST
Definition: test_hwm.cpp:13
ZMQ_SNDHWM
#define ZMQ_SNDHWM
Definition: zmq.h:293
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
test_defaults
void test_defaults()
Definition: test_hwm.cpp:16
test_finite_both_connect_first
void test_finite_both_connect_first()
Definition: test_hwm.cpp:234
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()
test_finite_both_bind_first
void test_finite_both_bind_first()
Definition: test_hwm.cpp:228
test_infinite_recv_bind_and_close_first
void test_infinite_recv_bind_and_close_first()
Definition: test_hwm.cpp:248
zmq_send
ZMQ_EXPORT int zmq_send(void *s_, const void *buf_, size_t len_, int flags_)
Definition: zmq.cpp:377
test_infinite_both_inproc_bind_first
void test_infinite_both_inproc_bind_first()
Definition: test_hwm.cpp:192
ZMQ_PULL
#define ZMQ_PULL
Definition: zmq.h:265
test_inproc_bind_first
int test_inproc_bind_first(int send_hwm_, int recv_hwm_)
Definition: test_hwm.cpp:109
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
count
GLint GLsizei count
Definition: glcorearb.h:2830
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
test_infinite_both_inproc_connect_first
void test_infinite_both_inproc_connect_first()
Definition: test_hwm.cpp:198


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