2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
#include "testutil.hpp"
|
2018-08-17 10:18:32 +02:00
|
|
|
#include "testutil_unity.hpp"
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2019-03-23 13:04:57 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-08-17 10:18:32 +02:00
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2018-08-17 10:18:32 +02:00
|
|
|
|
|
|
|
// SHALL receive incoming messages from its peers using a fair-queuing
|
|
|
|
// strategy.
|
2019-12-08 14:21:43 +01:00
|
|
|
void test_fair_queue_in (const char *bind_address_)
|
2018-08-17 10:18:32 +02:00
|
|
|
{
|
|
|
|
char connect_address[MAX_SOCKET_STRING];
|
|
|
|
void *receiver = test_context_socket (ZMQ_ROUTER);
|
2019-12-08 14:21:43 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (receiver, bind_address_));
|
2017-05-01 13:11:11 +02:00
|
|
|
size_t len = MAX_SOCKET_STRING;
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (receiver, ZMQ_LAST_ENDPOINT, connect_address, &len));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-05-14 20:54:36 +02:00
|
|
|
const unsigned char services = 5;
|
2013-07-14 08:32:35 +02:00
|
|
|
void *senders[services];
|
2018-05-14 20:54:36 +02:00
|
|
|
for (unsigned char peer = 0; peer < services; ++peer) {
|
2018-08-17 10:18:32 +02:00
|
|
|
senders[peer] = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
char *str = strdup ("A");
|
2013-07-14 08:32:35 +02:00
|
|
|
str[0] += peer;
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (senders[peer], ZMQ_ROUTING_ID, str, 2));
|
2013-07-02 15:04:31 +02:00
|
|
|
free (str);
|
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_connect (senders[peer], connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2016-02-13 15:25:57 +01:00
|
|
|
msleep (SETTLE_TIME);
|
|
|
|
|
2013-07-02 15:04:31 +02:00
|
|
|
zmq_msg_t msg;
|
2018-08-17 10:18:32 +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], "M", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
s_recv_seq (receiver, "A", "M", SEQ_END);
|
|
|
|
|
2013-07-14 08:32:35 +02:00
|
|
|
s_send_seq (senders[0], "M", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
s_recv_seq (receiver, "A", "M", SEQ_END);
|
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
int sum = 0;
|
|
|
|
|
2013-07-02 15:04:31 +02:00
|
|
|
// send N requests
|
2018-05-14 20:54:36 +02:00
|
|
|
for (unsigned char peer = 0; peer < services; ++peer) {
|
2013-07-14 08:32:35 +02:00
|
|
|
s_send_seq (senders[peer], "M", SEQ_END);
|
|
|
|
sum += 'A' + peer;
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (services * 'A' + services * (services - 1) / 2, sum);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2013-07-02 15:04:31 +02:00
|
|
|
// handle N requests
|
2018-05-14 20:54:36 +02:00
|
|
|
for (unsigned char peer = 0; peer < services; ++peer) {
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (
|
|
|
|
2, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, receiver, 0)));
|
|
|
|
const char *id = static_cast<const char *> (zmq_msg_data (&msg));
|
2013-07-14 08:32:35 +02:00
|
|
|
sum -= id[0];
|
2013-07-05 17:58:01 +02:00
|
|
|
|
|
|
|
s_recv_seq (receiver, "M", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, sum);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +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-08-17 10:18:32 +02:00
|
|
|
test_context_socket_close_zero_linger (senders[peer]);
|
2013-07-05 17:58:01 +02:00
|
|
|
|
|
|
|
// Wait for disconnects.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
// SHALL create a double queue when a peer connects to it. If this peer
|
|
|
|
// disconnects, the ROUTER socket SHALL destroy its double queue and SHALL
|
|
|
|
// discard any messages it contains.
|
2019-12-08 14:21:43 +01:00
|
|
|
void test_destroy_queue_on_disconnect (const char *bind_address_)
|
2013-07-02 15:04:31 +02:00
|
|
|
{
|
2018-08-17 10:18:32 +02:00
|
|
|
void *a = test_context_socket (ZMQ_ROUTER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
|
|
|
int enabled = 1;
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (a, ZMQ_ROUTER_MANDATORY, &enabled, sizeof (enabled)));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2019-12-08 14:21:43 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (a, bind_address_));
|
2017-05-01 13:11:11 +02:00
|
|
|
size_t len = MAX_SOCKET_STRING;
|
2018-08-17 10:18:32 +02:00
|
|
|
char connect_address[MAX_SOCKET_STRING];
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_getsockopt (a, ZMQ_LAST_ENDPOINT, connect_address, &len));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
void *b = test_context_socket (ZMQ_DEALER);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (b, ZMQ_ROUTING_ID, "B", 2));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
|
2013-07-05 17:58:01 +02:00
|
|
|
|
|
|
|
// Wait for connection.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
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, "B", "ABC", SEQ_END);
|
|
|
|
s_send_seq (b, "DEF", SEQ_END);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +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-08-17 10:18:32 +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-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (
|
|
|
|
EHOSTUNREACH, zmq_send (a, "B", 2, ZMQ_SNDMORE | ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +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-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, b, ZMQ_DONTWAIT));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
test_context_socket_close_zero_linger (a);
|
|
|
|
test_context_socket_close_zero_linger (b);
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2013-07-05 17:58:01 +02:00
|
|
|
// Wait for disconnects.
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
#define TEST_SUITE(name, bind_address) \
|
2023-01-12 01:42:09 +01:00
|
|
|
void test_fair_queue_in_##name () \
|
|
|
|
{ \
|
|
|
|
test_fair_queue_in (bind_address); \
|
|
|
|
} \
|
2018-08-17 10:18:32 +02:00
|
|
|
void test_destroy_queue_on_disconnect_##name () \
|
|
|
|
{ \
|
|
|
|
test_destroy_queue_on_disconnect (bind_address); \
|
|
|
|
}
|
2013-07-02 15:04:31 +02:00
|
|
|
|
2018-08-17 10:18:32 +02:00
|
|
|
TEST_SUITE (inproc, "inproc://a")
|
|
|
|
TEST_SUITE (tcp, "tcp://127.0.0.1:*")
|
|
|
|
|
|
|
|
int main ()
|
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-08-17 10:18:32 +02:00
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_fair_queue_in_tcp);
|
|
|
|
RUN_TEST (test_fair_queue_in_inproc);
|
|
|
|
// TODO commented out until libzmq implements this properly
|
|
|
|
// RUN_TEST (test_destroy_queue_on_disconnect_tcp);
|
|
|
|
// RUN_TEST (test_destroy_queue_on_disconnect_inproc);
|
|
|
|
return UNITY_END ();
|
2013-07-02 15:04:31 +02:00
|
|
|
}
|