Problem: test_immediate not using unity

Solution: migrate to unity, and split test cases
This commit is contained in:
Simon Giesecke 2018-03-16 15:46:39 +01:00
parent 6f8b604648
commit 1747cbdcac
2 changed files with 101 additions and 119 deletions

View File

@ -489,7 +489,8 @@ tests_test_connect_resolve_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_connect_resolve_CPPFLAGS = ${UNITY_CPPFLAGS} tests_test_connect_resolve_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_immediate_SOURCES = tests/test_immediate.cpp tests_test_immediate_SOURCES = tests/test_immediate.cpp
tests_test_immediate_LDADD = src/libzmq.la tests_test_immediate_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_immediate_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_last_endpoint_SOURCES = tests/test_last_endpoint.cpp tests_test_last_endpoint_SOURCES = tests/test_last_endpoint.cpp
tests_test_last_endpoint_LDADD = src/libzmq.la tests_test_last_endpoint_LDADD = src/libzmq.la

View File

@ -28,10 +28,22 @@
*/ */
#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_immediate_1 ()
{ {
setup_test_environment ();
int val; int val;
int rc; int rc;
char buffer[16]; char buffer[16];
@ -44,47 +56,38 @@ int main (void)
// of the messages getting queued, as connect() creates a // of the messages getting queued, as connect() creates a
// pipe immediately. // pipe immediately.
void *context = zmq_ctx_new (); void *to = test_context_socket (ZMQ_PULL);
assert (context);
void *to = zmq_socket (context, ZMQ_PULL);
assert (to);
// Bind the one valid receiver // Bind the one valid receiver
val = 0; val = 0;
rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
rc = zmq_bind (to, "tcp://127.0.0.1:*"); bind_loopback_ipv4 (to, my_endpoint, len);
assert (rc == 0);
rc = zmq_getsockopt (to, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
// Create a socket pushing to two endpoints - only 1 message should arrive. // Create a socket pushing to two endpoints - only 1 message should arrive.
void *from = zmq_socket (context, ZMQ_PUSH); void *from = test_context_socket (ZMQ_PUSH);
assert (from);
val = 0; val = 0;
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)); TEST_ASSERT_SUCCESS_ERRNO (
// This pipe will not connect zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
rc = zmq_connect (from, "tcp://localhost:5556"); // This pipe will not connect (provided the ephemeral port is not 5556)
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tcp://localhost:5556"));
// This pipe will // This pipe will
rc = zmq_connect (from, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, my_endpoint));
assert (rc == 0);
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
// We send 10 messages, 5 should just get stuck in the queue // We send 10 messages, 5 should just get stuck in the queue
// for the not-yet-connected pipe // for the not-yet-connected pipe
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
rc = zmq_send (from, "Hello", 5, 0); send_string_expect_success (from, "Hello", 0);
assert (rc == 5);
} }
// We now consume from the connected pipe // We now consume from the connected pipe
// - we should see just 5 // - we should see just 5
int timeout = 250; int timeout = 250;
rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
int seen = 0; int seen = 0;
while (true) { while (true) {
@ -93,158 +96,136 @@ int main (void)
break; // Break when we didn't get a message break; // Break when we didn't get a message
seen++; seen++;
} }
assert (seen == 5); TEST_ASSERT_EQUAL_INT (5, seen);
rc = zmq_close (from); test_context_socket_close (from);
assert (rc == 0); test_context_socket_close (to);
}
rc = zmq_close (to);
assert (rc == 0);
rc = zmq_ctx_term (context); void test_immediate_2 ()
assert (rc == 0); {
// TEST 2
// This time we will do the same thing, connect two pipes, // This time we will do the same thing, connect two pipes,
// one of which will succeed in connecting to a bound // one of which will succeed in connecting to a bound
// receiver, the other of which will fail. However, we will // receiver, the other of which will fail. However, we will
// also set the delay attach on connect flag, which should // also set the delay attach on connect flag, which should
// cause the pipe attachment to be delayed until the connection // cause the pipe attachment to be delayed until the connection
// succeeds. // succeeds.
context = zmq_ctx_new ();
// Bind the valid socket // Bind the valid socket
to = zmq_socket (context, ZMQ_PULL); void *to = test_context_socket (ZMQ_PULL);
assert (to); size_t len = MAX_SOCKET_STRING;
rc = zmq_bind (to, "tcp://127.0.0.1:*"); char my_endpoint[MAX_SOCKET_STRING];
assert (rc == 0); bind_loopback_ipv4 (to, my_endpoint, len);
len = MAX_SOCKET_STRING;
rc = zmq_getsockopt (to, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
val = 0; int val = 0;
rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
// Create a socket pushing to two endpoints - all messages should arrive. // Create a socket pushing to two endpoints - all messages should arrive.
from = zmq_socket (context, ZMQ_PUSH); void *from = test_context_socket (ZMQ_PUSH);
assert (from);
val = 0; val = 0;
rc = zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
// Set the key flag // Set the key flag
val = 1; val = 1;
rc = zmq_setsockopt (from, ZMQ_IMMEDIATE, &val, sizeof (val)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (from, ZMQ_IMMEDIATE, &val, sizeof (val)));
// Connect to the invalid socket // Connect to the invalid socket
rc = zmq_connect (from, "tcp://localhost:5561"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tcp://localhost:5561"));
assert (rc == 0);
// Connect to the valid socket // Connect to the valid socket
rc = zmq_connect (from, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, my_endpoint));
assert (rc == 0);
// Send 10 messages, all should be routed to the connected pipe // Send 10 messages, all should be routed to the connected pipe
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
rc = zmq_send (from, "Hello", 5, 0); send_string_expect_success (from, "Hello", 0);
assert (rc == 5);
} }
rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)); int timeout = 250;
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
seen = 0; int seen = 0;
while (true) { while (true) {
rc = zmq_recv (to, &buffer, sizeof (buffer), 0); char buffer[16];
int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
if (rc == -1) if (rc == -1)
break; // Break when we didn't get a message break; // Break when we didn't get a message
seen++; seen++;
} }
assert (seen == 10); TEST_ASSERT_EQUAL_INT (10, seen);
rc = zmq_close (from); test_context_socket_close (from);
assert (rc == 0); test_context_socket_close (to);
}
rc = zmq_close (to); void test_immediate_3 ()
assert (rc == 0); {
rc = zmq_ctx_term (context);
assert (rc == 0);
// TEST 3
// This time we want to validate that the same blocking behaviour // This time we want to validate that the same blocking behaviour
// occurs with an existing connection that is broken. We will send // occurs with an existing connection that is broken. We will send
// messages to a connected pipe, disconnect and verify the messages // messages to a connected pipe, disconnect and verify the messages
// block. Then we reconnect and verify messages flow again. // block. Then we reconnect and verify messages flow again.
context = zmq_ctx_new (); void *backend = test_context_socket (ZMQ_DEALER);
void *frontend = test_context_socket (ZMQ_DEALER);
void *backend = zmq_socket (context, ZMQ_DEALER);
assert (backend);
void *frontend = zmq_socket (context, ZMQ_DEALER);
assert (frontend);
int zero = 0; int zero = 0;
rc = zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
rc = zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero)));
// Frontend connects to backend using IMMEDIATE // Frontend connects to backend using IMMEDIATE
int on = 1; int on = 1;
rc = zmq_setsockopt (frontend, ZMQ_IMMEDIATE, &on, sizeof (on)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (frontend, ZMQ_IMMEDIATE, &on, sizeof (on)));
rc = zmq_bind (backend, "tcp://127.0.0.1:*");
assert (rc == 0); size_t len = MAX_SOCKET_STRING;
len = MAX_SOCKET_STRING; char my_endpoint[MAX_SOCKET_STRING];
rc = zmq_getsockopt (backend, ZMQ_LAST_ENDPOINT, my_endpoint, &len); bind_loopback_ipv4 (backend, my_endpoint, len);
assert (rc == 0);
rc = zmq_connect (frontend, my_endpoint); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (frontend, my_endpoint));
assert (rc == 0);
// Ping backend to frontend so we know when the connection is up // Ping backend to frontend so we know when the connection is up
rc = zmq_send (backend, "Hello", 5, 0); send_string_expect_success (backend, "Hello", 0);
assert (rc == 5); recv_string_expect_success (frontend, "Hello", 0);
rc = zmq_recv (frontend, buffer, 255, 0);
assert (rc == 5);
// Send message from frontend to backend // Send message from frontend to backend
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT); send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
assert (rc == 5);
rc = zmq_close (backend); test_context_socket_close (backend);
assert (rc == 0);
// Give time to process disconnect // Give time to process disconnect
msleep (SETTLE_TIME * 10); msleep (SETTLE_TIME * 10);
// Send a message, should fail // Send a message, should fail
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT); TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
assert (rc == -1); zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT));
// Recreate backend socket // Recreate backend socket
backend = zmq_socket (context, ZMQ_DEALER); backend = test_context_socket (ZMQ_DEALER);
assert (backend); TEST_ASSERT_SUCCESS_ERRNO (
rc = zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)); zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, my_endpoint));
rc = zmq_bind (backend, my_endpoint);
assert (rc == 0);
// Ping backend to frontend so we know when the connection is up // Ping backend to frontend so we know when the connection is up
rc = zmq_send (backend, "Hello", 5, 0); send_string_expect_success (backend, "Hello", 0);
assert (rc == 5); recv_string_expect_success (frontend, "Hello", 0);
rc = zmq_recv (frontend, buffer, 255, 0);
assert (rc == 5);
// After the reconnect, should succeed // After the reconnect, should succeed
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT); send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
assert (rc == 5);
rc = zmq_close (backend); test_context_socket_close (backend);
assert (rc == 0); test_context_socket_close (frontend);
}
rc = zmq_close (frontend);
assert (rc == 0); int main (void)
{
rc = zmq_ctx_term (context); setup_test_environment ();
assert (rc == 0); UNITY_BEGIN ();
RUN_TEST (test_immediate_1);
RUN_TEST (test_immediate_2);
RUN_TEST (test_immediate_3);
return UNITY_END ();
} }