mirror of
https://github.com/zeromq/libzmq.git
synced 2025-01-19 00:46:05 +01:00
Problem: test_stream_timeout not yet using unity
Solution: migrate to unity
This commit is contained in:
parent
f01682292c
commit
980791d182
@ -553,11 +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 ${UNITY_LIBS}
|
||||
tests_test_stream_empty_CPPFLAGS = ${UNITY_CPPFLAGS}
|
||||
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
|
||||
|
@ -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);
|
||||
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);
|
||||
}
|
||||
|
||||
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 ();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user