2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
#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 12:51:28 -04: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 07:01:36 -04: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 07:01:36 -04: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 14:23:00 +00: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 07:01:36 -04: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 07:01:36 -04: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 14:23:00 +00: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 07:01:36 -04:00
|
|
|
void test_destroy_queue_on_disconnect (const char *bind_address_)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
2018-05-27 07:01:36 -04: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 07:01:36 -04:00
|
|
|
test_bind (a, bind_address_, connect_address, sizeof (connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
void *b = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 07:01:36 -04: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 07:01:36 -04: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 07:01:36 -04: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 07:01:36 -04: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 07:01:36 -04:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (a, 0, 0, ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 07:01:36 -04: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 07:01:36 -04:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-27 07:01:36 -04: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 07:01:36 -04: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 07:01:36 -04:00
|
|
|
void test_block_on_send_no_peers (const char *bind_address_)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
2023-10-04 01:51:43 -07:00
|
|
|
LIBZMQ_UNUSED (bind_address_);
|
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); \
|
|
|
|
} \
|
2023-01-12 00:42:09 +00:00
|
|
|
void test_fair_queue_in_##name () \
|
|
|
|
{ \
|
|
|
|
test_fair_queue_in (bind_address); \
|
|
|
|
} \
|
2018-05-15 12:20:19 +02:00
|
|
|
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 13:43:45 +01: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
|
|
|
}
|