Unverified Commit e0fe7f10 authored by Constantin Rack's avatar Constantin Rack Committed by GitHub

Merge pull request #3232 from sigiesec/migrate-tests-to-unity

Migrate tests to unity, reduce duplication
parents 76f2edd0 430b97fc
......@@ -533,7 +533,8 @@ tests_test_router_mandatory_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_router_mandatory_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_router_mandatory_hwm_SOURCES = tests/test_router_mandatory_hwm.cpp
tests_test_router_mandatory_hwm_LDADD = src/libzmq.la
tests_test_router_mandatory_hwm_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_router_mandatory_hwm_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_router_handover_SOURCES = tests/test_router_handover.cpp
tests_test_router_handover_LDADD = src/libzmq.la ${UNITY_LIBS}
......@@ -552,10 +553,12 @@ tests_test_stream_empty_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_stream_empty_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_stream_timeout_SOURCES = tests/test_stream_timeout.cpp
tests_test_stream_timeout_LDADD = src/libzmq.la
tests_test_stream_timeout_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_stream_timeout_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_stream_disconnect_SOURCES = tests/test_stream_disconnect.cpp
tests_test_stream_disconnect_LDADD = src/libzmq.la
tests_test_stream_disconnect_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_stream_disconnect_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_disconnect_inproc_SOURCES = tests/test_disconnect_inproc.cpp
tests_test_disconnect_inproc_LDADD = src/libzmq.la
......@@ -623,7 +626,8 @@ tests_test_issue_566_SOURCES = tests/test_issue_566.cpp
tests_test_issue_566_LDADD = src/libzmq.la
tests_test_proxy_SOURCES = tests/test_proxy.cpp
tests_test_proxy_LDADD = src/libzmq.la
tests_test_proxy_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_proxy_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_proxy_single_socket_SOURCES = tests/test_proxy_single_socket.cpp
tests_test_proxy_single_socket_LDADD = src/libzmq.la
......@@ -760,8 +764,7 @@ test_apps += \
tests/test_pair_ipc \
tests/test_rebind_ipc \
tests/test_reqrep_ipc \
tests/test_use_fd_ipc \
tests/test_use_fd_tcp \
tests/test_use_fd \
tests/test_zmq_poll_fd \
tests/test_timeo \
tests/test_filter_ipc
......@@ -792,15 +795,11 @@ tests_test_filter_ipc_SOURCES = tests/test_filter_ipc.cpp
tests_test_filter_ipc_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_filter_ipc_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_use_fd_ipc_SOURCES = \
tests/test_use_fd_ipc.cpp \
tests_test_use_fd_SOURCES = \
tests/test_use_fd.cpp \
tests/testutil.hpp
tests_test_use_fd_ipc_LDADD = src/libzmq.la
tests_test_use_fd_tcp_SOURCES = \
tests/test_use_fd_tcp.cpp \
tests/testutil.hpp
tests_test_use_fd_tcp_LDADD = src/libzmq.la
tests_test_use_fd_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_use_fd_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_zmq_poll_fd_SOURCES = tests/test_zmq_poll_fd.cpp
tests_test_zmq_poll_fd_LDADD = src/libzmq.la
......@@ -916,7 +915,8 @@ tests_test_thread_safe_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_thread_safe_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_timers_SOURCES = tests/test_timers.cpp
tests_test_timers_LDADD = src/libzmq.la
tests_test_timers_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_timers_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_radio_dish_SOURCES = tests/test_radio_dish.cpp
tests_test_radio_dish_LDADD = src/libzmq.la ${UNITY_LIBS}
......
......@@ -90,8 +90,7 @@ if(NOT WIN32)
test_filter_ipc
test_stream_exceeds_buffer
test_router_mandatory_hwm
test_use_fd_ipc
test_use_fd_tcp
test_use_fd
test_zmq_poll_fd
)
if(HAVE_FORK)
......
This diff is collapsed.
......@@ -28,6 +28,17 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// DEBUG shouldn't be defined in sources as it will cause a redefined symbol
// error when it is defined in the build configuration. It appears that the
......@@ -36,101 +47,82 @@
//#define DEBUG 0
#define TRACE_ENABLED 0
int main (void)
void test_router_mandatory_hwm ()
{
int rc;
if (TRACE_ENABLED)
fprintf (stderr, "Staring router mandatory HWM test ...\n");
setup_test_environment ();
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *ctx = zmq_ctx_new ();
assert (ctx);
void *router = zmq_socket (ctx, ZMQ_ROUTER);
assert (router);
void *router = test_context_socket (ZMQ_ROUTER);
// Configure router socket to mandatory routing and set HWM and linger
int mandatory = 1;
rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &mandatory,
sizeof (mandatory));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
&mandatory, sizeof (mandatory)));
int sndhwm = 1;
rc = zmq_setsockopt (router, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (router, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm)));
int linger = 1;
rc = zmq_setsockopt (router, ZMQ_LINGER, &linger, sizeof (linger));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (router, ZMQ_LINGER, &linger, sizeof (linger)));
rc = zmq_bind (router, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
// Create dealer called "X" and connect it to our router, configure HWM
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer);
rc = zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1);
assert (rc == 0);
void *dealer = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
int rcvhwm = 1;
rc = zmq_setsockopt (dealer, ZMQ_RCVHWM, &rcvhwm, sizeof (rcvhwm));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer, ZMQ_RCVHWM, &rcvhwm, sizeof (rcvhwm)));
rc = zmq_connect (dealer, my_endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
// Get message from dealer to know when connection is ready
char buffer[255];
rc = zmq_send (dealer, "Hello", 5, 0);
assert (rc == 5);
rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 1);
assert (buffer[0] == 'X');
send_string_expect_success (dealer, "Hello", 0);
recv_string_expect_success (router, "X", 0);
int i;
const int buf_size = 65536;
char buf[buf_size];
memset (buf, 0, buf_size);
const uint8_t buf[buf_size] = {0};
// Send first batch of messages
for (i = 0; i < 100000; ++i) {
if (TRACE_ENABLED)
fprintf (stderr, "Sending message %d ...\n", i);
rc = zmq_send (router, "X", 1, ZMQ_DONTWAIT | ZMQ_SNDMORE);
const int rc = zmq_send (router, "X", 1, ZMQ_DONTWAIT | ZMQ_SNDMORE);
if (rc == -1 && zmq_errno () == EAGAIN)
break;
assert (rc == 1);
rc = zmq_send (router, buf, buf_size, ZMQ_DONTWAIT);
assert (rc == buf_size);
TEST_ASSERT_EQUAL_INT (1, rc);
send_array_expect_success (router, buf, ZMQ_DONTWAIT);
}
// This should fail after one message but kernel buffering could
// skew results
assert (i < 10);
TEST_ASSERT_LESS_THAN_INT (10, i);
msleep (1000);
// Send second batch of messages
for (; i < 100000; ++i) {
if (TRACE_ENABLED)
fprintf (stderr, "Sending message %d (part 2) ...\n", i);
rc = zmq_send (router, "X", 1, ZMQ_DONTWAIT | ZMQ_SNDMORE);
const int rc = zmq_send (router, "X", 1, ZMQ_DONTWAIT | ZMQ_SNDMORE);
if (rc == -1 && zmq_errno () == EAGAIN)
break;
assert (rc == 1);
rc = zmq_send (router, buf, buf_size, ZMQ_DONTWAIT);
assert (rc == buf_size);
TEST_ASSERT_EQUAL_INT (1, rc);
send_array_expect_success (router, buf, ZMQ_DONTWAIT);
}
// This should fail after two messages but kernel buffering could
// skew results
assert (i < 20);
TEST_ASSERT_LESS_THAN_INT (20, i);
if (TRACE_ENABLED)
fprintf (stderr, "Done sending messages.\n");
rc = zmq_close (router);
assert (rc == 0);
rc = zmq_close (dealer);
assert (rc == 0);
test_context_socket_close (router);
test_context_socket_close (dealer);
}
rc = zmq_ctx_term (ctx);
assert (rc == 0);
int main ()
{
setup_test_environment ();
return 0;
UNITY_BEGIN ();
RUN_TEST (test_router_mandatory_hwm);
return UNITY_END ();
}
This diff is collapsed.
......@@ -28,6 +28,17 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// Read one event off the monitor socket; return value and address
// by reference, if not null, and event number by value. Returns -1
......@@ -40,7 +51,7 @@ static int get_monitor_event (void *monitor_, int *value_, char **address_)
zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
return -1; // Interruped, presumably
assert (zmq_msg_more (&msg));
TEST_ASSERT_TRUE (zmq_msg_more (&msg));
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
uint16_t event = *(uint16_t *) (data);
......@@ -51,7 +62,7 @@ static int get_monitor_event (void *monitor_, int *value_, char **address_)
zmq_msg_init (&msg);
if (zmq_msg_recv (&msg, monitor_, 0) == -1)
return -1; // Interruped, presumably
assert (!zmq_msg_more (&msg));
TEST_ASSERT_TRUE (!zmq_msg_more (&msg));
if (address_) {
uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
......@@ -63,172 +74,132 @@ static int get_monitor_event (void *monitor_, int *value_, char **address_)
return event;
}
static void test_stream_handshake_timeout_accept (void)
static void test_stream_handshake_timeout_accept ()
{
int rc;
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);
// We use this socket in raw mode, to make a connection and send nothing
void *stream = zmq_socket (ctx, ZMQ_STREAM);
assert (stream);
void *stream = test_context_socket (ZMQ_STREAM);
int zero = 0;
rc = zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)));
// We'll be using this socket to test TCP stream handshake timeout
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer);
rc = zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero));
assert (rc == 0);
void *dealer = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)));
int val, tenth = 100;
size_t vsize = sizeof (val);
// check for the expected default handshake timeout value - 30 sec
rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize);
assert (rc == 0);
assert (vsize == sizeof (val));
assert (val == 30000);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
TEST_ASSERT_EQUAL (sizeof (val), vsize);
TEST_ASSERT_EQUAL_INT (30000, val);
// make handshake timeout faster - 1/10 sec
rc = zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth)));
vsize = sizeof (val);
// make sure zmq_setsockopt changed the value
rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize);
assert (rc == 0);
assert (vsize == sizeof (val));
assert (val == tenth);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
TEST_ASSERT_EQUAL (sizeof (val), vsize);
TEST_ASSERT_EQUAL_INT (tenth, val);
// Create and connect a socket for collecting monitor events on dealer
void *dealer_mon = zmq_socket (ctx, ZMQ_PAIR);
assert (dealer_mon);
void *dealer_mon = test_context_socket (ZMQ_PAIR);
rc = zmq_socket_monitor (dealer, "inproc://monitor-dealer",
ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED
| ZMQ_EVENT_ACCEPTED);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
dealer, "inproc://monitor-dealer",
ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
// Connect to the inproc endpoint so we'll get events
rc = zmq_connect (dealer_mon, "inproc://monitor-dealer");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_connect (dealer_mon, "inproc://monitor-dealer"));
// bind dealer socket to accept connection from non-sending stream socket
rc = zmq_bind (dealer, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (dealer, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
bind_loopback_ipv4 (dealer, my_endpoint, sizeof my_endpoint);
rc = zmq_connect (stream, my_endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (stream, my_endpoint));
// we should get ZMQ_EVENT_ACCEPTED and then ZMQ_EVENT_DISCONNECTED
int event = get_monitor_event (dealer_mon, NULL, NULL);
assert (event == ZMQ_EVENT_ACCEPTED);
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_ACCEPTED, event);
event = get_monitor_event (dealer_mon, NULL, NULL);
assert (event == ZMQ_EVENT_DISCONNECTED);
rc = zmq_close (dealer);
assert (rc == 0);
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, event);
rc = zmq_close (dealer_mon);
assert (rc == 0);
rc = zmq_close (stream);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
test_context_socket_close (dealer);
test_context_socket_close (dealer_mon);
test_context_socket_close (stream);
}
static void test_stream_handshake_timeout_connect (void)
static void test_stream_handshake_timeout_connect ()
{
int rc;
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
// Set up our context and sockets
void *ctx = zmq_ctx_new ();
assert (ctx);
// We use this socket in raw mode, to accept a connection and send nothing
void *stream = zmq_socket (ctx, ZMQ_STREAM);
assert (stream);
void *stream = test_context_socket (ZMQ_STREAM);
int zero = 0;
rc = zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero));
assert (rc == 0);
rc = zmq_bind (stream, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (stream, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero)));
bind_loopback_ipv4 (stream, my_endpoint, sizeof my_endpoint);
// We'll be using this socket to test TCP stream handshake timeout
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer);
rc = zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero));
assert (rc == 0);
void *dealer = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero)));
int val, tenth = 100;
size_t vsize = sizeof (val);
// check for the expected default handshake timeout value - 30 sec
rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize);
assert (rc == 0);
assert (vsize == sizeof (val));
assert (val == 30000);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
TEST_ASSERT_EQUAL (sizeof (val), vsize);
TEST_ASSERT_EQUAL_INT (30000, val);
// make handshake timeout faster - 1/10 sec
rc = zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth)));
vsize = sizeof (val);
// make sure zmq_setsockopt changed the value
rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize);
assert (rc == 0);
assert (vsize == sizeof (val));
assert (val == tenth);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize));
TEST_ASSERT_EQUAL (sizeof (val), vsize);
TEST_ASSERT_EQUAL_INT (tenth, val);
// Create and connect a socket for collecting monitor events on dealer
void *dealer_mon = zmq_socket (ctx, ZMQ_PAIR);
assert (dealer_mon);
void *dealer_mon = test_context_socket (ZMQ_PAIR);
rc = zmq_socket_monitor (dealer, "inproc://monitor-dealer",
ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED
| ZMQ_EVENT_ACCEPTED);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_socket_monitor (
dealer, "inproc://monitor-dealer",
ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED | ZMQ_EVENT_ACCEPTED));
// Connect to the inproc endpoint so we'll get events
rc = zmq_connect (dealer_mon, "inproc://monitor-dealer");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_connect (dealer_mon, "inproc://monitor-dealer"));
// connect dealer socket to non-sending stream socket
rc = zmq_connect (dealer, my_endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
// we should get ZMQ_EVENT_CONNECTED and then ZMQ_EVENT_DISCONNECTED
int event = get_monitor_event (dealer_mon, NULL, NULL);
assert (event == ZMQ_EVENT_CONNECTED);
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_CONNECTED, event);
event = get_monitor_event (dealer_mon, NULL, NULL);
assert (event == ZMQ_EVENT_DISCONNECTED);
TEST_ASSERT_EQUAL_INT (ZMQ_EVENT_DISCONNECTED, event);
rc = zmq_close (dealer);
assert (rc == 0);
rc = zmq_close (dealer_mon);
assert (rc == 0);
rc = zmq_close (stream);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
test_context_socket_close (dealer);
test_context_socket_close (dealer_mon);
test_context_socket_close (stream);
}
int main (void)
int main ()
{
setup_test_environment ();
test_stream_handshake_timeout_accept ();
test_stream_handshake_timeout_connect ();
UNITY_BEGIN ();
RUN_TEST (test_stream_handshake_timeout_accept);
RUN_TEST (test_stream_handshake_timeout_connect);
return UNITY_END ();
}
......@@ -29,6 +29,15 @@
#define __STDC_LIMIT_MACROS // to define SIZE_MAX with older compilers
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
}
void tearDown ()
{
}
void handler (int timer_id_, void *arg_)
{
......@@ -53,193 +62,167 @@ void test_null_timer_pointers ()
{
void *timers = NULL;
int rc = zmq_timers_destroy (&timers);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_destroy (&timers));
// TODO this currently triggers an access violation
#if 0
rc = zmq_timers_destroy (NULL);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO(EFAULT, zmq_timers_destroy (NULL));
#endif
const size_t dummy_interval = 100;
const int dummy_timer_id = 1;
rc = zmq_timers_add (timers, dummy_interval, &handler, NULL);
assert (rc == -1 && errno == EFAULT);
rc = zmq_timers_add (&timers, dummy_interval, &handler, NULL);
assert (rc == -1 && errno == EFAULT);
rc = zmq_timers_cancel (timers, dummy_timer_id);
assert (rc == -1 && errno == EFAULT);
rc = zmq_timers_cancel (&timers, dummy_timer_id);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO (
EFAULT, zmq_timers_add (timers, dummy_interval, &handler, NULL));
TEST_ASSERT_FAILURE_ERRNO (
EFAULT, zmq_timers_add (&timers, dummy_interval, &handler, NULL));
rc = zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO (EFAULT,
zmq_timers_cancel (timers, dummy_timer_id));
TEST_ASSERT_FAILURE_ERRNO (EFAULT,
zmq_timers_cancel (&timers, dummy_timer_id));
rc = zmq_timers_set_interval (&timers, dummy_timer_id, dummy_interval);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO (
EFAULT, zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval));
TEST_ASSERT_FAILURE_ERRNO (
EFAULT,
zmq_timers_set_interval (&timers, dummy_timer_id, dummy_interval));
rc = zmq_timers_reset (timers, dummy_timer_id);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO (EFAULT,
zmq_timers_reset (timers, dummy_timer_id));
TEST_ASSERT_FAILURE_ERRNO (EFAULT,
zmq_timers_reset (&timers, dummy_timer_id));
rc = zmq_timers_reset (&timers, dummy_timer_id);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_timeout (timers));
TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_timeout (&timers));
rc = zmq_timers_timeout (timers);
assert (rc == -1 && errno == EFAULT);
rc = zmq_timers_timeout (&timers);
assert (rc == -1 && errno == EFAULT);
rc = zmq_timers_execute (timers);
assert (rc == -1 && errno == EFAULT);
rc = zmq_timers_execute (&timers);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_execute (timers));
TEST_ASSERT_FAILURE_ERRNO (EFAULT, zmq_timers_execute (&timers));
}
void test_corner_cases ()
{
void *timers = zmq_timers_new ();
assert (timers);
TEST_ASSERT_NOT_NULL (timers);
const size_t dummy_interval = SIZE_MAX;
const int dummy_timer_id = 1;
// attempt to cancel non-existent timer
int rc = zmq_timers_cancel (timers, dummy_timer_id);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (EINVAL,
zmq_timers_cancel (timers, dummy_timer_id));
// attempt to set interval of non-existent timer
rc = zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (
EINVAL, zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval));
// attempt to reset non-existent timer
rc = zmq_timers_reset (timers, dummy_timer_id);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (EINVAL,
zmq_timers_reset (timers, dummy_timer_id));
// attempt to add NULL handler
rc = zmq_timers_add (timers, dummy_interval, NULL, NULL);
assert (rc == -1 && errno == EFAULT);
TEST_ASSERT_FAILURE_ERRNO (
EFAULT, zmq_timers_add (timers, dummy_interval, NULL, NULL));
int timer_id = zmq_timers_add (timers, dummy_interval, handler, NULL);
assert (timer_id != -1);
const int timer_id = TEST_ASSERT_SUCCESS_ERRNO (
zmq_timers_add (timers, dummy_interval, handler, NULL));
// attempt to cancel timer twice
// TODO should this case really be an error? canceling twice could be allowed
rc = zmq_timers_cancel (timers, timer_id);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_cancel (timers, timer_id));
rc = zmq_timers_cancel (timers, timer_id);
assert (rc == -1 && errno == EINVAL);
TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_timers_cancel (timers, timer_id));
// timeout without any timers active
rc = zmq_timers_timeout (timers);
assert (rc == -1);
TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_timers_timeout (timers));
rc = zmq_timers_destroy (&timers);
assert (rc == 0);
// cleanup
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_destroy (&timers));
}
int main (void)
void test_timers ()
{
setup_test_environment ();
void *timers = zmq_timers_new ();
assert (timers);
TEST_ASSERT_NOT_NULL (timers);
bool timer_invoked = false;
const unsigned long full_timeout = 100;
void *const stopwatch = zmq_stopwatch_start ();
int timer_id =
zmq_timers_add (timers, full_timeout, handler, &timer_invoked);
assert (timer_id);
const int timer_id = TEST_ASSERT_SUCCESS_ERRNO (
zmq_timers_add (timers, full_timeout, handler, &timer_invoked));
// Timer should not have been invoked yet
int rc = zmq_timers_execute (timers);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
#ifdef ZMQ_BUILD_DRAFT_API
if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) {
assert (!timer_invoked);
TEST_ASSERT_FALSE (timer_invoked);
}
#endif
// Wait half the time and check again
long timeout = zmq_timers_timeout (timers);
assert (rc != -1);
long timeout = TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_timeout (timers));
msleep (timeout / 2);
rc = zmq_timers_execute (timers);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
#ifdef ZMQ_BUILD_DRAFT_API
if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) {
assert (!timer_invoked);
TEST_ASSERT_FALSE (timer_invoked);
}
#endif
// Wait until the end
rc = sleep_and_execute (timers);
assert (rc == 0);
assert (timer_invoked);
TEST_ASSERT_SUCCESS_ERRNO (sleep_and_execute (timers));
TEST_ASSERT_TRUE (timer_invoked);
timer_invoked = false;
// Wait half the time and check again
timeout = zmq_timers_timeout (timers);
assert (rc != -1);
timeout = TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_timeout (timers));
msleep (timeout / 2);
rc = zmq_timers_execute (timers);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
#ifdef ZMQ_BUILD_DRAFT_API
if (zmq_stopwatch_intermediate (stopwatch) < 2 * full_timeout) {
assert (!timer_invoked);
TEST_ASSERT_FALSE (timer_invoked);
}
#endif
// Reset timer and wait half of the time left
rc = zmq_timers_reset (timers, timer_id);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_reset (timers, timer_id));
msleep (timeout / 2);
rc = zmq_timers_execute (timers);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
if (zmq_stopwatch_stop (stopwatch) < 2 * full_timeout) {
assert (!timer_invoked);
TEST_ASSERT_FALSE (timer_invoked);
}
// Wait until the end
rc = sleep_and_execute (timers);
assert (rc == 0);
assert (timer_invoked);
TEST_ASSERT_SUCCESS_ERRNO (sleep_and_execute (timers));
TEST_ASSERT_TRUE (timer_invoked);
timer_invoked = false;
// reschedule
rc = zmq_timers_set_interval (timers, timer_id, 50);
assert (rc == 0);
rc = sleep_and_execute (timers);
assert (rc == 0);
assert (timer_invoked);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_set_interval (timers, timer_id, 50));
TEST_ASSERT_SUCCESS_ERRNO (sleep_and_execute (timers));
TEST_ASSERT_TRUE (timer_invoked);
timer_invoked = false;
// cancel timer
timeout = zmq_timers_timeout (timers);
assert (rc != -1);
rc = zmq_timers_cancel (timers, timer_id);
assert (rc == 0);
timeout = TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_timeout (timers));
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_cancel (timers, timer_id));
msleep (timeout * 2);
rc = zmq_timers_execute (timers);
assert (rc == 0);
assert (!timer_invoked);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_execute (timers));
TEST_ASSERT_FALSE (timer_invoked);
rc = zmq_timers_destroy (&timers);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_timers_destroy (&timers));
}
test_null_timer_pointers ();
test_corner_cases ();
int main ()
{
setup_test_environment ();
return 0;
UNITY_BEGIN ();
RUN_TEST (test_timers);
RUN_TEST (test_null_timer_pointers);
RUN_TEST (test_corner_cases);
return UNITY_END ();
}
/*
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/>.
*/
#include "testutil.hpp"
#if !defined(ZMQ_HAVE_WINDOWS)
#include <sys/socket.h>
#include <sys/un.h>
void pre_allocate_sock (void *zmq_socket_, const char *path_)
{
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strcpy (addr.sun_path, path_);
unlink (path_);
int s_pre = socket (AF_UNIX, SOCK_STREAM, 0);
assert (s_pre != -1);
int rc =
bind (s_pre, (struct sockaddr *) &addr, sizeof (struct sockaddr_un));
assert (rc == 0);
rc = listen (s_pre, SOMAXCONN);
assert (rc == 0);
rc = zmq_setsockopt (zmq_socket_, ZMQ_USE_FD, &s_pre, sizeof (s_pre));
assert (rc == 0);
}
void test_req_rep ()
{
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_REP);
assert (sb);
pre_allocate_sock (sb, "/tmp/test_use_fd_ipc");
int rc = zmq_bind (sb, "ipc:///tmp/test_use_fd_ipc");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_REQ);
assert (sc);
rc = zmq_connect (sc, "ipc:///tmp/test_use_fd_ipc");
assert (rc == 0);
bounce (sb, sc);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
rc = unlink ("/tmp/test_use_fd_ipc");
assert (rc == 0);
}
void test_pair ()
{
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_PAIR);
assert (sb);
pre_allocate_sock (sb, "/tmp/test_use_fd_ipc");
int rc = zmq_bind (sb, "ipc:///tmp/test_use_fd_ipc");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_PAIR);
assert (sc);
rc = zmq_connect (sc, "ipc:///tmp/test_use_fd_ipc");
assert (rc == 0);
bounce (sb, sc);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
rc = unlink ("/tmp/test_use_fd_ipc");
assert (rc == 0);
}
void test_client_server ()
{
#if defined(ZMQ_SERVER) && defined(ZMQ_CLIENT)
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_SERVER);
assert (sb);
pre_allocate_sock (sb, "/tmp/test_use_fd_ipc");
int rc = zmq_bind (sb, "ipc:///tmp/test_use_fd_ipc");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_CLIENT);
assert (sc);
rc = zmq_connect (sc, "ipc:///tmp/test_use_fd_ipc");
assert (rc == 0);
zmq_msg_t msg;
rc = zmq_msg_init_size (&msg, 1);
assert (rc == 0);
char *data = (char *) zmq_msg_data (&msg);
data[0] = 1;
rc = zmq_msg_send (&msg, sc, ZMQ_SNDMORE);
assert (rc == -1);
rc = zmq_msg_send (&msg, sc, 0);
assert (rc == 1);
rc = zmq_msg_init (&msg);
assert (rc == 0);
rc = zmq_msg_recv (&msg, sb, 0);
assert (rc == 1);
uint32_t routing_id = zmq_msg_routing_id (&msg);
assert (routing_id != 0);
rc = zmq_msg_close (&msg);
assert (rc == 0);
rc = zmq_msg_init_size (&msg, 1);
assert (rc == 0);
data = (char *) zmq_msg_data (&msg);
data[0] = 2;
rc = zmq_msg_set_routing_id (&msg, routing_id);
assert (rc == 0);
rc = zmq_msg_send (&msg, sb, ZMQ_SNDMORE);
assert (rc == -1);
rc = zmq_msg_send (&msg, sb, 0);
assert (rc == 1);
rc = zmq_msg_recv (&msg, sc, 0);
assert (rc == 1);
routing_id = zmq_msg_routing_id (&msg);
assert (routing_id == 0);
rc = zmq_msg_close (&msg);
assert (rc == 0);
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
rc = unlink ("/tmp/test_use_fd_ipc");
assert (rc == 0);
#endif
}
int main (void)
{
setup_test_environment ();
test_req_rep ();
test_pair ();
test_client_server ();
return 0;
}
#else
int main (void)
{
return 0;
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment