2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2013-08-17 14:43:45 +02:00
|
|
|
#include "testutil.hpp"
|
2018-03-16 15:46:39 +01:00
|
|
|
#include "testutil_unity.hpp"
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2019-03-24 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2018-03-16 15:46:39 +01:00
|
|
|
|
|
|
|
void test_immediate_1 ()
|
2012-06-12 16:31:23 +02:00
|
|
|
{
|
|
|
|
int val;
|
|
|
|
int rc;
|
|
|
|
char buffer[16];
|
2017-05-01 13:11:11 +02:00
|
|
|
size_t len = MAX_SOCKET_STRING;
|
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
2012-12-01 00:07:12 +01:00
|
|
|
// 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-02-01 11:46:09 +01:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
void *to = test_context_socket (ZMQ_PULL);
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// Bind the one valid receiver
|
2012-06-12 16:31:23 +02:00
|
|
|
val = 0;
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
|
|
|
|
bind_loopback_ipv4 (to, my_endpoint, len);
|
2012-06-12 16:31:23 +02:00
|
|
|
|
|
|
|
// Create a socket pushing to two endpoints - only 1 message should arrive.
|
2018-03-16 15:46:39 +01:00
|
|
|
void *from = test_context_socket (ZMQ_PUSH);
|
2012-06-12 16:31:23 +02:00
|
|
|
|
|
|
|
val = 0;
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
|
|
|
|
// This pipe will not connect (provided the ephemeral port is not 5556)
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tcp://localhost:5556"));
|
2012-12-01 00:07:12 +01:00
|
|
|
// This pipe will
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, my_endpoint));
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2016-02-13 15:25:57 +01:00
|
|
|
msleep (SETTLE_TIME);
|
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// We send 10 messages, 5 should just get stuck in the queue
|
|
|
|
// for the not-yet-connected pipe
|
2013-01-31 19:46:22 +01:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2018-03-16 15:46:39 +01:00
|
|
|
send_string_expect_success (from, "Hello", 0);
|
2012-06-12 16:31:23 +02:00
|
|
|
}
|
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// We now consume from the connected pipe
|
|
|
|
// - we should see just 5
|
2015-01-30 11:57:31 +01:00
|
|
|
int timeout = 250;
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
|
2013-01-31 19:46:22 +01:00
|
|
|
|
|
|
|
int seen = 0;
|
|
|
|
while (true) {
|
|
|
|
rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
|
|
|
|
if (rc == -1)
|
|
|
|
break; // Break when we didn't get a message
|
2012-06-12 16:31:23 +02:00
|
|
|
seen++;
|
|
|
|
}
|
2021-09-22 13:38:34 +02:00
|
|
|
// TODO: this fails ~1% of the runs on OBS but it does not seem to be reproducible anywhere else
|
|
|
|
if (seen == 0)
|
|
|
|
TEST_IGNORE_MESSAGE (
|
|
|
|
"Unreliable test occasionally fails on slow CIs, ignoring");
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (5, seen);
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
test_context_socket_close (from);
|
|
|
|
test_context_socket_close (to);
|
|
|
|
}
|
2012-06-12 16:31:23 +02:00
|
|
|
|
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
void test_immediate_2 ()
|
|
|
|
{
|
2012-12-01 00:07:12 +01:00
|
|
|
// 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.
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// Bind the valid socket
|
2018-03-16 15:46:39 +01:00
|
|
|
void *to = test_context_socket (ZMQ_PULL);
|
|
|
|
size_t len = MAX_SOCKET_STRING;
|
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
|
|
|
bind_loopback_ipv4 (to, my_endpoint, len);
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
int val = 0;
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
|
2012-06-12 16:31:23 +02:00
|
|
|
|
|
|
|
// Create a socket pushing to two endpoints - all messages should arrive.
|
2018-03-16 15:46:39 +01:00
|
|
|
void *from = test_context_socket (ZMQ_PUSH);
|
2012-06-12 16:31:23 +02:00
|
|
|
|
|
|
|
val = 0;
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// Set the key flag
|
2012-06-12 16:31:23 +02:00
|
|
|
val = 1;
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (from, ZMQ_IMMEDIATE, &val, sizeof (val)));
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// Connect to the invalid socket
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tcp://localhost:5561"));
|
2012-12-01 00:07:12 +01:00
|
|
|
// Connect to the valid socket
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, my_endpoint));
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// Send 10 messages, all should be routed to the connected pipe
|
2013-01-31 19:46:22 +01:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2018-03-16 15:46:39 +01:00
|
|
|
send_string_expect_success (from, "Hello", 0);
|
2012-06-12 16:31:23 +02:00
|
|
|
}
|
2018-03-16 15:46:39 +01:00
|
|
|
int timeout = 250;
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
int seen = 0;
|
2013-01-31 19:46:22 +01:00
|
|
|
while (true) {
|
2018-03-16 15:46:39 +01:00
|
|
|
char buffer[16];
|
|
|
|
int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
|
2013-01-31 19:46:22 +01:00
|
|
|
if (rc == -1)
|
|
|
|
break; // Break when we didn't get a message
|
|
|
|
seen++;
|
2012-06-12 16:31:23 +02:00
|
|
|
}
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (10, seen);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
test_context_socket_close (from);
|
|
|
|
test_context_socket_close (to);
|
|
|
|
}
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
void test_immediate_3 ()
|
|
|
|
{
|
2012-12-01 00:07:12 +01:00
|
|
|
// This time we want to validate that the same blocking behaviour
|
|
|
|
// occurs with an existing connection that is broken. We will send
|
2013-01-31 19:46:22 +01:00
|
|
|
// messages to a connected pipe, disconnect and verify the messages
|
2012-12-01 00:07:12 +01:00
|
|
|
// block. Then we reconnect and verify messages flow again.
|
2018-03-16 15:46:39 +01:00
|
|
|
void *backend = test_context_socket (ZMQ_DEALER);
|
|
|
|
void *frontend = test_context_socket (ZMQ_DEALER);
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
int zero = 0;
|
2018-03-16 15:46:39 +01: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)));
|
2012-12-01 00:07:12 +01:00
|
|
|
|
2013-09-20 14:23:43 +02:00
|
|
|
// Frontend connects to backend using IMMEDIATE
|
2013-01-31 19:46:22 +01:00
|
|
|
int on = 1;
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
|
|
|
zmq_setsockopt (frontend, ZMQ_IMMEDIATE, &on, sizeof (on)));
|
|
|
|
|
|
|
|
size_t len = MAX_SOCKET_STRING;
|
|
|
|
char my_endpoint[MAX_SOCKET_STRING];
|
|
|
|
bind_loopback_ipv4 (backend, my_endpoint, len);
|
|
|
|
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (frontend, my_endpoint));
|
2013-01-31 19:46:22 +01:00
|
|
|
|
|
|
|
// Ping backend to frontend so we know when the connection is up
|
2018-03-16 15:46:39 +01:00
|
|
|
send_string_expect_success (backend, "Hello", 0);
|
|
|
|
recv_string_expect_success (frontend, "Hello", 0);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Send message from frontend to backend
|
2018-03-16 15:46:39 +01:00
|
|
|
send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
test_context_socket_close (backend);
|
2013-08-17 14:43:45 +02:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Give time to process disconnect
|
2013-12-20 14:34:29 +01:00
|
|
|
msleep (SETTLE_TIME * 10);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// Send a message, should fail
|
2018-03-16 15:46:39 +01:00
|
|
|
TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
|
|
|
|
zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT));
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2013-01-31 19:46:22 +01:00
|
|
|
// Recreate backend socket
|
2018-03-16 15:46:39 +01: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, my_endpoint));
|
2013-01-31 19:46:22 +01:00
|
|
|
|
|
|
|
// Ping backend to frontend so we know when the connection is up
|
2018-03-16 15:46:39 +01:00
|
|
|
send_string_expect_success (backend, "Hello", 0);
|
|
|
|
recv_string_expect_success (frontend, "Hello", 0);
|
2013-01-31 19:46:22 +01:00
|
|
|
|
2012-12-01 00:07:12 +01:00
|
|
|
// After the reconnect, should succeed
|
2018-03-16 15:46:39 +01:00
|
|
|
send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
test_context_socket_close (backend);
|
|
|
|
test_context_socket_close (frontend);
|
|
|
|
}
|
2012-06-12 16:31:23 +02:00
|
|
|
|
2018-03-16 15:46:39 +01:00
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_immediate_1);
|
|
|
|
RUN_TEST (test_immediate_2);
|
|
|
|
RUN_TEST (test_immediate_3);
|
|
|
|
return UNITY_END ();
|
2012-06-13 08:47:18 +02:00
|
|
|
}
|