2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2013-10-30 16:03:27 +01:00
|
|
|
|
2014-07-24 03:00:21 +02:00
|
|
|
#include "testutil.hpp"
|
2018-08-15 18:35:17 +02:00
|
|
|
#include "testutil_unity.hpp"
|
2018-09-29 23:26:30 +02:00
|
|
|
#include "testutil_security.hpp"
|
2014-07-24 03:00:21 +02:00
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2018-03-09 17:47:42 +01:00
|
|
|
|
2018-08-15 18:35:17 +02:00
|
|
|
void test_send_one_connected_one_unconnected ()
|
|
|
|
{
|
2013-10-30 16:03:27 +01:00
|
|
|
int val;
|
|
|
|
// TEST 1.
|
|
|
|
// First we're going to attempt to send messages to two
|
|
|
|
// pipes, one connected, the other not. We should see
|
|
|
|
// the PUSH load balancing to both pipes, and hence half
|
|
|
|
// of the messages getting queued, as connect() creates a
|
|
|
|
// pipe immediately.
|
|
|
|
|
2018-08-16 11:55:07 +02:00
|
|
|
void *to = test_context_socket (ZMQ_PULL);
|
2018-09-29 23:26:30 +02:00
|
|
|
int timeout = 5000;
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_LINGER, &timeout, sizeof (timeout)));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Bind the one valid receiver
|
|
|
|
val = 0;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (to, "tipc://{6555,0,0}"));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Create a socket pushing to two endpoints - only 1 message should arrive.
|
2018-08-16 11:55:07 +02:00
|
|
|
void *from = test_context_socket (ZMQ_PUSH);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
val = 0;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
|
2018-09-29 23:26:30 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (from, ZMQ_LINGER, &timeout, sizeof (timeout)));
|
2013-10-30 16:03:27 +01:00
|
|
|
// This pipe will not connect
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5556,0}@0.0.0"));
|
2013-10-30 16:03:27 +01:00
|
|
|
// This pipe will
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{6555,0}@0.0.0"));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// We send 10 messages, 5 should just get stuck in the queue
|
|
|
|
// for the not-yet-connected pipe
|
2018-08-16 11:55:07 +02:00
|
|
|
const int send_count = 10;
|
|
|
|
for (int i = 0; i < send_count; ++i) {
|
|
|
|
send_string_expect_success (from, "Hello", 0);
|
2013-10-30 16:03:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We now consume from the connected pipe
|
|
|
|
// - we should see just 5
|
2018-09-29 23:26:30 +02:00
|
|
|
timeout = SETTLE_TIME;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
int seen = 0;
|
|
|
|
while (true) {
|
2018-08-16 11:55:07 +02:00
|
|
|
char buffer[16];
|
|
|
|
int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
|
|
|
|
if (rc == -1) {
|
|
|
|
TEST_ASSERT_EQUAL_INT (EAGAIN, zmq_errno ());
|
2013-10-30 16:03:27 +01:00
|
|
|
break; // Break when we didn't get a message
|
2018-08-16 11:55:07 +02:00
|
|
|
}
|
2013-10-30 16:03:27 +01:00
|
|
|
seen++;
|
|
|
|
}
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (send_count / 2, seen);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
2018-08-16 11:55:07 +02:00
|
|
|
test_context_socket_close (from);
|
|
|
|
test_context_socket_close (to);
|
2018-08-15 18:35:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_send_one_connected_one_unconnected_with_delay ()
|
|
|
|
{
|
|
|
|
int val;
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// TEST 2
|
|
|
|
// This time we will do the same thing, connect two pipes,
|
|
|
|
// one of which will succeed in connecting to a bound
|
|
|
|
// receiver, the other of which will fail. However, we will
|
|
|
|
// also set the delay attach on connect flag, which should
|
|
|
|
// cause the pipe attachment to be delayed until the connection
|
|
|
|
// succeeds.
|
|
|
|
|
|
|
|
// Bind the valid socket
|
2018-08-16 11:55:07 +02:00
|
|
|
void *to = test_context_socket (ZMQ_PULL);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (to, "tipc://{5560,0,0}"));
|
2018-09-29 23:26:30 +02:00
|
|
|
int timeout = 5000;
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_LINGER, &timeout, sizeof (timeout)));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
val = 0;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Create a socket pushing to two endpoints - all messages should arrive.
|
2018-08-16 11:55:07 +02:00
|
|
|
void *from = test_context_socket (ZMQ_PUSH);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
val = 0;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
|
2018-09-29 23:26:30 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (from, ZMQ_LINGER, &timeout, sizeof (timeout)));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Set the key flag
|
|
|
|
val = 1;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (from, ZMQ_DELAY_ATTACH_ON_CONNECT, &val, sizeof (val)));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Connect to the invalid socket
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5561,0}@0.0.0"));
|
2013-10-30 16:03:27 +01:00
|
|
|
// Connect to the valid socket
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5560,0}@0.0.0"));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Send 10 messages, all should be routed to the connected pipe
|
2018-08-16 11:55:07 +02:00
|
|
|
const int send_count = 10;
|
|
|
|
for (int i = 0; i < send_count; ++i) {
|
|
|
|
send_string_expect_success (from, "Hello", 0);
|
2013-10-30 16:03:27 +01:00
|
|
|
}
|
2018-09-29 23:26:30 +02:00
|
|
|
timeout = SETTLE_TIME;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
2018-08-15 18:35:17 +02:00
|
|
|
int seen = 0;
|
2013-10-30 16:03:27 +01:00
|
|
|
while (true) {
|
2018-08-16 11:55:07 +02:00
|
|
|
char buffer[16];
|
|
|
|
int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
|
|
|
|
if (rc == -1) {
|
|
|
|
TEST_ASSERT_EQUAL_INT (EAGAIN, zmq_errno ());
|
2013-10-30 16:03:27 +01:00
|
|
|
break; // Break when we didn't get a message
|
2018-08-16 11:55:07 +02:00
|
|
|
}
|
2013-10-30 16:03:27 +01:00
|
|
|
seen++;
|
|
|
|
}
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_EQUAL_INT (send_count, seen);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
2018-08-16 11:55:07 +02:00
|
|
|
test_context_socket_close (from);
|
|
|
|
test_context_socket_close (to);
|
2018-08-15 18:35:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_send_disconnected_with_delay ()
|
|
|
|
{
|
2013-10-30 16:03:27 +01:00
|
|
|
// TEST 3
|
|
|
|
// This time we want to validate that the same blocking behaviour
|
|
|
|
// occurs with an existing connection that is broken. We will send
|
|
|
|
// messages to a connected pipe, disconnect and verify the messages
|
|
|
|
// block. Then we reconnect and verify messages flow again.
|
2018-08-16 11:55:07 +02:00
|
|
|
void *backend = test_context_socket (ZMQ_DEALER);
|
|
|
|
void *frontend = test_context_socket (ZMQ_DEALER);
|
2018-09-29 23:26:30 +02:00
|
|
|
void *monitor = test_context_socket (ZMQ_PAIR);
|
|
|
|
int rc;
|
2013-10-30 16:03:27 +01:00
|
|
|
int zero = 0;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero)));
|
2018-09-29 23:26:30 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (frontend, "inproc://monitor",
|
|
|
|
ZMQ_EVENT_DISCONNECTED));
|
|
|
|
int timeout = 5000;
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (backend, ZMQ_LINGER, &timeout, sizeof (timeout)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (frontend, ZMQ_LINGER, &timeout, sizeof (timeout)));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Frontend connects to backend using DELAY_ATTACH_ON_CONNECT
|
|
|
|
int on = 1;
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (frontend, ZMQ_DELAY_ATTACH_ON_CONNECT, &on, sizeof (on)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tipc://{5560,0,0}"));
|
2018-09-29 23:26:30 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (monitor, "inproc://monitor"));
|
2018-08-16 11:55:07 +02:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (frontend, "tipc://{5560,0}@0.0.0"));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Ping backend to frontend so we know when the connection is up
|
2018-08-16 11:55:07 +02:00
|
|
|
send_string_expect_success (backend, "Hello", 0);
|
|
|
|
recv_string_expect_success (frontend, "Hello", 0);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Send message from frontend to backend
|
2018-08-16 11:55:07 +02:00
|
|
|
send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
2018-08-16 11:55:07 +02:00
|
|
|
test_context_socket_close (backend);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
2018-09-29 23:26:30 +02:00
|
|
|
// Wait for disconnect to happen
|
|
|
|
expect_monitor_event (monitor, ZMQ_EVENT_DISCONNECTED);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
2018-09-29 23:26:30 +02:00
|
|
|
// Send a message, might succeed depending on scheduling of the I/O thread
|
|
|
|
do {
|
|
|
|
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
|
|
|
|
TEST_ASSERT_TRUE (rc == 5 || (rc == -1 && zmq_errno () == EAGAIN));
|
|
|
|
} while (rc == 5);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Recreate backend socket
|
2018-08-16 11:55:07 +02:00
|
|
|
backend = test_context_socket (ZMQ_DEALER);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tipc://{5560,0,0}"));
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// Ping backend to frontend so we know when the connection is up
|
2018-08-16 11:55:07 +02:00
|
|
|
send_string_expect_success (backend, "Hello", 0);
|
|
|
|
recv_string_expect_success (frontend, "Hello", 0);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
|
|
|
// After the reconnect, should succeed
|
2018-08-16 11:55:07 +02:00
|
|
|
send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
|
2013-10-30 16:03:27 +01:00
|
|
|
|
2018-09-29 23:26:30 +02:00
|
|
|
test_context_socket_close (monitor);
|
2018-08-16 11:55:07 +02:00
|
|
|
test_context_socket_close (backend);
|
|
|
|
test_context_socket_close (frontend);
|
2013-10-30 16:03:27 +01:00
|
|
|
}
|
2018-08-15 18:35:17 +02:00
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
if (!is_tipc_available ()) {
|
|
|
|
printf ("TIPC environment unavailable, skipping test\n");
|
|
|
|
return 77;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_send_one_connected_one_unconnected);
|
|
|
|
RUN_TEST (test_send_one_connected_one_unconnected_with_delay);
|
|
|
|
RUN_TEST (test_send_disconnected_with_delay);
|
|
|
|
return UNITY_END ();
|
|
|
|
}
|