1/*
2 Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "testutil.hpp"
31#include "testutil_unity.hpp"
32
33#include <stdlib.h>
34
35SETUP_TEARDOWN_TESTCONTEXT
36
37// This is a test for issue #1382. The server thread creates a SUB-PUSH
38// steerable proxy. The main process then sends messages to the SUB
39// but there is no pull on the other side, previously the proxy blocks
40// in writing to the backend, preventing the proxy from terminating
41
42void server_task (void * /*unused_*/)
43{
44 char my_endpoint[MAX_SOCKET_STRING];
45 // Frontend socket talks to main process
46 void *frontend = zmq_socket (get_test_context (), ZMQ_SUB);
47 TEST_ASSERT_NOT_NULL (frontend);
48 TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (frontend, ZMQ_SUBSCRIBE, "", 0));
49 bind_loopback_ipv4 (frontend, my_endpoint, sizeof my_endpoint);
50
51 // Nice socket which is never read
52 void *backend = zmq_socket (get_test_context (), ZMQ_PUSH);
53 TEST_ASSERT_NOT_NULL (backend);
54 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tcp://127.0.0.1:*"));
55
56 // Control socket receives terminate command from main over inproc
57 void *control = zmq_socket (get_test_context (), ZMQ_REQ);
58 TEST_ASSERT_NOT_NULL (control);
59 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
60 send_string_expect_success (control, my_endpoint, 0);
61
62 // Connect backend to frontend via a proxy
63 TEST_ASSERT_SUCCESS_ERRNO (
64 zmq_proxy_steerable (frontend, backend, NULL, control));
65
66 TEST_ASSERT_SUCCESS_ERRNO (zmq_close (frontend));
67 TEST_ASSERT_SUCCESS_ERRNO (zmq_close (backend));
68 TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
69}
70
71
72// The main thread simply starts a basic steerable proxy server, publishes some messages, and then
73// waits for the server to terminate.
74void test_proxy_terminate ()
75{
76 void *thread = zmq_threadstart (&server_task, NULL);
77
78 // Control socket receives terminate command from main over inproc
79 void *control = test_context_socket (ZMQ_REP);
80 TEST_ASSERT_NOT_NULL (control);
81 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (control, "inproc://control"));
82 char *my_endpoint = s_recv (control);
83 TEST_ASSERT_NOT_NULL (my_endpoint);
84
85 msleep (500); // Run for 500 ms
86
87 // Start a secondary publisher which writes data to the SUB-PUSH server socket
88 void *publisher = test_context_socket (ZMQ_PUB);
89 TEST_ASSERT_NOT_NULL (publisher);
90 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (publisher, my_endpoint));
91
92 msleep (SETTLE_TIME);
93 send_string_expect_success (publisher, "This is a test", 0);
94
95 msleep (50);
96 send_string_expect_success (publisher, "This is a test", 0);
97
98 msleep (50);
99 send_string_expect_success (publisher, "This is a test", 0);
100 send_string_expect_success (control, "TERMINATE", 0);
101
102 test_context_socket_close (publisher);
103 test_context_socket_close (control);
104 free (my_endpoint);
105
106 zmq_threadclose (thread);
107}
108
109int main (void)
110{
111 setup_test_environment ();
112
113 UNITY_BEGIN ();
114 RUN_TEST (test_proxy_terminate);
115 return UNITY_END ();
116}
117