Unverified Commit b65b523a authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #3341 from sigiesec/migrate-more-tests

Problem: tests without test framework
parents cb2b9a8c 88bea4fd
...@@ -580,7 +580,8 @@ tests_test_ctx_options_SOURCES = tests/test_ctx_options.cpp ...@@ -580,7 +580,8 @@ tests_test_ctx_options_SOURCES = tests/test_ctx_options.cpp
tests_test_ctx_options_LDADD = src/libzmq.la tests_test_ctx_options_LDADD = src/libzmq.la
tests_test_iov_SOURCES = tests/test_iov.cpp tests_test_iov_SOURCES = tests/test_iov.cpp
tests_test_iov_LDADD = src/libzmq.la tests_test_iov_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_iov_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_ctx_destroy_SOURCES = tests/test_ctx_destroy.cpp tests_test_ctx_destroy_SOURCES = tests/test_ctx_destroy.cpp
tests_test_ctx_destroy_LDADD = src/libzmq.la ${UNITY_LIBS} tests_test_ctx_destroy_LDADD = src/libzmq.la ${UNITY_LIBS}
...@@ -635,7 +636,8 @@ tests_test_inproc_connect_LDADD = src/libzmq.la ${UNITY_LIBS} ...@@ -635,7 +636,8 @@ tests_test_inproc_connect_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_inproc_connect_CPPFLAGS = ${UNITY_CPPFLAGS} tests_test_inproc_connect_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_issue_566_SOURCES = tests/test_issue_566.cpp tests_test_issue_566_SOURCES = tests/test_issue_566.cpp
tests_test_issue_566_LDADD = src/libzmq.la tests_test_issue_566_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_issue_566_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_proxy_SOURCES = tests/test_proxy.cpp tests_test_proxy_SOURCES = tests/test_proxy.cpp
tests_test_proxy_LDADD = src/libzmq.la tests_test_proxy_LDADD = src/libzmq.la
...@@ -657,7 +659,8 @@ tests_test_many_sockets_SOURCES = tests/test_many_sockets.cpp ...@@ -657,7 +659,8 @@ tests_test_many_sockets_SOURCES = tests/test_many_sockets.cpp
tests_test_many_sockets_LDADD = src/libzmq.la tests_test_many_sockets_LDADD = src/libzmq.la
tests_test_diffserv_SOURCES = tests/test_diffserv.cpp tests_test_diffserv_SOURCES = tests/test_diffserv.cpp
tests_test_diffserv_LDADD = src/libzmq.la tests_test_diffserv_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_diffserv_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_connect_rid_SOURCES = tests/test_connect_rid.cpp tests_test_connect_rid_SOURCES = tests/test_connect_rid.cpp
tests_test_connect_rid_LDADD = src/libzmq.la ${UNITY_LIBS} tests_test_connect_rid_LDADD = src/libzmq.la ${UNITY_LIBS}
...@@ -794,7 +797,8 @@ tests_test_shutdown_stress_LDADD = src/libzmq.la ${UNITY_LIBS} ...@@ -794,7 +797,8 @@ tests_test_shutdown_stress_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_shutdown_stress_CPPFLAGS = ${UNITY_CPPFLAGS} tests_test_shutdown_stress_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_ipc_wildcard_SOURCES = tests/test_ipc_wildcard.cpp tests_test_ipc_wildcard_SOURCES = tests/test_ipc_wildcard.cpp
tests_test_ipc_wildcard_LDADD = src/libzmq.la tests_test_ipc_wildcard_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_ipc_wildcard_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_pair_ipc_SOURCES = \ tests_test_pair_ipc_SOURCES = \
tests/test_pair_ipc.cpp \ tests/test_pair_ipc.cpp \
...@@ -813,7 +817,8 @@ tests_test_reqrep_ipc_LDADD = src/libzmq.la ${UNITY_LIBS} ...@@ -813,7 +817,8 @@ tests_test_reqrep_ipc_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_reqrep_ipc_CPPFLAGS = ${UNITY_CPPFLAGS} tests_test_reqrep_ipc_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_timeo_SOURCES = tests/test_timeo.cpp tests_test_timeo_SOURCES = tests/test_timeo.cpp
tests_test_timeo_LDADD = src/libzmq.la tests_test_timeo_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_timeo_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_filter_ipc_SOURCES = tests/test_filter_ipc.cpp tests_test_filter_ipc_SOURCES = tests/test_filter_ipc.cpp
tests_test_filter_ipc_LDADD = src/libzmq.la ${UNITY_LIBS} tests_test_filter_ipc_LDADD = src/libzmq.la ${UNITY_LIBS}
......
...@@ -28,57 +28,55 @@ ...@@ -28,57 +28,55 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) #include <unity.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void test_diffserv ()
{ {
int rc;
int tos = 0x28; int tos = 0x28;
int o_tos; int o_tos;
size_t tos_size = sizeof (tos); size_t tos_size = sizeof (tos);
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
setup_test_environment (); void *sb = test_context_socket (ZMQ_PAIR);
void *ctx = zmq_ctx_new (); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (sb, ZMQ_TOS, &tos, tos_size));
assert (ctx); bind_loopback_ipv4 (sb, my_endpoint, sizeof (my_endpoint));
void *sb = zmq_socket (ctx, ZMQ_PAIR); TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (sb, ZMQ_TOS, &o_tos, &tos_size));
assert (sb); TEST_ASSERT_EQUAL (tos, o_tos);
rc = zmq_setsockopt (sb, ZMQ_TOS, &tos, tos_size);
assert (rc == 0); void *sc = test_context_socket (ZMQ_PAIR);
rc = zmq_bind (sb, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
rc = zmq_getsockopt (sb, ZMQ_TOS, &o_tos, &tos_size);
assert (rc == 0);
assert (o_tos == tos);
void *sc = zmq_socket (ctx, ZMQ_PAIR);
assert (sc);
tos = 0x58; tos = 0x58;
rc = zmq_setsockopt (sc, ZMQ_TOS, &tos, tos_size); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (sc, ZMQ_TOS, &tos, tos_size));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
rc = zmq_connect (sc, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (sc, ZMQ_TOS, &o_tos, &tos_size));
assert (rc == 0); TEST_ASSERT_EQUAL (tos, o_tos);
rc = zmq_getsockopt (sc, ZMQ_TOS, &o_tos, &tos_size);
assert (rc == 0);
assert (o_tos == tos);
// Wireshark can be used to verify that the server socket is // Wireshark can be used to verify that the server socket is
// using DSCP 0x28 in packets to the client while the client // using DSCP 0x28 in packets to the client while the client
// is using 0x58 in packets to the server. // is using 0x58 in packets to the server.
bounce (sb, sc); bounce (sb, sc);
rc = zmq_close (sc); test_context_socket_close (sc);
assert (rc == 0); test_context_socket_close (sb);
}
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx); int main ()
assert (rc == 0); {
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test_diffserv);
return UNITY_END ();
} }
...@@ -28,6 +28,19 @@ ...@@ -28,6 +28,19 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// XSI vector I/O // XSI vector I/O
#if defined ZMQ_HAVE_UIO #if defined ZMQ_HAVE_UIO
...@@ -40,102 +53,91 @@ struct iovec ...@@ -40,102 +53,91 @@ struct iovec
}; };
#endif #endif
void do_check (void *sb_, void *sc_, size_t msg_size_) static void do_check (void *sb_, void *sc_, size_t msg_size_)
{ {
assert (sb_ && sc_ && msg_size_ > 0); assert (sb_ && sc_ && msg_size_ > 0);
int rc = 0;
const char msg_val = '1'; const char msg_val = '1';
const int num_messages = 10; const int num_messages = 10;
size_t send_count, recv_count; size_t send_count, recv_count;
send_count = recv_count = num_messages; send_count = recv_count = num_messages;
char *ref_msg = (char *) malloc (msg_size_); char *ref_msg = static_cast<char *> (malloc (msg_size_));
assert (ref_msg); TEST_ASSERT_NOT_NULL (ref_msg);
memset (ref_msg, msg_val, msg_size_); memset (ref_msg, msg_val, msg_size_);
// zmq_sendiov(3) as a single multi-part send // zmq_sendiov(3) as a single multi-part send
struct iovec send_iov[num_messages]; struct iovec send_iov[num_messages];
char *buf = (char *) malloc (msg_size_ * num_messages); char *buf = static_cast<char *> (malloc (msg_size_ * num_messages));
for (int i = 0; i < num_messages; i++) { for (int i = 0; i < num_messages; i++) {
send_iov[i].iov_base = &buf[i * msg_size_]; send_iov[i].iov_base = &buf[i * msg_size_];
send_iov[i].iov_len = msg_size_; send_iov[i].iov_len = msg_size_;
memcpy (send_iov[i].iov_base, ref_msg, msg_size_); memcpy (send_iov[i].iov_base, ref_msg, msg_size_);
// TODO: this assertion only checks if memcpy behaves as expected... remove this?
assert (memcmp (ref_msg, send_iov[i].iov_base, msg_size_) == 0); assert (memcmp (ref_msg, send_iov[i].iov_base, msg_size_) == 0);
} }
// Test errors - zmq_recviov - null socket // Test errors - zmq_recviov - null socket
rc = zmq_sendiov (NULL, send_iov, send_count, ZMQ_SNDMORE); TEST_ASSERT_FAILURE_ERRNO (
assert (rc == -1 && errno == ENOTSOCK); ENOTSOCK, zmq_sendiov (NULL, send_iov, send_count, ZMQ_SNDMORE));
// Test errors - zmq_recviov - invalid send count // Test errors - zmq_recviov - invalid send count
rc = zmq_sendiov (sc_, send_iov, 0, 0); TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_sendiov (sc_, send_iov, 0, 0));
assert (rc == -1 && errno == EINVAL);
// Test errors - zmq_recviov - null iovec // Test errors - zmq_recviov - null iovec
rc = zmq_sendiov (sc_, NULL, send_count, 0); TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_sendiov (sc_, NULL, send_count, 0));
assert (rc == -1 && errno == EINVAL);
// Test success // Test success
rc = zmq_sendiov (sc_, send_iov, send_count, ZMQ_SNDMORE);
// The zmq_sendiov(3) API method does not follow the same semantics as // The zmq_sendiov(3) API method does not follow the same semantics as
// zmq_recviov(3); the latter returns the count of messages sent, rightly // zmq_recviov(3); the latter returns the count of messages sent, rightly
// so, whilst the former sends the number of bytes successfully sent from // so, whilst the former sends the number of bytes successfully sent from
// the last message, which does not hold much sense from a batch send // the last message, which does not hold much sense from a batch send
// perspective; hence the assert checks if rc is same as msg_size. // perspective; hence the assert checks if the result is same as msg_size.
assert ((size_t) rc == msg_size_); TEST_ASSERT_EQUAL_INT (
(int) msg_size_, TEST_ASSERT_SUCCESS_ERRNO (
zmq_sendiov (sc_, send_iov, send_count, ZMQ_SNDMORE)));
// zmq_recviov(3) single-shot // zmq_recviov(3) single-shot
struct iovec recv_iov[num_messages]; struct iovec recv_iov[num_messages];
// Test errors - zmq_recviov - null socket // Test errors - zmq_recviov - null socket
rc = zmq_recviov (NULL, recv_iov, &recv_count, 0); TEST_ASSERT_FAILURE_ERRNO (ENOTSOCK,
assert (rc == -1 && errno == ENOTSOCK); zmq_recviov (NULL, recv_iov, &recv_count, 0));
// Test error - zmq_recviov - invalid receive count // Test error - zmq_recviov - invalid receive count
rc = zmq_recviov (sb_, recv_iov, NULL, 0); TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_recviov (sb_, recv_iov, NULL, 0));
assert (rc == -1 && errno == EINVAL);
size_t invalid_recv_count = 0; size_t invalid_recv_count = 0;
rc = zmq_recviov (sb_, recv_iov, &invalid_recv_count, 0); TEST_ASSERT_FAILURE_ERRNO (
assert (rc == -1 && errno == EINVAL); EINVAL, zmq_recviov (sb_, recv_iov, &invalid_recv_count, 0));
// Test error - zmq_recviov - null iovec // Test error - zmq_recviov - null iovec
rc = zmq_recviov (sb_, NULL, &recv_count, 0); TEST_ASSERT_FAILURE_ERRNO (EINVAL, zmq_recviov (sb_, NULL, &recv_count, 0));
assert (rc == -1 && errno == EINVAL);
// Test success // Test success
rc = zmq_recviov (sb_, recv_iov, &recv_count, 0); TEST_ASSERT_EQUAL_INT (
assert (rc == num_messages); num_messages,
TEST_ASSERT_SUCCESS_ERRNO (zmq_recviov (sb_, recv_iov, &recv_count, 0)));
for (int i = 0; i < num_messages; i++) { for (int i = 0; i < num_messages; i++) {
assert (recv_iov[i].iov_base); TEST_ASSERT_NOT_NULL (recv_iov[i].iov_base);
assert (memcmp (ref_msg, recv_iov[i].iov_base, msg_size_) == 0); TEST_ASSERT_EQUAL_STRING_LEN (ref_msg, recv_iov[i].iov_base, msg_size_);
free (recv_iov[i].iov_base); free (recv_iov[i].iov_base);
} }
assert (send_count == recv_count); TEST_ASSERT_EQUAL_INT (send_count, recv_count);
free (ref_msg); free (ref_msg);
free (buf); free (buf);
} }
int main (void) void test_iov ()
{ {
setup_test_environment (); void *sb = test_context_socket (ZMQ_PULL);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "inproc://a"));
void *ctx = zmq_ctx_new ();
assert (ctx);
int rc;
void *sb = zmq_socket (ctx, ZMQ_PULL);
assert (sb);
rc = zmq_bind (sb, "inproc://a");
assert (rc == 0);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
void *sc = zmq_socket (ctx, ZMQ_PUSH);
rc = zmq_connect (sc, "inproc://a");
assert (rc == 0);
void *sc = test_context_socket (ZMQ_PUSH);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "inproc://a"));
// message bigger than VSM max // message bigger than VSM max
do_check (sb, sc, 100); do_check (sb, sc, 100);
...@@ -143,14 +145,15 @@ int main (void) ...@@ -143,14 +145,15 @@ int main (void)
// message smaller than VSM max // message smaller than VSM max
do_check (sb, sc, 10); do_check (sb, sc, 10);
rc = zmq_close (sc); test_context_socket_close (sc);
assert (rc == 0); test_context_socket_close (sb);
}
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx); int main ()
assert (rc == 0); {
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test_iov);
return UNITY_END ();
} }
...@@ -28,38 +28,44 @@ ...@@ -28,38 +28,44 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) #include <unity.h>
void setUp ()
{ {
setup_test_environment (); setup_test_context ();
void *ctx = zmq_ctx_new (); }
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_PAIR); void tearDown ()
assert (sb); {
int rc = zmq_bind (sb, "ipc://*"); teardown_test_context ();
assert (rc == 0); }
void test_ipc_wildcard ()
{
void *sb = test_context_socket (ZMQ_PAIR);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ipc://*"));
char endpoint[200]; char endpoint[200];
size_t size = sizeof (endpoint); size_t size = sizeof (endpoint);
rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &size); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &size));
void *sc = zmq_socket (ctx, ZMQ_PAIR); void *sc = test_context_socket (ZMQ_PAIR);
assert (sc); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, endpoint));
rc = zmq_connect (sc, endpoint);
assert (rc == 0);
bounce (sb, sc); bounce (sb, sc);
rc = zmq_close (sc); test_context_socket_close (sc);
assert (rc == 0); test_context_socket_close (sb);
}
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx); int main ()
assert (rc == 0); {
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test_ipc_wildcard);
return UNITY_END ();
} }
...@@ -28,32 +28,38 @@ ...@@ -28,32 +28,38 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// Issue 566 describes a problem in libzmq v4.0.0 where a dealer to router // Issue 566 describes a problem in libzmq v4.0.0 where a dealer to router
// connection would fail randomly. The test works when the two sockets are // connection would fail randomly. The test works when the two sockets are
// on the same context, and failed when they were on separate contexts. // on the same context, and failed when they were on separate contexts.
// Fixed by https://github.com/zeromq/libzmq/commit/be25cf. // Fixed by https://github.com/zeromq/libzmq/commit/be25cf.
void test_issue_566 ()
int main (void)
{ {
setup_test_environment ();
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
void *ctx1 = zmq_ctx_new (); void *ctx1 = zmq_ctx_new ();
assert (ctx1); TEST_ASSERT_NOT_NULL (ctx1);
void *ctx2 = zmq_ctx_new (); void *ctx2 = zmq_ctx_new ();
assert (ctx2); TEST_ASSERT_NOT_NULL (ctx2);
void *router = zmq_socket (ctx1, ZMQ_ROUTER); void *router = zmq_socket (ctx1, ZMQ_ROUTER);
int on = 1; int on = 1;
int rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &on, sizeof (on)));
rc = zmq_bind (router, "tcp://127.0.0.1:*"); bind_loopback_ipv4 (router, my_endpoint, sizeof (my_endpoint));
assert (rc != -1);
rc = zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
// Repeat often enough to be sure this works as it should // Repeat often enough to be sure this works as it should
for (int cycle = 0; cycle < 100; cycle++) { for (int cycle = 0; cycle < 100; cycle++) {
...@@ -62,13 +68,12 @@ int main (void) ...@@ -62,13 +68,12 @@ int main (void)
void *dealer = zmq_socket (ctx2, ZMQ_DEALER); void *dealer = zmq_socket (ctx2, ZMQ_DEALER);
char routing_id[10]; char routing_id[10];
sprintf (routing_id, "%09d", cycle); sprintf (routing_id, "%09d", cycle);
rc = zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (dealer, ZMQ_ROUTING_ID, routing_id, 10));
int rcvtimeo = 1000; int rcvtimeo = 1000;
rc = zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (dealer, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int)));
rc = zmq_connect (dealer, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
assert (rc == 0);
// Router will try to send to dealer, at short intervals. // Router will try to send to dealer, at short intervals.
// It typically takes 2-5 msec for the connection to establish // It typically takes 2-5 msec for the connection to establish
...@@ -76,24 +81,27 @@ int main (void) ...@@ -76,24 +81,27 @@ int main (void)
// before failing the test (e.g. for running on a debugger or // before failing the test (e.g. for running on a debugger or
// a very slow system). // a very slow system).
for (int attempt = 0; attempt < 500; attempt++) { for (int attempt = 0; attempt < 500; attempt++) {
zmq_poll (0, 0, 2); zmq_poll (NULL, 0, 2);
rc = zmq_send (router, routing_id, 10, ZMQ_SNDMORE); int rc = zmq_send (router, routing_id, 10, ZMQ_SNDMORE);
if (rc == -1 && errno == EHOSTUNREACH) if (rc == -1 && errno == EHOSTUNREACH)
continue; continue;
assert (rc == 10); TEST_ASSERT_EQUAL (10, rc);
rc = zmq_send (router, "HELLO", 5, 0); send_string_expect_success (router, "HELLO", 0);
assert (rc == 5);
break; break;
} }
uint8_t buffer[5]; recv_string_expect_success (dealer, "HELLO", 0);
rc = zmq_recv (dealer, buffer, 5, 0);
assert (rc == 5);
assert (memcmp (buffer, "HELLO", 5) == 0);
close_zero_linger (dealer); close_zero_linger (dealer);
} }
zmq_close (router); zmq_close (router);
zmq_ctx_destroy (ctx1); zmq_ctx_destroy (ctx1);
zmq_ctx_destroy (ctx2); zmq_ctx_destroy (ctx2);
}
int main ()
{
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test_issue_566);
return UNITY_END ();
} }
...@@ -28,36 +28,41 @@ ...@@ -28,36 +28,41 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) #include <unity.h>
void setUp ()
{ {
setup_test_environment (); setup_test_context ();
void *ctx = zmq_ctx_new (); }
assert (ctx);
void *frontend = zmq_socket (ctx, ZMQ_DEALER); void tearDown ()
assert (frontend); {
int rc = zmq_bind (frontend, "inproc://timeout_test"); teardown_test_context ();
assert (rc == 0); }
void test_timeo ()
{
void *frontend = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (frontend, "inproc://timeout_test"));
// Receive on disconnected socket returns immediately // Receive on disconnected socket returns immediately
char buffer[32]; char buffer[32];
rc = zmq_recv (frontend, buffer, 32, ZMQ_DONTWAIT); TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
assert (rc == -1); zmq_recv (frontend, buffer, 32, ZMQ_DONTWAIT));
assert (zmq_errno () == EAGAIN);
// Check whether receive timeout is honored // Check whether receive timeout is honored
const int timeout = 250; const int timeout = 250;
const int jitter = 50; const int jitter = 50;
rc = zmq_setsockopt (frontend, ZMQ_RCVTIMEO, &timeout, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (frontend, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
void *stopwatch = zmq_stopwatch_start (); void *stopwatch = zmq_stopwatch_start ();
rc = zmq_recv (frontend, buffer, 32, 0); TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_recv (frontend, buffer, 32, 0));
assert (rc == -1);
assert (zmq_errno () == EAGAIN);
unsigned int elapsed = zmq_stopwatch_stop (stopwatch) / 1000; unsigned int elapsed = zmq_stopwatch_stop (stopwatch) / 1000;
assert (elapsed > timeout - jitter); TEST_ASSERT_GREATER_THAN_INT (timeout - jitter, elapsed);
if (elapsed >= timeout + jitter) { if (elapsed >= timeout + jitter) {
// we cannot assert this on a non-RT system // we cannot assert this on a non-RT system
fprintf (stderr, fprintf (stderr,
...@@ -67,27 +72,24 @@ int main (void) ...@@ -67,27 +72,24 @@ int main (void)
} }
// Check that normal message flow works as expected // Check that normal message flow works as expected
void *backend = zmq_socket (ctx, ZMQ_DEALER); void *backend = test_context_socket (ZMQ_DEALER);
assert (backend); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (backend, "inproc://timeout_test"));
rc = zmq_connect (backend, "inproc://timeout_test"); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (backend, ZMQ_SNDTIMEO, &timeout, sizeof (int)));
rc = zmq_setsockopt (backend, ZMQ_SNDTIMEO, &timeout, sizeof (int));
assert (rc == 0);
rc = zmq_send (backend, "Hello", 5, 0);
assert (rc == 5);
rc = zmq_recv (frontend, buffer, 32, 0);
assert (rc == 5);
// Clean-up send_string_expect_success (backend, "Hello", 0);
rc = zmq_close (backend); recv_string_expect_success (frontend, "Hello", 0);
assert (rc == 0);
rc = zmq_close (frontend); // Clean-up
assert (rc == 0); test_context_socket_close (backend);
test_context_socket_close (frontend);
}
rc = zmq_ctx_term (ctx); int main ()
assert (rc == 0); {
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test_timeo);
return UNITY_END ();
} }
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