test_proxy_terminate.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 
6 #include <stdlib.h>
7 
8 void setUp ()
9 {
11 }
12 
13 // This is a test for issue #1382. The server thread creates a SUB-PUSH
14 // steerable proxy. The main process then sends messages to the SUB
15 // but there is no pull on the other side, previously the proxy blocks
16 // in writing to the backend, preventing the proxy from terminating
17 
18 void server_task (void * /*unused_*/)
19 {
21  // Frontend socket talks to main process
22  void *frontend = zmq_socket (get_test_context (), ZMQ_SUB);
23  TEST_ASSERT_NOT_NULL (frontend);
25  bind_loopback_ipv4 (frontend, my_endpoint, sizeof my_endpoint);
26 
27  // Nice socket which is never read
28  void *backend = zmq_socket (get_test_context (), ZMQ_PUSH);
29  TEST_ASSERT_NOT_NULL (backend);
30  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tcp://127.0.0.1:*"));
31 
32  // Control socket receives terminate command from main over inproc
33  void *control = zmq_socket (get_test_context (), ZMQ_REQ);
34  TEST_ASSERT_NOT_NULL (control);
35  TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
37 
38  // Connect backend to frontend via a proxy
39  zmq_proxy (frontend, backend, NULL);
40 
44 }
45 
46 
47 // The main thread simply starts a basic steerable proxy server, publishes some messages, and then
48 // waits for the server to terminate.
50 {
51  void *thread = zmq_threadstart (&server_task, NULL);
52 
53  // Control socket receives terminate command from main over inproc
54  void *control = test_context_socket (ZMQ_REP);
55  TEST_ASSERT_NOT_NULL (control);
56  TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (control, "inproc://control"));
57  char *my_endpoint = s_recv (control);
59 
60  msleep (500); // Run for 500 ms
61 
62  // Start a secondary publisher which writes data to the SUB-PUSH server socket
63  void *publisher = test_context_socket (ZMQ_PUB);
64  TEST_ASSERT_NOT_NULL (publisher);
66 
68  send_string_expect_success (publisher, "This is a test", 0);
69 
70  msleep (50);
71  send_string_expect_success (publisher, "This is a test", 0);
72 
73  msleep (50);
74  send_string_expect_success (publisher, "This is a test", 0);
75 
76  test_context_socket_close (publisher);
77  test_context_socket_close (control);
79  free (my_endpoint);
80 
81  zmq_threadclose (thread);
82 }
83 
84 int main (void)
85 {
87 
88  UNITY_BEGIN ();
90  return UNITY_END ();
91 }
NULL
NULL
Definition: test_security_zap.cpp:405
UNITY_END
return UNITY_END()
ZMQ_PUB
#define ZMQ_PUB
Definition: zmq.h:259
zmq_threadstart
ZMQ_EXPORT void * zmq_threadstart(zmq_thread_fn *func_, void *arg_)
Definition: zmq_utils.cpp:54
msleep
void msleep(int milliseconds_)
Definition: testutil.cpp:227
test_proxy_terminate
void test_proxy_terminate()
Definition: test_proxy_terminate.cpp:49
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
main
int main(void)
Definition: test_proxy_terminate.cpp:84
setup_test_context
void setup_test_context()
Definition: testutil_unity.cpp:179
ZMQ_SUBSCRIBE
#define ZMQ_SUBSCRIBE
Definition: zmq.h:278
bind_loopback_ipv4
void bind_loopback_ipv4(void *socket_, char *my_endpoint_, size_t len_)
Definition: testutil_unity.cpp:246
teardown_test_context
void teardown_test_context()
Definition: testutil_unity.cpp:189
get_test_context
void * get_test_context()
Definition: testutil_unity.cpp:184
ZMQ_SUB
#define ZMQ_SUB
Definition: zmq.h:260
s_recv
char * s_recv(void *socket_)
Definition: testutil.cpp:123
ZMQ_REQ
#define ZMQ_REQ
Definition: zmq.h:261
zmq_connect
ZMQ_EXPORT int zmq_connect(void *s_, const char *addr_)
Definition: zmq.cpp:307
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_threadclose
ZMQ_EXPORT void zmq_threadclose(void *thread_)
Definition: zmq_utils.cpp:62
ZMQ_PUSH
#define ZMQ_PUSH
Definition: zmq.h:266
testutil.hpp
ZMQ_REP
#define ZMQ_REP
Definition: zmq.h:262
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
zmq_bind
ZMQ_EXPORT int zmq_bind(void *s_, const char *addr_)
Definition: zmq.cpp:299
zmq_socket
ZMQ_EXPORT void * zmq_socket(void *, int type_)
Definition: zmq.cpp:230
test_context_socket
void * test_context_socket(int type_)
Definition: testutil_unity.cpp:200
SETTLE_TIME
#define SETTLE_TIME
Definition: libzmq/tests/testutil.hpp:31
zmq_close
ZMQ_EXPORT int zmq_close(void *s_)
Definition: zmq.cpp:241
send_string_expect_success
void send_string_expect_success(void *socket_, const char *str_, int flags_)
Definition: testutil_unity.cpp:94
server_task
void server_task(void *)
Definition: test_proxy_terminate.cpp:18
zmq_proxy
ZMQ_EXPORT int zmq_proxy(void *frontend_, void *backend_, void *capture_)
Definition: zmq.cpp:1726
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
setUp
void setUp()
Definition: test_proxy_terminate.cpp:8
test_context_socket_close
void * test_context_socket_close(void *socket_)
Definition: testutil_unity.cpp:208
TEST_ASSERT_NOT_NULL
#define TEST_ASSERT_NOT_NULL(pointer)
Definition: unity.h:125
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