2023-06-05 00:16:05 +01:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2016-02-12 15:46:55 +01:00
|
|
|
|
2015-06-04 01:38:45 +02:00
|
|
|
#include "testutil.hpp"
|
2018-03-14 09:59:16 +01:00
|
|
|
#include "testutil_unity.hpp"
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2019-03-24 12:51:28 -04:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2018-02-11 12:29:06 +01:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
const int MAX_SENDS = 10000;
|
|
|
|
|
|
|
|
void test_change_before_connected ()
|
2015-06-04 01:38:45 +02:00
|
|
|
{
|
2015-06-05 17:14:55 +01:00
|
|
|
int rc;
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
void *bind_socket = test_context_socket (ZMQ_PUSH);
|
|
|
|
void *connect_socket = test_context_socket (ZMQ_PULL);
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
int val = 2;
|
|
|
|
rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
2015-06-05 17:14:55 +01:00
|
|
|
rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
zmq_connect (connect_socket, "inproc://a");
|
|
|
|
zmq_bind (bind_socket, "inproc://a");
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
size_t placeholder = sizeof (val);
|
|
|
|
val = 0;
|
|
|
|
rc = zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &val, &placeholder);
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
TEST_ASSERT_EQUAL_INT (2, val);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
|
|
|
int send_count = 0;
|
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++send_count;
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (4, send_count);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (bind_socket);
|
|
|
|
test_context_socket_close (connect_socket);
|
2015-06-05 17:14:55 +01:00
|
|
|
}
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
void test_change_after_connected ()
|
2015-06-04 01:38:45 +02:00
|
|
|
{
|
2015-06-05 17:14:55 +01:00
|
|
|
int rc;
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
void *bind_socket = test_context_socket (ZMQ_PUSH);
|
|
|
|
void *connect_socket = test_context_socket (ZMQ_PULL);
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
int val = 1;
|
|
|
|
rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
2015-06-05 17:14:55 +01:00
|
|
|
rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
zmq_connect (connect_socket, "inproc://a");
|
|
|
|
zmq_bind (bind_socket, "inproc://a");
|
2015-06-05 10:43:40 +01:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
val = 5;
|
|
|
|
rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
2015-06-05 10:43:40 +01:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
size_t placeholder = sizeof (val);
|
|
|
|
val = 0;
|
|
|
|
rc = zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &val, &placeholder);
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
TEST_ASSERT_EQUAL_INT (5, val);
|
2015-06-05 10:43:40 +01:00
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
int send_count = 0;
|
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++send_count;
|
2015-06-05 10:43:40 +01:00
|
|
|
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (6, send_count);
|
2015-06-05 10:43:40 +01:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (bind_socket);
|
|
|
|
test_context_socket_close (connect_socket);
|
2015-06-05 17:14:55 +01:00
|
|
|
}
|
2015-06-05 10:43:40 +01:00
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int send_until_wouldblock (void *socket_)
|
2017-08-18 17:29:23 +02:00
|
|
|
{
|
|
|
|
int send_count = 0;
|
|
|
|
while (send_count < MAX_SENDS
|
2018-05-27 07:01:36 -04:00
|
|
|
&& zmq_send (socket_, &send_count, sizeof (send_count), ZMQ_DONTWAIT)
|
2017-08-18 17:29:23 +02:00
|
|
|
== sizeof (send_count)) {
|
|
|
|
++send_count;
|
|
|
|
}
|
|
|
|
return send_count;
|
|
|
|
}
|
|
|
|
|
2018-05-27 07:01:36 -04:00
|
|
|
int test_fill_up_to_hwm (void *socket_, int sndhwm_)
|
2017-08-20 11:51:09 +02:00
|
|
|
{
|
2018-05-27 07:01:36 -04:00
|
|
|
int send_count = send_until_wouldblock (socket_);
|
|
|
|
fprintf (stderr, "sndhwm==%i, send_count==%i\n", sndhwm_, send_count);
|
|
|
|
TEST_ASSERT_LESS_OR_EQUAL_INT (sndhwm_ + 1, send_count);
|
|
|
|
TEST_ASSERT_GREATER_THAN_INT (sndhwm_ / 10, send_count);
|
2017-08-20 11:51:09 +02:00
|
|
|
return send_count;
|
|
|
|
}
|
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
void test_decrease_when_full ()
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
void *bind_socket = test_context_socket (ZMQ_PUSH);
|
|
|
|
void *connect_socket = test_context_socket (ZMQ_PULL);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
|
|
|
int val = 1;
|
|
|
|
rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
2017-08-18 17:29:23 +02:00
|
|
|
int sndhwm = 100;
|
|
|
|
rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
|
|
|
zmq_bind (bind_socket, "inproc://a");
|
|
|
|
zmq_connect (connect_socket, "inproc://a");
|
|
|
|
|
2019-03-01 06:30:44 -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);
|
|
|
|
|
2015-06-05 17:14:55 +01:00
|
|
|
// Fill up to hwm
|
2017-08-20 11:51:09 +02:00
|
|
|
int send_count = test_fill_up_to_hwm (bind_socket, sndhwm);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
2017-08-18 17:29:23 +02:00
|
|
|
// Decrease snd hwm
|
|
|
|
sndhwm = 70;
|
|
|
|
rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
2017-08-18 17:29:23 +02:00
|
|
|
int sndhwm_read = 0;
|
|
|
|
size_t sndhwm_read_size = sizeof (sndhwm_read);
|
|
|
|
rc =
|
|
|
|
zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm_read, &sndhwm_read_size);
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (0, rc);
|
|
|
|
TEST_ASSERT_EQUAL_INT (sndhwm, sndhwm_read);
|
2017-08-18 17:29:23 +02:00
|
|
|
|
|
|
|
msleep (SETTLE_TIME);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
|
|
|
// Read out all data (should get up to previous hwm worth so none were dropped)
|
|
|
|
int read_count = 0;
|
|
|
|
int read_data = 0;
|
2017-08-18 17:29:23 +02:00
|
|
|
while (
|
|
|
|
read_count < MAX_SENDS
|
|
|
|
&& zmq_recv (connect_socket, &read_data, sizeof (read_data), ZMQ_DONTWAIT)
|
|
|
|
== sizeof (read_data)) {
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (read_data, read_count);
|
2015-06-05 17:14:55 +01:00
|
|
|
++read_count;
|
|
|
|
}
|
|
|
|
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (send_count, read_count);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
|
|
|
// Give io thread some time to catch up
|
2016-02-13 14:23:00 +00:00
|
|
|
msleep (SETTLE_TIME);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
|
|
|
// Fill up to new hwm
|
2017-08-20 11:51:09 +02:00
|
|
|
test_fill_up_to_hwm (bind_socket, sndhwm);
|
2015-06-05 17:14:55 +01:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (bind_socket);
|
|
|
|
test_context_socket_close (connect_socket);
|
2015-06-04 01:38:45 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 10:43:40 +01:00
|
|
|
|
2015-06-04 01:38:45 +02:00
|
|
|
int main ()
|
|
|
|
{
|
2018-02-11 12:29:06 +01:00
|
|
|
setup_test_environment ();
|
|
|
|
|
|
|
|
UNITY_BEGIN ();
|
|
|
|
RUN_TEST (test_change_before_connected);
|
|
|
|
RUN_TEST (test_change_after_connected);
|
|
|
|
RUN_TEST (test_decrease_when_full);
|
|
|
|
|
|
|
|
return UNITY_END ();
|
2015-06-04 01:38:45 +02:00
|
|
|
}
|