2012-06-17 02:33:43 +04:00
|
|
|
/*
|
2017-05-01 12:11:11 +01:00
|
|
|
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
|
2012-06-17 02:33:43 +04:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2012-06-17 02:33:43 +04:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2012-06-17 02:33:43 +04:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
2012-06-17 02:33:43 +04:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2013-08-17 13:43:45 +01:00
|
|
|
#include "testutil.hpp"
|
2018-03-13 12:00:41 +01:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-23 08:04:57 -04:00
|
|
|
#include <string.h>
|
2018-03-13 12:00:41 +01:00
|
|
|
|
2019-03-24 12:51:28 -04:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2012-06-17 02:33:43 +04:00
|
|
|
|
2017-08-30 09:38:44 +02:00
|
|
|
#ifdef ZMQ_BUILD_DRAFT_API
|
2018-05-27 07:01:36 -04:00
|
|
|
bool send_msg_to_peer_if_ready (void *router_, const char *peer_routing_id_)
|
2017-08-30 09:38:44 +02:00
|
|
|
{
|
2018-03-13 12:00:41 +01:00
|
|
|
int rc = TEST_ASSERT_SUCCESS_MESSAGE_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_socket_get_peer_state (router_, peer_routing_id_, 1),
|
|
|
|
peer_routing_id_);
|
2017-08-30 09:38:44 +02:00
|
|
|
if (rc & ZMQ_POLLOUT) {
|
2018-05-27 07:01:36 -04:00
|
|
|
send_string_expect_success (router_, peer_routing_id_,
|
2018-03-13 12:00:41 +01:00
|
|
|
ZMQ_SNDMORE | ZMQ_DONTWAIT);
|
2018-05-27 07:01:36 -04:00
|
|
|
send_string_expect_success (router_, "Hello", ZMQ_DONTWAIT);
|
2017-08-30 09:38:44 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-29 10:04:06 +02:00
|
|
|
void test_get_peer_state ()
|
|
|
|
{
|
|
|
|
#ifdef ZMQ_BUILD_DRAFT_API
|
2018-03-14 09:59:16 +01:00
|
|
|
void *router = test_context_socket (ZMQ_ROUTER);
|
2017-08-29 10:04:06 +02:00
|
|
|
|
|
|
|
int mandatory = 1;
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
|
|
|
|
&mandatory, sizeof (mandatory)));
|
2017-08-29 10:04:06 +02:00
|
|
|
|
2017-09-01 15:51:57 +02:00
|
|
|
const char *my_endpoint = "inproc://test_get_peer_state";
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, my_endpoint));
|
2017-08-29 13:00:41 +02:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
void *dealer1 = test_context_socket (ZMQ_DEALER);
|
|
|
|
void *dealer2 = test_context_socket (ZMQ_DEALER);
|
2017-08-29 13:00:41 +02:00
|
|
|
|
|
|
|
// Lower HWMs to allow doing the test with fewer messages
|
2018-03-13 12:00:41 +01:00
|
|
|
const int hwm = 100;
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (router, ZMQ_SNDHWM, &hwm, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer1, ZMQ_RCVHWM, &hwm, sizeof (int)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer2, ZMQ_RCVHWM, &hwm, sizeof (int)));
|
2017-08-30 09:38:44 +02:00
|
|
|
|
2017-09-07 11:21:13 +02:00
|
|
|
const char *dealer1_routing_id = "X";
|
|
|
|
const char *dealer2_routing_id = "Y";
|
2017-08-29 13:00:41 +02:00
|
|
|
|
|
|
|
// Name dealer1 "X" and connect it to our router
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer1, ZMQ_ROUTING_ID, dealer1_routing_id, 1));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer1, my_endpoint));
|
2017-08-29 10:04:06 +02:00
|
|
|
|
2017-08-29 13:00:41 +02:00
|
|
|
// Name dealer2 "Y" and connect it to our router
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (dealer2, ZMQ_ROUTING_ID, dealer2_routing_id, 1));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer2, my_endpoint));
|
2017-08-29 10:04:06 +02:00
|
|
|
|
2017-08-29 11:50:30 +02:00
|
|
|
// Get message from both dealers to know when connection is ready
|
2018-03-13 12:00:41 +01:00
|
|
|
send_string_expect_success (dealer1, "Hello", 0);
|
|
|
|
recv_string_expect_success (router, dealer1_routing_id, 0);
|
|
|
|
recv_string_expect_success (router, "Hello", 0);
|
|
|
|
|
|
|
|
send_string_expect_success (dealer2, "Hello", 0);
|
|
|
|
recv_string_expect_success (router, dealer2_routing_id, 0);
|
|
|
|
recv_string_expect_success (router, "Hello", 0);
|
2017-08-29 11:50:30 +02:00
|
|
|
|
2017-08-29 10:04:06 +02:00
|
|
|
void *poller = zmq_poller_new ();
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (poller);
|
2017-08-29 10:04:06 +02:00
|
|
|
|
|
|
|
// Poll on router and dealer1, but not on dealer2
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_poller_add (poller, router, NULL, ZMQ_POLLOUT));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_poller_add (poller, dealer1, NULL, ZMQ_POLLIN));
|
2017-08-29 10:04:06 +02:00
|
|
|
|
2017-10-17 13:22:29 +01:00
|
|
|
const unsigned int count = 10000;
|
|
|
|
const unsigned int event_size = 2;
|
2017-08-29 13:00:41 +02:00
|
|
|
bool dealer2_blocked = false;
|
2017-10-17 13:22:29 +01:00
|
|
|
unsigned int dealer1_sent = 0, dealer2_sent = 0, dealer1_received = 0;
|
2017-08-29 10:04:06 +02:00
|
|
|
zmq_poller_event_t events[event_size];
|
2017-10-17 13:22:29 +01:00
|
|
|
for (unsigned int iteration = 0; iteration < count; ++iteration) {
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_poller_wait_all (poller, events, event_size, -1));
|
2017-10-17 13:22:29 +01:00
|
|
|
for (unsigned int event_no = 0; event_no < event_size; ++event_no) {
|
2017-08-30 09:38:44 +02:00
|
|
|
const zmq_poller_event_t ¤t_event = events[event_no];
|
|
|
|
if (current_event.socket == router
|
|
|
|
&& current_event.events & ZMQ_POLLOUT) {
|
2017-09-07 11:21:13 +02:00
|
|
|
if (send_msg_to_peer_if_ready (router, dealer1_routing_id))
|
2017-08-29 13:00:41 +02:00
|
|
|
++dealer1_sent;
|
2017-08-30 09:38:44 +02:00
|
|
|
|
2017-09-07 11:21:13 +02:00
|
|
|
if (send_msg_to_peer_if_ready (router, dealer2_routing_id))
|
2017-08-29 13:00:41 +02:00
|
|
|
++dealer2_sent;
|
2017-08-30 09:38:44 +02:00
|
|
|
else
|
2017-08-29 13:00:41 +02:00
|
|
|
dealer2_blocked = true;
|
2017-08-29 10:04:06 +02:00
|
|
|
}
|
2017-08-30 09:38:44 +02:00
|
|
|
if (current_event.socket == dealer1
|
|
|
|
&& current_event.events & ZMQ_POLLIN) {
|
2018-03-13 12:00:41 +01:00
|
|
|
recv_string_expect_success (dealer1, "Hello", ZMQ_DONTWAIT);
|
2017-08-30 09:38:44 +02:00
|
|
|
int more;
|
|
|
|
size_t more_size = sizeof (more);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (dealer1, ZMQ_RCVMORE, &more, &more_size));
|
|
|
|
TEST_ASSERT_FALSE (more);
|
2017-08-29 13:00:41 +02:00
|
|
|
|
|
|
|
++dealer1_received;
|
2017-08-29 10:04:06 +02:00
|
|
|
}
|
|
|
|
// never read from dealer2, so its pipe becomes full eventually
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 13:22:29 +01:00
|
|
|
printf ("dealer1_sent = %u, dealer2_sent = %u, dealer1_received = %u\n",
|
2017-08-29 13:00:41 +02:00
|
|
|
dealer1_sent, dealer2_sent, dealer1_received);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_TRUE (dealer2_blocked);
|
2017-08-29 11:50:30 +02:00
|
|
|
zmq_poller_destroy (&poller);
|
2017-08-29 10:04:06 +02:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (router);
|
|
|
|
test_context_socket_close (dealer1);
|
|
|
|
test_context_socket_close (dealer2);
|
2017-08-29 10:04:06 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-01 16:27:53 +02:00
|
|
|
void test_get_peer_state_corner_cases ()
|
|
|
|
{
|
|
|
|
#ifdef ZMQ_BUILD_DRAFT_API
|
2017-09-07 11:21:13 +02:00
|
|
|
const char peer_routing_id[] = "foo";
|
2017-09-01 16:27:53 +02:00
|
|
|
|
|
|
|
// call get_peer_state with NULL socket
|
2018-02-01 11:46:09 +01:00
|
|
|
int rc = zmq_socket_get_peer_state (NULL, peer_routing_id,
|
|
|
|
strlen (peer_routing_id));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (-1, rc);
|
|
|
|
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno);
|
2017-09-01 16:27:53 +02:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
void *dealer = test_context_socket (ZMQ_DEALER);
|
|
|
|
void *router = test_context_socket (ZMQ_ROUTER);
|
2017-09-01 16:27:53 +02:00
|
|
|
|
|
|
|
// call get_peer_state with a non-ROUTER socket
|
2018-02-01 11:46:09 +01:00
|
|
|
rc = zmq_socket_get_peer_state (dealer, peer_routing_id,
|
|
|
|
strlen (peer_routing_id));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (-1, rc);
|
|
|
|
TEST_ASSERT_EQUAL_INT (ENOTSUP, errno);
|
2017-09-01 16:27:53 +02:00
|
|
|
|
2017-09-07 11:21:13 +02:00
|
|
|
// call get_peer_state for an unknown routing id
|
2018-02-01 11:46:09 +01:00
|
|
|
rc = zmq_socket_get_peer_state (router, peer_routing_id,
|
|
|
|
strlen (peer_routing_id));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (-1, rc);
|
|
|
|
TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
|
2017-09-01 16:27:53 +02:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (router);
|
|
|
|
test_context_socket_close (dealer);
|
2017-09-01 16:27:53 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:03:17 +02:00
|
|
|
void test_basic ()
|
2012-06-17 02:33:43 +04:00
|
|
|
{
|
2017-05-01 12:11:11 +01:00
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2018-03-14 09:59:16 +01:00
|
|
|
void *router = test_context_socket (ZMQ_ROUTER);
|
2019-03-24 13:17:22 -04:00
|
|
|
bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
|
2012-12-08 10:20:42 +09:00
|
|
|
|
2013-01-31 16:26:28 +01:00
|
|
|
// Send a message to an unknown peer with the default setting
|
|
|
|
// This will not report any error
|
2018-03-13 12:00:41 +01:00
|
|
|
send_string_expect_success (router, "UNKNOWN", ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (router, "DATA", 0);
|
2012-06-17 02:33:43 +04:00
|
|
|
|
2013-01-31 16:26:28 +01:00
|
|
|
// Send a message to an unknown peer with mandatory routing
|
|
|
|
// This will fail
|
2012-10-08 16:36:35 +09:00
|
|
|
int mandatory = 1;
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
|
|
|
|
&mandatory, sizeof (mandatory)));
|
|
|
|
int rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
|
|
|
|
TEST_ASSERT_EQUAL_INT (-1, rc);
|
|
|
|
TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
|
2012-06-17 02:33:43 +04:00
|
|
|
|
2013-01-31 16:26:28 +01:00
|
|
|
// Create dealer called "X" and connect it to our router
|
2018-03-14 09:59:16 +01:00
|
|
|
void *dealer = test_context_socket (ZMQ_DEALER);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
|
2012-12-08 10:20:42 +09:00
|
|
|
|
2013-01-31 16:26:28 +01:00
|
|
|
// Get message from dealer to know when connection is ready
|
2018-03-13 12:00:41 +01:00
|
|
|
send_string_expect_success (dealer, "Hello", 0);
|
|
|
|
recv_string_expect_success (router, "X", 0);
|
2012-12-08 10:20:42 +09:00
|
|
|
|
2013-01-31 16:26:28 +01:00
|
|
|
// Send a message to connected dealer now
|
|
|
|
// It should work
|
2018-03-13 12:00:41 +01:00
|
|
|
send_string_expect_success (router, "X", ZMQ_SNDMORE);
|
|
|
|
send_string_expect_success (router, "Hello", 0);
|
2012-12-08 10:20:42 +09:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (router);
|
|
|
|
test_context_socket_close (dealer);
|
2017-08-29 10:03:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_basic);
|
|
|
|
RUN_TEST (test_get_peer_state);
|
|
|
|
RUN_TEST (test_get_peer_state_corner_cases);
|
2012-06-17 02:33:43 +04:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
return UNITY_END ();
|
2012-06-17 02:33:43 +04:00
|
|
|
}
|