2011-01-30 11:51:30 +01:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2011-01-30 11:51:30 +01:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2011-01-30 11:51:30 +01:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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
|
2011-01-30 11:51:30 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
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.
|
2011-01-30 11:51:30 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2013-08-17 14:43:45 +02:00
|
|
|
#include "testutil.hpp"
|
2018-03-13 12:00:41 +01:00
|
|
|
#include "testutil_unity.hpp"
|
|
|
|
|
|
|
|
#include <unity.h>
|
|
|
|
|
|
|
|
void setUp ()
|
|
|
|
{
|
2018-03-14 09:59:16 +01:00
|
|
|
setup_test_context ();
|
2018-03-13 12:00:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown ()
|
|
|
|
{
|
2018-03-14 09:59:16 +01:00
|
|
|
teardown_test_context ();
|
2018-03-13 12:00:41 +01:00
|
|
|
}
|
2011-01-30 11:51:30 +01:00
|
|
|
|
2013-09-14 18:27:18 +02:00
|
|
|
const int MAX_SENDS = 10000;
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
enum TestType
|
|
|
|
{
|
|
|
|
BIND_FIRST,
|
|
|
|
CONNECT_FIRST
|
|
|
|
};
|
2013-09-14 18:27:18 +02:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_defaults ()
|
2013-09-15 14:35:44 +02: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 14:35:44 +02: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 14:35:44 +02: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 14:35:44 +02: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 14:35:44 +02: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 14:35:44 +02: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 14:35:44 +02: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 14:35:44 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
int count_msg (int send_hwm_, int recv_hwm_, TestType test_type_)
|
2011-01-30 11:51:30 +01:00
|
|
|
{
|
2013-09-14 18:27:18 +02:00
|
|
|
void *bind_socket;
|
|
|
|
void *connect_socket;
|
2018-05-27 13:01:36 +02:00
|
|
|
if (test_type_ == BIND_FIRST) {
|
2013-09-14 18:27:18 +02: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 13:01:36 +02: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 18:27:18 +02: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 13:01:36 +02: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 22:57:29 +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);
|
2018-02-01 11:46:09 +01:00
|
|
|
} else {
|
2013-09-14 18:27:18 +02: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 13:01:36 +02: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 18:27:18 +02: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 13:01:36 +02: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 18:27:18 +02:00
|
|
|
// Send until we block
|
|
|
|
int send_count = 0;
|
2018-02-01 11:46:09 +01:00
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
2013-09-14 18:27:18 +02:00
|
|
|
++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 18:27:18 +02: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 18:27:18 +02: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 18:27:18 +02:00
|
|
|
return send_count;
|
|
|
|
}
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
int test_inproc_bind_first (int send_hwm_, int recv_hwm_)
|
2013-09-14 18:27:18 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
return count_msg (send_hwm_, recv_hwm_, BIND_FIRST);
|
2013-09-14 18:27:18 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
int test_inproc_connect_first (int send_hwm_, int recv_hwm_)
|
2013-09-14 18:27:18 +02:00
|
|
|
{
|
2018-05-27 13:01:36 +02:00
|
|
|
return count_msg (send_hwm_, recv_hwm_, CONNECT_FIRST);
|
2013-09-14 18:27:18 +02:00
|
|
|
}
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
int test_inproc_connect_and_close_first (int send_hwm_, int recv_hwm_)
|
2013-09-15 14:35:44 +02: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 13:01:36 +02: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 14:35:44 +02:00
|
|
|
|
|
|
|
// Send until we block
|
|
|
|
int send_count = 0;
|
2018-02-01 11:46:09 +01:00
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
2013-09-15 14:35:44 +02:00
|
|
|
++send_count;
|
|
|
|
|
|
|
|
// Close connect
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (connect_socket);
|
2013-09-15 14:35:44 +02: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 13:01:36 +02: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 14:35:44 +02: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 14:35:44 +02:00
|
|
|
|
|
|
|
// Clean up
|
2018-03-14 09:59:16 +01:00
|
|
|
test_context_socket_close (bind_socket);
|
2013-09-15 14:35:44 +02:00
|
|
|
|
|
|
|
return send_count;
|
|
|
|
}
|
|
|
|
|
2018-05-27 13:01:36 +02:00
|
|
|
int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */)
|
2013-09-15 14:35:44 +02:00
|
|
|
{
|
|
|
|
void *ctx = zmq_ctx_new ();
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (ctx);
|
2013-09-15 14:35:44 +02:00
|
|
|
|
|
|
|
// Set up bind socket
|
|
|
|
void *bind_socket = zmq_socket (ctx, ZMQ_PUSH);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_NOT_NULL (bind_socket);
|
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (
|
2018-05-27 13:01:36 +02: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 14:35:44 +02:00
|
|
|
|
|
|
|
// Send until we block
|
|
|
|
int send_count = 0;
|
2018-02-01 11:46:09 +01:00
|
|
|
while (send_count < MAX_SENDS
|
|
|
|
&& zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
|
2013-09-15 14:35:44 +02:00
|
|
|
++send_count;
|
|
|
|
|
|
|
|
// Close bind
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (bind_socket));
|
2013-09-15 14:35:44 +02: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 14:35:44 +02:00
|
|
|
// Set up connect socket
|
|
|
|
void *connect_socket = zmq_socket (ctx, ZMQ_PULL);
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_NOT_NULL(connect_socket);
|
|
|
|
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 14:35:44 +02: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 14:35:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Clean up
|
2018-03-13 12:00:41 +01:00
|
|
|
//TEST_ASSERT_SUCCESS_ERRNO (zmq_close (connect_socket));
|
2013-09-15 14:35:44 +02:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
|
2013-09-15 14:35:44 +02:00
|
|
|
|
|
|
|
return send_count;
|
|
|
|
}
|
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_infinite_both_inproc_bind_first ()
|
2013-09-14 18:27:18 +02: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 18:27:18 +02: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 18:27:18 +02: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 14:35:44 +02: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 18:27:18 +02:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_infinite_recv_connect_and_close_first ()
|
|
|
|
{
|
2013-09-15 14:35:44 +02: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 14:35:44 +02:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
void test_infinite_recv_bind_and_close_first ()
|
|
|
|
{
|
2013-09-15 14:35:44 +02: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 14:35:44 +02:00
|
|
|
|
2018-03-13 12:00:41 +01:00
|
|
|
return UNITY_END ();
|
2011-01-30 11:51:30 +01:00
|
|
|
}
|