2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2019-03-22 11:09:08 -04:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-02-02 02:11:33 +00:00
|
|
|
void setUp ()
|
|
|
|
{
|
|
|
|
setup_test_context ();
|
|
|
|
}
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
// This is a test for issue #1382. The server thread creates a SUB-PUSH
|
|
|
|
// steerable proxy. The main process then sends messages to the SUB
|
|
|
|
// but there is no pull on the other side, previously the proxy blocks
|
|
|
|
// in writing to the backend, preventing the proxy from terminating
|
|
|
|
|
2019-03-22 11:09:08 -04:00
|
|
|
void server_task (void * /*unused_*/)
|
2015-04-24 23:01:20 +02:00
|
|
|
{
|
2017-05-01 12:11:11 +01:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2015-04-24 23:01:20 +02:00
|
|
|
// Frontend socket talks to main process
|
2019-03-22 11:09:08 -04:00
|
|
|
void *frontend = zmq_socket (get_test_context (), ZMQ_SUB);
|
|
|
|
TEST_ASSERT_NOT_NULL (frontend);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (frontend, ZMQ_SUBSCRIBE, "", 0));
|
|
|
|
bind_loopback_ipv4 (frontend, my_endpoint, sizeof my_endpoint);
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
// Nice socket which is never read
|
2019-03-22 11:09:08 -04:00
|
|
|
void *backend = zmq_socket (get_test_context (), ZMQ_PUSH);
|
|
|
|
TEST_ASSERT_NOT_NULL (backend);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tcp://127.0.0.1:*"));
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
// Control socket receives terminate command from main over inproc
|
2019-03-22 11:09:08 -04:00
|
|
|
void *control = zmq_socket (get_test_context (), ZMQ_REQ);
|
|
|
|
TEST_ASSERT_NOT_NULL (control);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
|
2019-03-24 13:02:50 -04:00
|
|
|
send_string_expect_success (control, my_endpoint, 0);
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
// Connect backend to frontend via a proxy
|
2023-02-02 02:11:33 +00:00
|
|
|
zmq_proxy (frontend, backend, NULL);
|
2019-03-22 11:09:08 -04:00
|
|
|
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (frontend));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (backend));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
|
2015-04-24 23:01:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// The main thread simply starts a basic steerable proxy server, publishes some messages, and then
|
|
|
|
// waits for the server to terminate.
|
2019-03-22 11:09:08 -04:00
|
|
|
void test_proxy_terminate ()
|
2015-04-24 23:01:20 +02:00
|
|
|
{
|
2019-03-22 11:09:08 -04:00
|
|
|
void *thread = zmq_threadstart (&server_task, NULL);
|
2017-05-01 12:11:11 +01:00
|
|
|
|
2015-04-24 23:01:20 +02:00
|
|
|
// Control socket receives terminate command from main over inproc
|
2019-03-22 11:09:08 -04:00
|
|
|
void *control = test_context_socket (ZMQ_REP);
|
|
|
|
TEST_ASSERT_NOT_NULL (control);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (control, "inproc://control"));
|
2017-05-01 12:11:11 +01:00
|
|
|
char *my_endpoint = s_recv (control);
|
2019-03-22 11:09:08 -04:00
|
|
|
TEST_ASSERT_NOT_NULL (my_endpoint);
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
msleep (500); // Run for 500 ms
|
|
|
|
|
|
|
|
// Start a secondary publisher which writes data to the SUB-PUSH server socket
|
2019-03-22 11:09:08 -04:00
|
|
|
void *publisher = test_context_socket (ZMQ_PUB);
|
|
|
|
TEST_ASSERT_NOT_NULL (publisher);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (publisher, my_endpoint));
|
2015-04-24 23:01:20 +02:00
|
|
|
|
2016-02-13 14:23:00 +00:00
|
|
|
msleep (SETTLE_TIME);
|
2019-03-22 11:09:08 -04:00
|
|
|
send_string_expect_success (publisher, "This is a test", 0);
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
msleep (50);
|
2019-03-22 11:09:08 -04:00
|
|
|
send_string_expect_success (publisher, "This is a test", 0);
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
msleep (50);
|
2019-03-22 11:09:08 -04:00
|
|
|
send_string_expect_success (publisher, "This is a test", 0);
|
|
|
|
|
|
|
|
test_context_socket_close (publisher);
|
|
|
|
test_context_socket_close (control);
|
2023-02-02 02:11:33 +00:00
|
|
|
teardown_test_context ();
|
2017-05-01 12:11:11 +01:00
|
|
|
free (my_endpoint);
|
2015-04-24 23:01:20 +02:00
|
|
|
|
|
|
|
zmq_threadclose (thread);
|
2019-03-22 11:09:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
2015-04-24 23:01:20 +02:00
|
|
|
|
2019-03-22 11:09:08 -04:00
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_proxy_terminate);
|
|
|
|
return UNITY_END ();
|
2015-04-24 23:01:20 +02:00
|
|
|
}
|