Commit 309fe2e6 authored by Simon Giesecke's avatar Simon Giesecke

Problem: test_router_mandatory_hwm not yet using unity

Solution: migrate to unity
parent fd27324e
...@@ -533,7 +533,8 @@ tests_test_router_mandatory_LDADD = src/libzmq.la ${UNITY_LIBS} ...@@ -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_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_router_mandatory_hwm_SOURCES = tests/test_router_mandatory_hwm.cpp 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_SOURCES = tests/test_router_handover.cpp
tests_test_router_handover_LDADD = src/libzmq.la ${UNITY_LIBS} tests_test_router_handover_LDADD = src/libzmq.la ${UNITY_LIBS}
......
...@@ -28,6 +28,17 @@ ...@@ -28,6 +28,17 @@
*/ */
#include "testutil.hpp" #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 // 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 // error when it is defined in the build configuration. It appears that the
...@@ -36,101 +47,82 @@ ...@@ -36,101 +47,82 @@
//#define DEBUG 0 //#define DEBUG 0
#define TRACE_ENABLED 0 #define TRACE_ENABLED 0
int main (void) void test_router_mandatory_hwm ()
{ {
int rc;
if (TRACE_ENABLED) if (TRACE_ENABLED)
fprintf (stderr, "Staring router mandatory HWM test ...\n"); fprintf (stderr, "Staring router mandatory HWM test ...\n");
setup_test_environment ();
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
void *ctx = zmq_ctx_new (); void *router = test_context_socket (ZMQ_ROUTER);
assert (ctx);
void *router = zmq_socket (ctx, ZMQ_ROUTER);
assert (router);
// Configure router socket to mandatory routing and set HWM and linger // Configure router socket to mandatory routing and set HWM and linger
int mandatory = 1; int mandatory = 1;
rc = zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, &mandatory, TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
sizeof (mandatory)); &mandatory, sizeof (mandatory)));
assert (rc == 0);
int sndhwm = 1; int sndhwm = 1;
rc = zmq_setsockopt (router, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (router, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm)));
int linger = 1; int linger = 1;
rc = zmq_setsockopt (router, ZMQ_LINGER, &linger, sizeof (linger)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (router, ZMQ_LINGER, &linger, sizeof (linger)));
rc = zmq_bind (router, "tcp://127.0.0.1:*"); bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
assert (rc == 0);
rc = zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
// Create dealer called "X" and connect it to our router, configure HWM // Create dealer called "X" and connect it to our router, configure HWM
void *dealer = zmq_socket (ctx, ZMQ_DEALER); void *dealer = test_context_socket (ZMQ_DEALER);
assert (dealer); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
rc = zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1);
assert (rc == 0);
int rcvhwm = 1; int rcvhwm = 1;
rc = zmq_setsockopt (dealer, ZMQ_RCVHWM, &rcvhwm, sizeof (rcvhwm)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (dealer, ZMQ_RCVHWM, &rcvhwm, sizeof (rcvhwm)));
rc = zmq_connect (dealer, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
assert (rc == 0);
// Get message from dealer to know when connection is ready // Get message from dealer to know when connection is ready
char buffer[255]; send_string_expect_success (dealer, "Hello", 0);
rc = zmq_send (dealer, "Hello", 5, 0); recv_string_expect_success (router, "X", 0);
assert (rc == 5);
rc = zmq_recv (router, buffer, 255, 0);
assert (rc == 1);
assert (buffer[0] == 'X');
int i; int i;
const int buf_size = 65536; const int buf_size = 65536;
char buf[buf_size]; const uint8_t buf[buf_size] = {0};
memset (buf, 0, buf_size);
// Send first batch of messages // Send first batch of messages
for (i = 0; i < 100000; ++i) { for (i = 0; i < 100000; ++i) {
if (TRACE_ENABLED) if (TRACE_ENABLED)
fprintf (stderr, "Sending message %d ...\n", i); 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) if (rc == -1 && zmq_errno () == EAGAIN)
break; break;
assert (rc == 1); TEST_ASSERT_EQUAL_INT (1, rc);
rc = zmq_send (router, buf, buf_size, ZMQ_DONTWAIT); send_array_expect_success (router, buf, ZMQ_DONTWAIT);
assert (rc == buf_size);
} }
// This should fail after one message but kernel buffering could // This should fail after one message but kernel buffering could
// skew results // skew results
assert (i < 10); TEST_ASSERT_LESS_THAN_INT (10, i);
msleep (1000); msleep (1000);
// Send second batch of messages // Send second batch of messages
for (; i < 100000; ++i) { for (; i < 100000; ++i) {
if (TRACE_ENABLED) if (TRACE_ENABLED)
fprintf (stderr, "Sending message %d (part 2) ...\n", i); 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) if (rc == -1 && zmq_errno () == EAGAIN)
break; break;
assert (rc == 1); TEST_ASSERT_EQUAL_INT (1, rc);
rc = zmq_send (router, buf, buf_size, ZMQ_DONTWAIT); send_array_expect_success (router, buf, ZMQ_DONTWAIT);
assert (rc == buf_size);
} }
// This should fail after two messages but kernel buffering could // This should fail after two messages but kernel buffering could
// skew results // skew results
assert (i < 20); TEST_ASSERT_LESS_THAN_INT (20, i);
if (TRACE_ENABLED) if (TRACE_ENABLED)
fprintf (stderr, "Done sending messages.\n"); fprintf (stderr, "Done sending messages.\n");
rc = zmq_close (router); test_context_socket_close (router);
assert (rc == 0); test_context_socket_close (dealer);
}
rc = zmq_close (dealer);
assert (rc == 0);
rc = zmq_ctx_term (ctx); int main ()
assert (rc == 0); {
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test_router_mandatory_hwm);
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