test_proxy_terminate.cpp 4.3 KB

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