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