test_stream_timeout.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_monitoring.hpp"
  26. #include "testutil_unity.hpp"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. SETUP_TEARDOWN_TESTCONTEXT
  30. static void test_stream_handshake_timeout_accept ()
  31. {
  32. char my_endpoint[MAX_SOCKET_STRING];
  33. // We use this socket in raw mode, to make a connection and send nothing
  34. void *stream = test_context_socket (ZMQ_STREAM);
  35. int zero = 0;
  36. TEST_ASSERT_SUCCESS_ERRNO (
  37. zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)));
  38. // We'll be using this socket to test TCP stream handshake timeout
  39. void *dealer = test_context_socket (ZMQ_DEALER);
  40. TEST_ASSERT_SUCCESS_ERRNO (
  41. zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)));
  42. int val, tenth = 100;
  43. size_t vsize = sizeof (val);
  44. // check for the expected default handshake timeout value - 30 sec
  45. TEST_ASSERT_SUCCESS_ERRNO (
  46. zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
  47. TEST_ASSERT_EQUAL (sizeof (val), vsize);
  48. TEST_ASSERT_EQUAL_INT (30000, val);
  49. // make handshake timeout faster - 1/10 sec
  50. TEST_ASSERT_SUCCESS_ERRNO (
  51. zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth)));
  52. vsize = sizeof (val);
  53. // make sure zmq_setsockopt changed the value
  54. TEST_ASSERT_SUCCESS_ERRNO (
  55. zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
  56. TEST_ASSERT_EQUAL (sizeof (val), vsize);
  57. TEST_ASSERT_EQUAL_INT (tenth, val);
  58. // Create and connect a socket for collecting monitor events on dealer
  59. void *dealer_mon = test_context_socket (ZMQ_PAIR);
  60. TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
  61. dealer, "inproc://monitor-dealer",
  62. ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
  63. // Connect to the inproc endpoint so we'll get events
  64. TEST_ASSERT_SUCCESS_ERRNO (
  65. zmq_connect (dealer_mon, "inproc://monitor-dealer"));
  66. // bind dealer socket to accept connection from non-sending stream socket
  67. bind_loopback_ipv4 (dealer, my_endpoint, sizeof my_endpoint);
  68. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (stream, my_endpoint));
  69. // we should get ZMQ_EVENT_ACCEPTED and then ZMQ_EVENT_DISCONNECTED
  70. int event = get_monitor_event (dealer_mon, NULL, NULL);
  71. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, event);
  72. event = get_monitor_event (dealer_mon, NULL, NULL);
  73. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, event);
  74. test_context_socket_close (dealer);
  75. test_context_socket_close (dealer_mon);
  76. test_context_socket_close (stream);
  77. }
  78. static void test_stream_handshake_timeout_connect ()
  79. {
  80. char my_endpoint[MAX_SOCKET_STRING];
  81. // We use this socket in raw mode, to accept a connection and send nothing
  82. void *stream = test_context_socket (ZMQ_STREAM);
  83. int zero = 0;
  84. TEST_ASSERT_SUCCESS_ERRNO (
  85. zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)));
  86. bind_loopback_ipv4 (stream, my_endpoint, sizeof my_endpoint);
  87. // We'll be using this socket to test TCP stream handshake timeout
  88. void *dealer = test_context_socket (ZMQ_DEALER);
  89. TEST_ASSERT_SUCCESS_ERRNO (
  90. zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)));
  91. int val, tenth = 100;
  92. size_t vsize = sizeof (val);
  93. // check for the expected default handshake timeout value - 30 sec
  94. TEST_ASSERT_SUCCESS_ERRNO (
  95. zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
  96. TEST_ASSERT_EQUAL (sizeof (val), vsize);
  97. TEST_ASSERT_EQUAL_INT (30000, val);
  98. // make handshake timeout faster - 1/10 sec
  99. TEST_ASSERT_SUCCESS_ERRNO (
  100. zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth)));
  101. vsize = sizeof (val);
  102. // make sure zmq_setsockopt changed the value
  103. TEST_ASSERT_SUCCESS_ERRNO (
  104. zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
  105. TEST_ASSERT_EQUAL (sizeof (val), vsize);
  106. TEST_ASSERT_EQUAL_INT (tenth, val);
  107. // Create and connect a socket for collecting monitor events on dealer
  108. void *dealer_mon = test_context_socket (ZMQ_PAIR);
  109. TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
  110. dealer, "inproc://monitor-dealer",
  111. ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
  112. // Connect to the inproc endpoint so we'll get events
  113. TEST_ASSERT_SUCCESS_ERRNO (
  114. zmq_connect (dealer_mon, "inproc://monitor-dealer"));
  115. // connect dealer socket to non-sending stream socket
  116. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
  117. // we should get ZMQ_EVENT_CONNECTED and then ZMQ_EVENT_DISCONNECTED
  118. int event = get_monitor_event (dealer_mon, NULL, NULL);
  119. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_CONNECTED, event);
  120. event = get_monitor_event (dealer_mon, NULL, NULL);
  121. TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, event);
  122. test_context_socket_close (dealer);
  123. test_context_socket_close (dealer_mon);
  124. test_context_socket_close (stream);
  125. }
  126. int main ()
  127. {
  128. setup_test_environment ();
  129. UNITY_BEGIN ();
  130. RUN_TEST (test_stream_handshake_timeout_accept);
  131. RUN_TEST (test_stream_handshake_timeout_connect);
  132. return UNITY_END ();
  133. }