2016-02-12 15:46:55 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
|
|
|
|
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
|
|
|
|
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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 17:51:28 +01:00
|
|
|
SETUP_TEARDOWN_TESTCONTEXT
|
2018-02-11 12:29:06 +01:00
|
|
|
|
2015-06-05 18:14:55 +02:00
|
|
|
const int MAX_SENDS = 10000;
|
|
|
|
|
|
|
|
void test_change_before_connected ()
|
2015-06-04 01:38:45 +02:00
|
|
|
{
|
2015-06-05 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02:00
|
|
|
zmq_connect (connect_socket, "inproc://a");
|
|
|
|
zmq_bind (bind_socket, "inproc://a");
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2015-06-05 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (bind_socket);
|
|
|
|
test_context_socket_close (connect_socket);
|
2015-06-05 18:14:55 +02:00
|
|
|
}
|
2015-06-04 01:38:45 +02:00
|
|
|
|
2015-06-05 18:14:55 +02:00
|
|
|
void test_change_after_connected ()
|
2015-06-04 01:38:45 +02:00
|
|
|
{
|
2015-06-05 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02:00
|
|
|
zmq_connect (connect_socket, "inproc://a");
|
|
|
|
zmq_bind (bind_socket, "inproc://a");
|
2015-06-05 11:43:40 +02:00
|
|
|
|
2015-06-05 18:14:55 +02: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 11:43:40 +02:00
|
|
|
|
2015-06-05 18:14:55 +02: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 11:43:40 +02:00
|
|
|
|
2015-06-05 18:14:55 +02:00
|
|
|
int send_count = 0;
|
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
|
|
|
++send_count;
|
2015-06-05 11:43:40 +02:00
|
|
|
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (6, send_count);
|
2015-06-05 11:43:40 +02:00
|
|
|
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (bind_socket);
|
|
|
|
test_context_socket_close (connect_socket);
|
2015-06-05 18:14:55 +02:00
|
|
|
}
|
2015-06-05 11:43:40 +02:00
|
|
|
|
2018-05-27 13:01:36 +02: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 13:01:36 +02: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 13:01:36 +02:00
|
|
|
int test_fill_up_to_hwm (void *socket_, int sndhwm_)
|
2017-08-20 11:51:09 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02: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 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02:00
|
|
|
|
|
|
|
zmq_bind (bind_socket, "inproc://a");
|
|
|
|
zmq_connect (connect_socket, "inproc://a");
|
|
|
|
|
2019-03-01 12:30:44 +01: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 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02: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 18:14:55 +02:00
|
|
|
++read_count;
|
|
|
|
}
|
|
|
|
|
2018-02-11 12:29:06 +01:00
|
|
|
TEST_ASSERT_EQUAL_INT (send_count, read_count);
|
2015-06-05 18:14:55 +02:00
|
|
|
|
|
|
|
// Give io thread some time to catch up
|
2016-02-13 15:23:00 +01:00
|
|
|
msleep (SETTLE_TIME);
|
2015-06-05 18:14:55 +02: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 18:14:55 +02: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 11:43:40 +02: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
|
|
|
}
|