test_router_handover.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. SETUP_TEARDOWN_TESTCONTEXT
  27. void test_with_handover ()
  28. {
  29. char my_endpoint[MAX_SOCKET_STRING];
  30. void *router = test_context_socket (ZMQ_ROUTER);
  31. bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
  32. // Enable the handover flag
  33. int handover = 1;
  34. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_HANDOVER,
  35. &handover, sizeof (handover)));
  36. // Create dealer called "X" and connect it to our router
  37. void *dealer_one = test_context_socket (ZMQ_DEALER);
  38. TEST_ASSERT_SUCCESS_ERRNO (
  39. zmq_setsockopt (dealer_one, ZMQ_ROUTING_ID, "X", 1));
  40. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer_one, my_endpoint));
  41. // Get message from dealer to know when connection is ready
  42. char buffer[255];
  43. send_string_expect_success (dealer_one, "Hello", 0);
  44. recv_string_expect_success (router, "X", 0);
  45. recv_string_expect_success (router, "Hello", 0);
  46. // Now create a second dealer that uses the same routing id
  47. void *dealer_two = test_context_socket (ZMQ_DEALER);
  48. TEST_ASSERT_SUCCESS_ERRNO (
  49. zmq_setsockopt (dealer_two, ZMQ_ROUTING_ID, "X", 1));
  50. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer_two, my_endpoint));
  51. // Get message from dealer to know when connection is ready
  52. send_string_expect_success (dealer_two, "Hello", 0);
  53. recv_string_expect_success (router, "X", 0);
  54. recv_string_expect_success (router, "Hello", 0);
  55. // Send a message to 'X' routing id. This should be delivered
  56. // to the second dealer, instead of the first because of the handover.
  57. send_string_expect_success (router, "X", ZMQ_SNDMORE);
  58. send_string_expect_success (router, "Hello", 0);
  59. // Ensure that the first dealer doesn't receive the message
  60. // but the second one does
  61. const int timeout = SETTLE_TIME;
  62. TEST_ASSERT_SUCCESS_ERRNO (
  63. zmq_setsockopt (dealer_one, ZMQ_RCVTIMEO, &timeout, sizeof timeout));
  64. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (dealer_one, buffer, 255, 0));
  65. recv_string_expect_success (dealer_two, "Hello", 0);
  66. test_context_socket_close (router);
  67. test_context_socket_close (dealer_one);
  68. test_context_socket_close (dealer_two);
  69. }
  70. void test_without_handover ()
  71. {
  72. size_t len = MAX_SOCKET_STRING;
  73. char my_endpoint[MAX_SOCKET_STRING];
  74. void *router = test_context_socket (ZMQ_ROUTER);
  75. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, "tcp://127.0.0.1:*"));
  76. TEST_ASSERT_SUCCESS_ERRNO (
  77. zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
  78. // Create dealer called "X" and connect it to our router
  79. void *dealer_one = test_context_socket (ZMQ_DEALER);
  80. TEST_ASSERT_SUCCESS_ERRNO (
  81. zmq_setsockopt (dealer_one, ZMQ_ROUTING_ID, "X", 1));
  82. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer_one, my_endpoint));
  83. // Get message from dealer to know when connection is ready
  84. char buffer[255];
  85. send_string_expect_success (dealer_one, "Hello", 0);
  86. recv_string_expect_success (router, "X", 0);
  87. recv_string_expect_success (router, "Hello", 0);
  88. // Now create a second dealer that uses the same routing id
  89. void *dealer_two = test_context_socket (ZMQ_DEALER);
  90. TEST_ASSERT_SUCCESS_ERRNO (
  91. zmq_setsockopt (dealer_two, ZMQ_ROUTING_ID, "X", 1));
  92. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer_two, my_endpoint));
  93. // Send message from second dealer
  94. send_string_expect_success (dealer_two, "Hello", 0);
  95. // This should be ignored by the router
  96. const int timeout = SETTLE_TIME;
  97. TEST_ASSERT_SUCCESS_ERRNO (
  98. zmq_setsockopt (router, ZMQ_RCVTIMEO, &timeout, sizeof timeout));
  99. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (router, buffer, 255, 0));
  100. // Send a message to 'X' routing id. This should be delivered
  101. // to the second dealer, instead of the first because of the handover.
  102. send_string_expect_success (router, "X", ZMQ_SNDMORE);
  103. send_string_expect_success (router, "Hello", 0);
  104. // Ensure that the second dealer doesn't receive the message
  105. // but the first one does
  106. TEST_ASSERT_SUCCESS_ERRNO (
  107. zmq_setsockopt (dealer_two, ZMQ_RCVTIMEO, &timeout, sizeof timeout));
  108. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (dealer_two, buffer, 255, 0));
  109. recv_string_expect_success (dealer_one, "Hello", 0);
  110. test_context_socket_close (router);
  111. test_context_socket_close (dealer_one);
  112. test_context_socket_close (dealer_two);
  113. }
  114. int main ()
  115. {
  116. setup_test_environment ();
  117. UNITY_BEGIN ();
  118. RUN_TEST (test_with_handover);
  119. RUN_TEST (test_without_handover);
  120. return UNITY_END ();
  121. }