2013-07-02 15:04:31 +02:00
|
|
|
/*
|
2017-05-01 13:11:11 +02:00
|
|
|
Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2013-07-02 15:04:31 +02: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
|
2013-07-02 15:04:31 +02: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.
|
2013-07-02 15:04:31 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2018-05-15 12:20:19 +02:00
|
|
|
#include "testutil_unity.hpp"
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2018-05-15 12:20:19 +02:00
|
|
|
|
|
|
|
// SHALL route outgoing messages to available peers using a round-robin
|
|
|
|
// strategy.
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_round_robin_out (const char *bind_address_)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
2018-05-15 12:20:19 +02:00
|
|
|
void *dealer = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
char connect_address[MAX_SOCKET_STRING];
|
2018-05-27 13:01:36 +02:00
|
|
|
test_bind (dealer, bind_address_, connect_address,
|
|
|
|
sizeof (connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
const size_t services = 5;
|
|
|
|
void *rep[services];
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer) {
|
2018-05-15 12:20:19 +02:00
|
|
|
rep[peer] = test_context_socket (ZMQ_REP);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rep[peer], connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// Wait for connections.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
// Send all requests
|
|
|
|
for (size_t i = 0; i < services; ++i)
|
2013-07-02 15:04:31 +02:00
|
|
|
s_send_seq (dealer, 0, "ABC", SEQ_END);
|
|
|
|
|
|
|
|
// Expect every REP got one message
|
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init (&msg);
|
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
|
|
|
s_recv_seq (rep[peer], "ABC", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
test_context_socket_close_zero_linger (dealer);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
2018-05-15 12:20:19 +02:00
|
|
|
test_context_socket_close_zero_linger (rep[peer]);
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
// SHALL receive incoming messages from its peers using a fair-queuing
|
|
|
|
// strategy.
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_fair_queue_in (const char *bind_address_)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
2018-05-15 12:20:19 +02:00
|
|
|
void *receiver = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
char connect_address[MAX_SOCKET_STRING];
|
2018-05-27 13:01:36 +02:00
|
|
|
test_bind (receiver, bind_address_, connect_address,
|
2018-05-15 12:20:19 +02:00
|
|
|
sizeof (connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
const size_t services = 5;
|
|
|
|
void *senders[services];
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer) {
|
2018-05-15 12:20:19 +02:00
|
|
|
senders[peer] = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_connect (senders[peer], connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq_msg_t msg;
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
s_send_seq (senders[0], "A", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
s_recv_seq (receiver, "A", SEQ_END);
|
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
s_send_seq (senders[0], "A", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
s_recv_seq (receiver, "A", SEQ_END);
|
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
// send our requests
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
|
|
|
s_send_seq (senders[peer], "B", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// Wait for data.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
// handle the requests
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
2013-07-05 17:58:01 +02:00
|
|
|
s_recv_seq (receiver, "B", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
test_context_socket_close_zero_linger (receiver);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
for (size_t peer = 0; peer < services; ++peer)
|
2018-05-15 12:20:19 +02:00
|
|
|
test_context_socket_close_zero_linger (senders[peer]);
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
// SHALL create a double queue when a peer connects to it. If this peer
|
|
|
|
// disconnects, the DEALER socket SHALL destroy its double queue and SHALL
|
|
|
|
// discard any messages it contains.
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_destroy_queue_on_disconnect (const char *bind_address_)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
void *a = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
char connect_address[MAX_SOCKET_STRING];
|
2018-05-27 13:01:36 +02:00
|
|
|
test_bind (a, bind_address_, connect_address, sizeof (connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
void *b = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
// Send a message in both directions
|
2018-05-27 13:01:36 +02:00
|
|
|
s_send_seq (a, "ABC", SEQ_END);
|
|
|
|
s_send_seq (b, "DEF", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (b, connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
// Disconnect may take time and need command processing.
|
2018-05-27 13:01:36 +02:00
|
|
|
zmq_pollitem_t poller[2] = {{a, 0, 0, 0}, {b, 0, 0, 0}};
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (poller, 2, 100));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (poller, 2, 100));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
// No messages should be available, sending should fail.
|
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init (&msg);
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (a, 0, 0, ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
// After a reconnect of B, the messages should still be gone
|
2018-05-27 13:01:36 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, b, ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
test_context_socket_close_zero_linger (a);
|
|
|
|
test_context_socket_close_zero_linger (b);
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
// SHALL block on sending, or return a suitable error, when it has no connected peers.
|
2018-05-27 13:01:36 +02:00
|
|
|
void test_block_on_send_no_peers (const char *bind_address_)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
2018-05-15 12:20:19 +02:00
|
|
|
void *sc = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2015-01-30 11:57:31 +01:00
|
|
|
int timeout = 250;
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (timeout)));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (sc, 0, 0, ZMQ_DONTWAIT));
|
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (sc, 0, 0, 0));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
test_context_socket_close (sc);
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
#define TEST_CASES(name, bind_address) \
|
|
|
|
void test_round_robin_out_##name () \
|
|
|
|
{ \
|
|
|
|
test_round_robin_out (bind_address); \
|
|
|
|
} \
|
|
|
|
void test_fair_queue_in_##name () { test_fair_queue_in (bind_address); } \
|
|
|
|
void test_block_on_send_no_peers_##name () \
|
|
|
|
{ \
|
|
|
|
test_block_on_send_no_peers (bind_address); \
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASES (inproc, "inproc://a")
|
|
|
|
TEST_CASES (tcp, "tcp://127.0.0.1:*")
|
|
|
|
|
2013-07-07 12:49:24 +02:00
|
|
|
int main (void)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
2013-08-17 14:43:45 +02:00
|
|
|
setup_test_environment ();
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
UNITY_BEGIN ();
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
RUN_TEST (test_round_robin_out_inproc);
|
|
|
|
RUN_TEST (test_fair_queue_in_inproc);
|
|
|
|
RUN_TEST (test_block_on_send_no_peers_inproc);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
RUN_TEST (test_round_robin_out_tcp);
|
|
|
|
RUN_TEST (test_fair_queue_in_tcp);
|
|
|
|
RUN_TEST (test_block_on_send_no_peers_tcp);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
// TODO *** Test disabled until libzmq does this properly ***
|
|
|
|
// test_destroy_queue_on_disconnect (ctx);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-15 12:20:19 +02:00
|
|
|
return UNITY_END ();
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|