test_timeo.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_timeo ()
9 {
10  void *frontend = test_context_socket (ZMQ_DEALER);
11  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (frontend, "inproc://timeout_test"));
12 
13  // Receive on disconnected socket returns immediately
14  char buffer[32];
16  zmq_recv (frontend, buffer, 32, ZMQ_DONTWAIT));
17 
18 
19  // Check whether receive timeout is honored
20  const int timeout = 250;
21  const int jitter = 50;
23  zmq_setsockopt (frontend, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
24 
25  void *stopwatch = zmq_stopwatch_start ();
26  TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (frontend, buffer, 32, 0));
27  unsigned int elapsed = zmq_stopwatch_stop (stopwatch) / 1000;
28  TEST_ASSERT_GREATER_THAN_INT (timeout - jitter, elapsed);
29  if (elapsed >= timeout + jitter) {
30  // we cannot assert this on a non-RT system
31  fprintf (stderr,
32  "zmq_recv took quite long, with a timeout of %i ms, it took "
33  "actually %i ms\n",
34  timeout, elapsed);
35  }
36 
37  // Check that normal message flow works as expected
38  void *backend = test_context_socket (ZMQ_DEALER);
39  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (backend, "inproc://timeout_test"));
41  zmq_setsockopt (backend, ZMQ_SNDTIMEO, &timeout, sizeof (int)));
42 
43  send_string_expect_success (backend, "Hello", 0);
44  recv_string_expect_success (frontend, "Hello", 0);
45 
46  // Clean-up
47  test_context_socket_close (backend);
48  test_context_socket_close (frontend);
49 }
50 
51 int main ()
52 {
54 
55  UNITY_BEGIN ();
57  return UNITY_END ();
58 }
UNITY_END
return UNITY_END()
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
test_timeo
SETUP_TEARDOWN_TESTCONTEXT void test_timeo()
Definition: test_timeo.cpp:8
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
main
int main()
Definition: test_timeo.cpp:51
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
ZMQ_SNDTIMEO
#define ZMQ_SNDTIMEO
Definition: zmq.h:297
testutil.hpp
zmq_stopwatch_start
ZMQ_EXPORT void * zmq_stopwatch_start(void)
Definition: zmq_utils.cpp:32
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
timeout
GLbitfield GLuint64 timeout
Definition: glcorearb.h:3588
buffer
Definition: buffer_processor.h:43
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
ZMQ_DONTWAIT
#define ZMQ_DONTWAIT
Definition: zmq.h:358
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
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()
TEST_ASSERT_FAILURE_ERRNO
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr)
Definition: testutil_unity.hpp:95
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_stopwatch_stop
ZMQ_EXPORT unsigned long zmq_stopwatch_stop(void *watch_)
Definition: zmq_utils.cpp:47


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