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

Merge pull request #2987 from sigiesec/migrate-to-unity

Migrate further tests (test_hwm, test_router_mandatory) to unity
parents c9437ab7 c6023618
......@@ -462,8 +462,9 @@ tests_test_reqrep_tcp_SOURCES = \
tests/testutil.hpp
tests_test_reqrep_tcp_LDADD = src/libzmq.la
tests_test_hwm_SOURCES = tests/test_hwm.cpp
tests_test_hwm_LDADD = src/libzmq.la
tests_test_hwm_SOURCES = tests/test_hwm.cpp tests/testutil_unity.hpp
tests_test_hwm_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_hwm_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_hwm_pubsub_SOURCES = tests/test_hwm_pubsub.cpp
tests_test_hwm_pubsub_LDADD = src/libzmq.la
......@@ -501,8 +502,9 @@ tests_test_srcfd_LDADD = src/libzmq.la
tests_test_monitor_SOURCES = tests/test_monitor.cpp
tests_test_monitor_LDADD = src/libzmq.la
tests_test_router_mandatory_SOURCES = tests/test_router_mandatory.cpp
tests_test_router_mandatory_LDADD = src/libzmq.la
tests_test_router_mandatory_SOURCES = tests/test_router_mandatory.cpp tests/testutil_unity.hpp
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
......
......@@ -177,7 +177,7 @@ foreach(test ${tests})
add_executable(${test} ${test}.cpp
"testutil_security.hpp")
else ()
add_executable(${test} ${test}.cpp)
add_executable(${test} ${test}.cpp "testutil.hpp" "testutil_unity.hpp")
endif ()
if(WIN32)
# This is the output for Debug dynamic builds on Visual Studio 6.0
......
......@@ -2,27 +2,23 @@
Write your test case as if you were writing clean application code. It should be safe to compile on all platforms.
The only include file you should use is `testutil.hpp`. Do not include files from src. Do not use the internal libzmq API or your test case is fair game to be deleted.
Normally, you should only include the header files from the tests directory, e.g. `testutil.hpp`. Do not include files from src. Do not use the internal libzmq API. Tests for these should be placed in unittests instead.
If you must write non-portable code, wrap it in #ifdefs to ensure it will compile and run on all systems.
Note that testutil.hpp includes platform.h. Do not include it yourself as it changes location depending on the build system and OS.
All sources must contain the correct header. Please copy from test_system.cpp if you're not certain.
All sources must contain the correct copyright header. Please copy from test_system.cpp if you're not certain.
Write new tests using the unity test framework. For an example, see test_sockopt_hwm.
Please use only ANSI C99 in test cases, no C++. This is to make the code more reusable.
On many slower environments, like embedded systems, VMs or CI systems, test might
On many slower environments, like embedded systems, VMs or CI systems, tests might
fail because it takes time for sockets to settle after a connect. If you need
to add a sleep, please be consistent with all the other tests and use:
msleep (SETTLE_TIME);
# Building tests in Windows
According to the version of your compiler, you should adapt the path `libzmq.lib` in the file `tests/CMakeLists.txt`.
Install CMAKE
CMD> CMAKE libzmq/tests
CMD> tests.sln
CMD> # build all projects in the solution
# Building tests in Windows
The tests are only built via cmake, not when using the checked-in Visual Studio .sln files.
......@@ -28,6 +28,17 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
}
void tearDown ()
{
}
const int MAX_SENDS = 10000;
......@@ -37,23 +48,22 @@ enum TestType
CONNECT_FIRST
};
int test_defaults ()
void test_defaults ()
{
void *ctx = zmq_ctx_new ();
assert (ctx);
int rc;
TEST_ASSERT_NOT_NULL (ctx);
// Set up bind socket
void *bind_socket = zmq_socket (ctx, ZMQ_PULL);
assert (bind_socket);
rc = zmq_bind (bind_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (bind_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
// Set up connect socket
void *connect_socket = zmq_socket (ctx, ZMQ_PUSH);
assert (connect_socket);
rc = zmq_connect (connect_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (connect_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
// Send until we block
int send_count = 0;
......@@ -68,65 +78,52 @@ int test_defaults ()
while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
++recv_count;
assert (send_count == recv_count);
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
// Clean up
rc = zmq_close (connect_socket);
assert (rc == 0);
rc = zmq_close (bind_socket);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (connect_socket));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (bind_socket));
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return send_count;
// Default values are 1000 on send and 1000 one receive, so 2000 total
TEST_ASSERT_EQUAL_INT (2000, send_count);
}
int count_msg (int send_hwm, int recv_hwm, TestType testType)
{
void *ctx = zmq_ctx_new ();
assert (ctx);
int rc;
TEST_ASSERT_NOT_NULL (ctx);
void *bind_socket;
void *connect_socket;
if (testType == BIND_FIRST) {
// Set up bind socket
bind_socket = zmq_socket (ctx, ZMQ_PULL);
assert (bind_socket);
rc = zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm,
sizeof (recv_hwm));
assert (rc == 0);
rc = zmq_bind (bind_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (bind_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
// Set up connect socket
connect_socket = zmq_socket (ctx, ZMQ_PUSH);
assert (connect_socket);
rc = zmq_setsockopt (connect_socket, ZMQ_SNDHWM, &send_hwm,
sizeof (send_hwm));
assert (rc == 0);
rc = zmq_connect (connect_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (connect_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
connect_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
} else {
// Set up connect socket
connect_socket = zmq_socket (ctx, ZMQ_PUSH);
assert (connect_socket);
rc = zmq_setsockopt (connect_socket, ZMQ_SNDHWM, &send_hwm,
sizeof (send_hwm));
assert (rc == 0);
rc = zmq_connect (connect_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (connect_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
connect_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
// Set up bind socket
bind_socket = zmq_socket (ctx, ZMQ_PULL);
assert (bind_socket);
rc = zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm,
sizeof (recv_hwm));
assert (rc == 0);
rc = zmq_bind (bind_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (bind_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
}
// Send until we block
......@@ -140,25 +137,18 @@ int count_msg (int send_hwm, int recv_hwm, TestType testType)
while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
++recv_count;
assert (send_count == recv_count);
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
// Now it should be possible to send one more.
rc = zmq_send (connect_socket, NULL, 0, 0);
assert (rc == 0);
send_string_expect_success (connect_socket, NULL, 0);
// Consume the remaining message.
rc = zmq_recv (bind_socket, NULL, 0, 0);
assert (rc == 0);
recv_string_expect_success (bind_socket, NULL, 0);
// Clean up
rc = zmq_close (connect_socket);
assert (rc == 0);
rc = zmq_close (bind_socket);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (connect_socket));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (bind_socket));
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
return send_count;
}
......@@ -176,17 +166,14 @@ int test_inproc_connect_first (int send_hwm, int recv_hwm)
int test_inproc_connect_and_close_first (int send_hwm, int recv_hwm)
{
void *ctx = zmq_ctx_new ();
assert (ctx);
int rc;
TEST_ASSERT_NOT_NULL (ctx);
// Set up connect socket
void *connect_socket = zmq_socket (ctx, ZMQ_PUSH);
assert (connect_socket);
rc =
zmq_setsockopt (connect_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm));
assert (rc == 0);
rc = zmq_connect (connect_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (connect_socket);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_SNDHWM,
&send_hwm, sizeof (send_hwm)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
// Send until we block
int send_count = 0;
......@@ -195,30 +182,25 @@ int test_inproc_connect_and_close_first (int send_hwm, int recv_hwm)
++send_count;
// Close connect
rc = zmq_close (connect_socket);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (connect_socket));
// Set up bind socket
void *bind_socket = zmq_socket (ctx, ZMQ_PULL);
assert (bind_socket);
rc = zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm));
assert (rc == 0);
rc = zmq_bind (bind_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (bind_socket);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
// Now receive all sent messages
int recv_count = 0;
while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
++recv_count;
assert (send_count == recv_count);
TEST_ASSERT_EQUAL_INT (send_count, recv_count);
// Clean up
rc = zmq_close (bind_socket);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (bind_socket));
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
return send_count;
}
......@@ -226,16 +208,14 @@ int test_inproc_connect_and_close_first (int send_hwm, int recv_hwm)
int test_inproc_bind_and_close_first (int send_hwm, int /* recv_hwm */)
{
void *ctx = zmq_ctx_new ();
assert (ctx);
int rc;
TEST_ASSERT_NOT_NULL (ctx);
// Set up bind socket
void *bind_socket = zmq_socket (ctx, ZMQ_PUSH);
assert (bind_socket);
rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm));
assert (rc == 0);
rc = zmq_bind (bind_socket, "inproc://a");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (bind_socket);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
// Send until we block
int send_count = 0;
......@@ -244,78 +224,116 @@ int test_inproc_bind_and_close_first (int send_hwm, int /* recv_hwm */)
++send_count;
// Close bind
rc = zmq_close (bind_socket);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (bind_socket));
/* Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
/* TODO Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
// Set up connect socket
void *connect_socket = zmq_socket (ctx, ZMQ_PULL);
assert (connect_socket);
rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm));
assert (rc == 0);
rc = zmq_connect (connect_socket, "inproc://a");
assert (rc == 0);
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"));
// Now receive all sent messages
int recv_count = 0;
while (zmq_recv (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
++recv_count;
assert (send_count == recv_count);
TEST_ASSERT_EQUAL_INT(send_count, recv_count);
*/
// Clean up
//rc = zmq_close (connect_socket);
//assert (rc == 0);
//TEST_ASSERT_SUCCESS_ERRNO (zmq_close (connect_socket));
rc = zmq_ctx_term (ctx);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
return send_count;
}
int main (void)
void test_infinite_both_inproc_bind_first ()
{
setup_test_environment ();
int count = test_inproc_bind_first (0, 0);
TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}
int count;
void test_infinite_both_inproc_connect_first ()
{
int count = test_inproc_connect_first (0, 0);
TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}
// Default values are 1000 on send and 1000 one receive, so 2000 total
count = test_defaults ();
assert (count == 2000);
// Infinite send and receive buffer
count = test_inproc_bind_first (0, 0);
assert (count == MAX_SENDS);
count = test_inproc_connect_first (0, 0);
assert (count == MAX_SENDS);
// Infinite receive buffer
count = test_inproc_bind_first (1, 0);
assert (count == MAX_SENDS);
count = test_inproc_connect_first (1, 0);
assert (count == MAX_SENDS);
// Infinite send buffer
count = test_inproc_bind_first (0, 1);
assert (count == MAX_SENDS);
count = test_inproc_connect_first (0, 1);
assert (count == MAX_SENDS);
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);
}
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 ()
{
// Send and recv buffers hwm 1, so total that can be queued is 2
count = test_inproc_bind_first (1, 1);
assert (count == 2);
count = test_inproc_connect_first (1, 1);
assert (count == 2);
int count = test_inproc_connect_first (1, 1);
TEST_ASSERT_EQUAL_INT (2, count);
}
void test_infinite_recv_connect_and_close_first ()
{
// Send hwm of 1, send before bind so total that can be queued is 1
count = test_inproc_connect_and_close_first (1, 0);
assert (count == 1);
int count = test_inproc_connect_and_close_first (1, 0);
TEST_ASSERT_EQUAL_INT (1, count);
}
void test_infinite_recv_bind_and_close_first ()
{
// 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?
count = test_inproc_bind_and_close_first (1, 0);
//assert (count == 1);
/*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);
return 0;
return UNITY_END ();
}
......@@ -28,20 +28,27 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void setUp ()
{
}
void tearDown ()
{
}
#ifdef ZMQ_BUILD_DRAFT_API
bool send_msg_to_peer_if_ready (void *router, const char *peer_routing_id)
{
int rc = zmq_socket_get_peer_state (router, peer_routing_id, 1);
if (rc == -1)
printf ("zmq_socket_get_peer_state failed for %s: %i\n",
peer_routing_id, errno);
assert (rc != -1);
int rc = TEST_ASSERT_SUCCESS_MESSAGE_ERRNO (
zmq_socket_get_peer_state (router, peer_routing_id, 1), peer_routing_id);
if (rc & ZMQ_POLLOUT) {
rc = zmq_send (router, peer_routing_id, 1, ZMQ_SNDMORE | ZMQ_DONTWAIT);
assert (rc == 1);
rc = zmq_send (router, "Hello", 5, ZMQ_DONTWAIT);
assert (rc == 5);
send_string_expect_success (router, peer_routing_id,
ZMQ_SNDMORE | ZMQ_DONTWAIT);
send_string_expect_success (router, "Hello", ZMQ_DONTWAIT);
return true;
}
......@@ -53,75 +60,62 @@ void test_get_peer_state ()
{
#ifdef ZMQ_BUILD_DRAFT_API
void *ctx = zmq_ctx_new ();
assert (ctx);
TEST_ASSERT_NOT_NULL (ctx);
void *router = zmq_socket (ctx, ZMQ_ROUTER);
assert (router);
TEST_ASSERT_NOT_NULL (router);
int rc;
int mandatory = 1;
rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &mandatory,
sizeof (mandatory));
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
&mandatory, sizeof (mandatory)));
const char *my_endpoint = "inproc://test_get_peer_state";
rc = zmq_bind (router, my_endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, my_endpoint));
void *dealer1 = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer1);
TEST_ASSERT_NOT_NULL (dealer1);
void *dealer2 = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer2);
TEST_ASSERT_NOT_NULL (dealer2);
// Lower HWMs to allow doing the test with fewer messages
int hwm = 100;
rc = zmq_setsockopt (router, ZMQ_SNDHWM, &hwm, sizeof (int));
assert (rc == 0);
rc = zmq_setsockopt (dealer1, ZMQ_RCVHWM, &hwm, sizeof (int));
assert (rc == 0);
rc = zmq_setsockopt (dealer2, ZMQ_RCVHWM, &hwm, sizeof (int));
assert (rc == 0);
const int hwm = 100;
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (router, ZMQ_SNDHWM, &hwm, sizeof (int)));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer1, ZMQ_RCVHWM, &hwm, sizeof (int)));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer2, ZMQ_RCVHWM, &hwm, sizeof (int)));
const char *dealer1_routing_id = "X";
const char *dealer2_routing_id = "Y";
// Name dealer1 "X" and connect it to our router
rc = zmq_setsockopt (dealer1, ZMQ_ROUTING_ID, dealer1_routing_id, 1);
assert (rc == 0);
rc = zmq_connect (dealer1, my_endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer1, ZMQ_ROUTING_ID, dealer1_routing_id, 1));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer1, my_endpoint));
// Name dealer2 "Y" and connect it to our router
rc = zmq_setsockopt (dealer2, ZMQ_ROUTING_ID, dealer2_routing_id, 1);
assert (rc == 0);
rc = zmq_connect (dealer2, my_endpoint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (dealer2, ZMQ_ROUTING_ID, dealer2_routing_id, 1));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer2, my_endpoint));
// Get message from both dealers to know when connection is ready
char buffer[255];
rc = zmq_send (dealer1, "Hello", 5, 0);
assert (rc == 5);
rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 1);
assert (0 == memcmp (buffer, dealer1_routing_id, rc));
rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 5);
rc = zmq_send (dealer2, "Hello", 5, 0);
assert (rc == 5);
rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 1);
assert (0 == memcmp (buffer, dealer2_routing_id, rc));
rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 5);
send_string_expect_success (dealer1, "Hello", 0);
recv_string_expect_success (router, dealer1_routing_id, 0);
recv_string_expect_success (router, "Hello", 0);
send_string_expect_success (dealer2, "Hello", 0);
recv_string_expect_success (router, dealer2_routing_id, 0);
recv_string_expect_success (router, "Hello", 0);
void *poller = zmq_poller_new ();
assert (poller);
TEST_ASSERT_NOT_NULL (poller);
// Poll on router and dealer1, but not on dealer2
rc = zmq_poller_add (poller, router, NULL, ZMQ_POLLOUT);
assert (rc == 0);
rc = zmq_poller_add (poller, dealer1, NULL, ZMQ_POLLIN);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_poller_add (poller, router, NULL, ZMQ_POLLOUT));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_poller_add (poller, dealer1, NULL, ZMQ_POLLIN));
const unsigned int count = 10000;
const unsigned int event_size = 2;
......@@ -129,8 +123,8 @@ void test_get_peer_state ()
unsigned int dealer1_sent = 0, dealer2_sent = 0, dealer1_received = 0;
zmq_poller_event_t events[event_size];
for (unsigned int iteration = 0; iteration < count; ++iteration) {
rc = zmq_poller_wait_all (poller, events, event_size, -1);
assert (rc != -1);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_poller_wait_all (poller, events, event_size, -1));
for (unsigned int event_no = 0; event_no < event_size; ++event_no) {
const zmq_poller_event_t &current_event = events[event_no];
if (current_event.socket == router
......@@ -145,13 +139,12 @@ void test_get_peer_state ()
}
if (current_event.socket == dealer1
&& current_event.events & ZMQ_POLLIN) {
rc = zmq_recv (dealer1, buffer, 255, ZMQ_DONTWAIT);
assert (rc == 5);
recv_string_expect_success (dealer1, "Hello", ZMQ_DONTWAIT);
int more;
size_t more_size = sizeof (more);
rc = zmq_getsockopt (dealer1, ZMQ_RCVMORE, &more, &more_size);
assert (rc == 0);
assert (!more);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (dealer1, ZMQ_RCVMORE, &more, &more_size));
TEST_ASSERT_FALSE (more);
++dealer1_received;
}
......@@ -160,20 +153,13 @@ void test_get_peer_state ()
}
printf ("dealer1_sent = %u, dealer2_sent = %u, dealer1_received = %u\n",
dealer1_sent, dealer2_sent, dealer1_received);
assert (dealer2_blocked);
TEST_ASSERT_TRUE (dealer2_blocked);
zmq_poller_destroy (&poller);
rc = zmq_close (router);
assert (rc == 0);
rc = zmq_close (dealer1);
assert (rc == 0);
rc = zmq_close (dealer2);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (router));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (dealer1));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (dealer2));
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
#endif
}
......@@ -185,34 +171,31 @@ void test_get_peer_state_corner_cases ()
// call get_peer_state with NULL socket
int rc = zmq_socket_get_peer_state (NULL, peer_routing_id,
strlen (peer_routing_id));
assert (rc == -1 && errno == ENOTSOCK);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno);
void *ctx = zmq_ctx_new ();
assert (ctx);
TEST_ASSERT_NOT_NULL (ctx);
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer);
TEST_ASSERT_NOT_NULL (dealer);
void *router = zmq_socket (ctx, ZMQ_ROUTER);
assert (router);
TEST_ASSERT_NOT_NULL (router);
// call get_peer_state with a non-ROUTER socket
rc = zmq_socket_get_peer_state (dealer, peer_routing_id,
strlen (peer_routing_id));
assert (rc == -1 && errno == ENOTSUP);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSUP, errno);
// call get_peer_state for an unknown routing id
rc = zmq_socket_get_peer_state (router, peer_routing_id,
strlen (peer_routing_id));
assert (rc == -1 && errno == EHOSTUNREACH);
rc = zmq_close (router);
assert (rc == 0);
rc = zmq_close (dealer);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (router));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (dealer));
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
#endif
}
......@@ -221,72 +204,56 @@ void test_basic ()
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *ctx = zmq_ctx_new ();
assert (ctx);
TEST_ASSERT_NOT_NULL (ctx);
void *router = zmq_socket (ctx, ZMQ_ROUTER);
assert (router);
int rc = zmq_bind (router, "tcp://127.0.0.1:*");
assert (rc == 0);
TEST_ASSERT_NOT_NULL (router);
rc = zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, "tcp://127.0.0.1:*"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
// Send a message to an unknown peer with the default setting
// This will not report any error
rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
assert (rc == 7);
rc = zmq_send (router, "DATA", 4, 0);
assert (rc == 4);
send_string_expect_success (router, "UNKNOWN", ZMQ_SNDMORE);
send_string_expect_success (router, "DATA", 0);
// Send a message to an unknown peer with mandatory routing
// This will fail
int mandatory = 1;
rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &mandatory,
sizeof (mandatory));
assert (rc == 0);
rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
assert (rc == -1 && errno == EHOSTUNREACH);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
&mandatory, sizeof (mandatory)));
int rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
// Create dealer called "X" and connect it to our router
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer);
rc = zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1);
assert (rc == 0);
rc = zmq_connect (dealer, my_endpoint);
assert (rc == 0);
TEST_ASSERT_NOT_NULL (dealer);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
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);
// Send a message to connected dealer now
// It should work
rc = zmq_send (router, "X", 1, ZMQ_SNDMORE);
assert (rc == 1);
rc = zmq_send (router, "Hello", 5, 0);
assert (rc == 5);
rc = zmq_close (router);
assert (rc == 0);
rc = zmq_close (dealer);
assert (rc == 0);
send_string_expect_success (router, "X", ZMQ_SNDMORE);
send_string_expect_success (router, "Hello", 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (router));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (dealer));
TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
}
int main (void)
{
setup_test_environment ();
test_basic ();
test_get_peer_state ();
test_get_peer_state_corner_cases ();
UNITY_BEGIN ();
RUN_TEST (test_basic);
RUN_TEST (test_get_peer_state);
RUN_TEST (test_get_peer_state_corner_cases);
return 0;
return UNITY_END ();
}
#pragma once
/*
Copyright (c) 2018 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 "../include/zmq.h"
#include <unity.h>
#include <string.h>
#include <stdio.h>
#if defined(_MSC_VER) && _MSC_VER <= 1800
#define snprintf _snprintf
#endif
int test_assert_success_message_errno_helper (int rc,
const char *msg,
const char *expr)
{
if (rc == -1) {
char buffer[512];
buffer[sizeof (buffer) - 1] =
0; // to ensure defined behavior with VC++ <= 2013
snprintf (buffer, sizeof (buffer) - 1,
"%s failed%s%s%s, errno = %i (%s)", expr,
msg ? " (additional info: " : "", msg ? msg : "",
msg ? ")" : "", zmq_errno (), zmq_strerror (zmq_errno ()));
TEST_FAIL_MESSAGE (buffer);
}
return rc;
}
#define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg) \
test_assert_success_message_errno_helper (expr, msg, #expr)
#define TEST_ASSERT_SUCCESS_ERRNO(expr) \
test_assert_success_message_errno_helper (expr, NULL, #expr)
void send_string_expect_success (void *socket, const char *str, int flags)
{
const size_t len = str ? strlen (str) : 0;
const int rc = zmq_send (socket, str, len, flags);
TEST_ASSERT_EQUAL_INT ((int) len, rc);
}
void recv_string_expect_success (void *socket, const char *str, int flags)
{
const size_t len = str ? strlen (str) : 0;
char buffer[255];
TEST_ASSERT_LESS_OR_EQUAL_MESSAGE (sizeof (buffer), len,
"recv_string_expect_success cannot be "
"used for strings longer than 255 "
"characters");
const int rc =
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (socket, buffer, sizeof (buffer), 0));
TEST_ASSERT_EQUAL_INT ((int) len, rc);
if (str)
TEST_ASSERT_EQUAL_STRING_LEN (str, buffer, len);
}
......@@ -48,7 +48,6 @@ foreach(test ${unittests})
# TODO prevent libzmq (non-static) being in the list of link libraries at all
get_target_property(LIBS ${test} LINK_LIBRARIES)
list(REMOVE_ITEM LIBS libzmq)
message("Link libraries of ${test}: ${LIBS}")
set_target_properties(${test} PROPERTIES LINK_LIBRARIES "${LIBS}")
endforeach()
......
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