2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2011-01-30 11:51:30 +01:00
|
|
|
|
2013-08-17 13:43:45 +01:00
|
|
|
#include "testutil.hpp"
|
2018-03-13 12:00:41 +01:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
2019-03-24 12:51:28 -04:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2011-01-30 11:51:30 +01:00
|
|
|
|
2013-09-14 17:27:18 +01:00
|
|
|
const int MAX_SENDS = 10000;
|
|
|
|
|
|
|
|
enum TestType
|
|
|
|
{
|
|
|
|
BIND_FIRST,
|
|
|
|
CONNECT_FIRST
|
|
|
|
};
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_defaults ()
|
2013-09-15 13:35:44 +01:00
|
|
|
{
|
|
|
|
// Set up bind socket
|
2018-03-14 09:59:16 +01:00
|
|
|
void *bind_socket = test_context_socket (ZMQ_PULL);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Set up connect socket
|
2018-03-14 09:59:16 +01:00
|
|
|
void *connect_socket = test_context_socket (ZMQ_PUSH);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Send until we block
|
|
|
|
int send_count = 0;
|
2017-08-07 09:59:49 +02:00
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
2013-09-15 13:35:44 +01:00
|
|
|
++send_count;
|
|
|
|
|
2017-08-07 09:59:49 +02:00
|
|
|
msleep (SETTLE_TIME);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2013-09-15 13:35:44 +01:00
|
|
|
// Now receive all sent messages
|
|
|
|
int recv_count = 0;
|
|
|
|
while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++recv_count;
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Clean up
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (connect_socket);
|
|
|
|
test_context_socket_close (bind_socket);
|
2013-09-15 13:35:44 +01:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
// Default values are 1000 on send and 1000 one receive, so 2000 total
|
|
|
|
TEST_ASSERT_EQUAL_INT (2000, send_count);
|
2013-09-15 13:35:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int count_msg (int send_hwm_, int recv_hwm_, TestType test_type_)
|
2011-01-30 11:51:30 +01:00
|
|
|
{
|
2013-09-14 17:27:18 +01:00
|
|
|
void *bind_socket;
|
|
|
|
void *connect_socket;
|
2018-05-27 07:01:36 -04:00
|
|
|
if (test_type_ == BIND_FIRST) {
|
2013-09-14 17:27:18 +01:00
|
|
|
// Set up bind socket
|
2018-03-14 09:59:16 +01:00
|
|
|
bind_socket = test_context_socket (ZMQ_PULL);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
|
2013-09-14 17:27:18 +01:00
|
|
|
|
|
|
|
// Set up connect socket
|
2018-03-14 09:59:16 +01:00
|
|
|
connect_socket = test_context_socket (ZMQ_PUSH);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
2018-05-27 07:01:36 -04:00
|
|
|
connect_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
|
2019-03-01 16:57:29 -05:00
|
|
|
|
|
|
|
// we must wait for the connect to succeed here, unfortunately we don't
|
|
|
|
// have monitoring events for inproc, so we just hope SETTLE_TIME suffices
|
|
|
|
msleep (SETTLE_TIME);
|
2013-09-14 17:27:18 +01:00
|
|
|
} else {
|
|
|
|
// Set up connect socket
|
2018-03-14 09:59:16 +01:00
|
|
|
connect_socket = test_context_socket (ZMQ_PUSH);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
2018-05-27 07:01:36 -04:00
|
|
|
connect_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
|
2011-01-30 11:51:30 +01:00
|
|
|
|
2013-09-14 17:27:18 +01:00
|
|
|
// Set up bind socket
|
2018-03-14 09:59:16 +01:00
|
|
|
bind_socket = test_context_socket (ZMQ_PULL);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
|
2018-05-27 07:01:36 -04:00
|
|
|
bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
|
2011-03-24 10:03:49 +01:00
|
|
|
}
|
2011-01-30 11:51:30 +01:00
|
|
|
|
2013-09-14 17:27:18 +01:00
|
|
|
// Send until we block
|
|
|
|
int send_count = 0;
|
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++send_count;
|
|
|
|
|
|
|
|
// Now receive all sent messages
|
|
|
|
int recv_count = 0;
|
|
|
|
while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++recv_count;
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
|
2013-09-14 17:27:18 +01:00
|
|
|
|
2011-03-24 10:03:49 +01:00
|
|
|
// Now it should be possible to send one more.
|
2018-03-13 12:00:41 +01:00
|
|
|
send_string_expect_success (connect_socket, NULL, 0);
|
2011-03-24 10:03:49 +01:00
|
|
|
|
|
|
|
// Consume the remaining message.
|
2018-03-13 12:00:41 +01:00
|
|
|
recv_string_expect_success (bind_socket, NULL, 0);
|
2011-03-24 10:03:49 +01:00
|
|
|
|
2013-09-14 17:27:18 +01:00
|
|
|
// Clean up
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (connect_socket);
|
|
|
|
test_context_socket_close (bind_socket);
|
2011-01-30 11:51:30 +01:00
|
|
|
|
2013-09-14 17:27:18 +01:00
|
|
|
return send_count;
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int test_inproc_bind_first (int send_hwm_, int recv_hwm_)
|
2013-09-14 17:27:18 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
return count_msg (send_hwm_, recv_hwm_, BIND_FIRST);
|
2013-09-14 17:27:18 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int test_inproc_connect_first (int send_hwm_, int recv_hwm_)
|
2013-09-14 17:27:18 +01:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
return count_msg (send_hwm_, recv_hwm_, CONNECT_FIRST);
|
2013-09-14 17:27:18 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int test_inproc_connect_and_close_first (int send_hwm_, int recv_hwm_)
|
2013-09-15 13:35:44 +01:00
|
|
|
{
|
|
|
|
// Set up connect socket
|
2018-03-14 09:59:16 +01:00
|
|
|
void *connect_socket = test_context_socket (ZMQ_PUSH);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_SNDHWM,
|
2018-05-27 07:01:36 -04:00
|
|
|
&send_hwm_, sizeof (send_hwm_)));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Send until we block
|
|
|
|
int send_count = 0;
|
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++send_count;
|
|
|
|
|
|
|
|
// Close connect
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (connect_socket);
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Set up bind socket
|
2018-03-14 09:59:16 +01:00
|
|
|
void *bind_socket = test_context_socket (ZMQ_PULL);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Now receive all sent messages
|
|
|
|
int recv_count = 0;
|
|
|
|
while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++recv_count;
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Clean up
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (bind_socket);
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
return send_count;
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */)
|
2013-09-15 13:35:44 +01:00
|
|
|
{
|
|
|
|
// Set up bind socket
|
2019-03-22 06:52:54 -04:00
|
|
|
void *bind_socket = test_context_socket (ZMQ_PUSH);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 07:01:36 -04:00
|
|
|
zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Send until we block
|
|
|
|
int send_count = 0;
|
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++send_count;
|
|
|
|
|
|
|
|
// Close bind
|
2019-03-22 06:52:54 -04:00
|
|
|
test_context_socket_close (bind_socket);
|
2013-09-15 13:35:44 +01:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
/* TODO Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
|
2013-09-15 13:35:44 +01:00
|
|
|
// Set up connect socket
|
2019-03-22 06:52:54 -04:00
|
|
|
void *connect_socket = test_context_socket (ZMQ_PULL);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
// Now receive all sent messages
|
|
|
|
int recv_count = 0;
|
|
|
|
while (zmq_recv (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++recv_count;
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT(send_count, recv_count);
|
2013-09-15 13:35:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Clean up
|
2019-03-22 06:52:54 -04:00
|
|
|
//test_context_socket_close (connect_socket);
|
2013-09-15 13:35:44 +01:00
|
|
|
|
|
|
|
return send_count;
|
|
|
|
}
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_infinite_both_inproc_bind_first ()
|
2013-09-14 17:27:18 +01:00
|
|
|
{
|
2018-03-13 12:00:41 +01:00
|
|
|
int count = test_inproc_bind_first (0, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
|
|
|
|
}
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_infinite_both_inproc_connect_first ()
|
|
|
|
{
|
|
|
|
int count = test_inproc_connect_first (0, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
|
|
|
|
}
|
2013-09-14 17:27:18 +01:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_infinite_receive_inproc_bind_first ()
|
|
|
|
{
|
|
|
|
int count = test_inproc_bind_first (1, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_infinite_receive_inproc_connect_first ()
|
|
|
|
{
|
|
|
|
int count = test_inproc_connect_first (1, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_infinite_send_inproc_bind_first ()
|
|
|
|
{
|
|
|
|
int count = test_inproc_bind_first (0, 1);
|
|
|
|
TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_infinite_send_inproc_connect_first ()
|
|
|
|
{
|
|
|
|
int count = test_inproc_connect_first (0, 1);
|
|
|
|
TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
|
|
|
|
}
|
2013-09-14 17:27:18 +01:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_finite_both_bind_first ()
|
|
|
|
{
|
|
|
|
// Send and recv buffers hwm 1, so total that can be queued is 2
|
|
|
|
int count = test_inproc_bind_first (1, 1);
|
|
|
|
TEST_ASSERT_EQUAL_INT (2, count);
|
|
|
|
}
|
|
|
|
void test_finite_both_connect_first ()
|
|
|
|
{
|
2013-09-15 13:35:44 +01:00
|
|
|
// Send and recv buffers hwm 1, so total that can be queued is 2
|
2018-03-13 12:00:41 +01:00
|
|
|
int count = test_inproc_connect_first (1, 1);
|
|
|
|
TEST_ASSERT_EQUAL_INT (2, count);
|
|
|
|
}
|
2013-09-14 17:27:18 +01:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_infinite_recv_connect_and_close_first ()
|
|
|
|
{
|
2013-09-15 13:35:44 +01:00
|
|
|
// Send hwm of 1, send before bind so total that can be queued is 1
|
2018-03-13 12:00:41 +01:00
|
|
|
int count = test_inproc_connect_and_close_first (1, 0);
|
|
|
|
TEST_ASSERT_EQUAL_INT (1, count);
|
|
|
|
}
|
2013-09-15 13:35:44 +01:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_infinite_recv_bind_and_close_first ()
|
|
|
|
{
|
2013-09-15 13:35:44 +01:00
|
|
|
// Send hwm of 1, send from bind side before connect so total that can be queued should be 1,
|
|
|
|
// however currently all messages get thrown away before the connect. BUG?
|
2018-03-13 12:00:41 +01:00
|
|
|
/*int count = */ test_inproc_bind_and_close_first (1, 0);
|
|
|
|
// TEST_ASSERT_EQUAL_INT (1, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (void)
|
|
|
|
{
|
|
|
|
setup_test_environment ();
|
|
|
|
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_defaults);
|
|
|
|
|
|
|
|
RUN_TEST (test_infinite_both_inproc_bind_first);
|
|
|
|
RUN_TEST (test_infinite_both_inproc_connect_first);
|
|
|
|
|
|
|
|
RUN_TEST (test_infinite_receive_inproc_bind_first);
|
|
|
|
RUN_TEST (test_infinite_receive_inproc_connect_first);
|
|
|
|
|
|
|
|
RUN_TEST (test_infinite_send_inproc_bind_first);
|
|
|
|
RUN_TEST (test_infinite_send_inproc_connect_first);
|
|
|
|
|
|
|
|
RUN_TEST (test_finite_both_bind_first);
|
|
|
|
RUN_TEST (test_finite_both_connect_first);
|
|
|
|
|
|
|
|
RUN_TEST (test_infinite_recv_connect_and_close_first);
|
|
|
|
RUN_TEST (test_infinite_recv_bind_and_close_first);
|
2013-09-15 13:35:44 +01:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
return UNITY_END ();
|
2011-01-30 11:51:30 +01:00
|
|
|
}
|