0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-15 02:07:59 +08:00

Problem: test not using unity assertions and test utils

Solution: Migrate to unity assertions and test utils
This commit is contained in:
Simon Giesecke 2018-08-16 11:55:07 +02:00
parent 75e4a342e2
commit f6a3ebde99

View File

@ -45,8 +45,6 @@ void tearDown ()
void test_send_one_connected_one_unconnected () void test_send_one_connected_one_unconnected ()
{ {
int val; int val;
int rc;
char buffer[16];
// TEST 1. // TEST 1.
// First we're going to attempt to send messages to two // First we're going to attempt to send messages to two
// pipes, one connected, the other not. We should see // pipes, one connected, the other not. We should see
@ -54,68 +52,57 @@ void test_send_one_connected_one_unconnected ()
// 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, "tipc://{6555,0,0}"); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (to, "tipc://{6555,0,0}"));
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 (
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
// This pipe will not connect // This pipe will not connect
rc = zmq_connect (from, "tipc://{5556,0}@0.0.0"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5556,0}@0.0.0"));
assert (rc == 0);
// This pipe will // This pipe will
rc = zmq_connect (from, "tipc://{6555,0}@0.0.0"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{6555,0}@0.0.0"));
assert (rc == 0);
// 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) { const int send_count = 10;
rc = zmq_send (from, "Hello", 5, 0); for (int i = 0; i < send_count; ++i) {
assert (rc == 5); send_string_expect_success (from, "Hello", 0);
} }
// 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 = SETTLE_TIME;
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) {
rc = zmq_recv (to, &buffer, sizeof (buffer), 0); char buffer[16];
if (rc == -1) int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
if (rc == -1) {
TEST_ASSERT_EQUAL_INT (EAGAIN, zmq_errno ());
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 (send_count / 2, 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);
assert (rc == 0);
} }
void test_send_one_connected_one_unconnected_with_delay () void test_send_one_connected_one_unconnected_with_delay ()
{ {
int val; int val;
int rc;
char buffer[16];
// TEST 2 // TEST 2
// This time we will do the same thing, connect two pipes, // This time we will do the same thing, connect two pipes,
@ -124,144 +111,110 @@ void test_send_one_connected_one_unconnected_with_delay ()
// 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.
void *context = zmq_ctx_new ();
// Bind the valid socket // Bind the valid socket
void *to = zmq_socket (context, ZMQ_PULL); void *to = test_context_socket (ZMQ_PULL);
assert (to); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (to, "tipc://{5560,0,0}"));
rc = zmq_bind (to, "tipc://{5560,0,0}");
assert (rc == 0);
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)));
// Create a socket pushing to two endpoints - all messages should arrive. // Create a socket pushing to two endpoints - all messages should arrive.
void *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_DELAY_ATTACH_ON_CONNECT, &val, sizeof (val)); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_setsockopt (from, ZMQ_DELAY_ATTACH_ON_CONNECT, &val, sizeof (val)));
// Connect to the invalid socket // Connect to the invalid socket
rc = zmq_connect (from, "tipc://{5561,0}@0.0.0"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5561,0}@0.0.0"));
assert (rc == 0);
// Connect to the valid socket // Connect to the valid socket
rc = zmq_connect (from, "tipc://{5560,0}@0.0.0"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tipc://{5560,0}@0.0.0"));
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) { const int send_count = 10;
rc = zmq_send (from, "Hello", 5, 0); for (int i = 0; i < send_count; ++i) {
assert (rc == 5); send_string_expect_success (from, "Hello", 0);
} }
int timeout = 250; int timeout = SETTLE_TIME;
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) {
rc = zmq_recv (to, &buffer, sizeof (buffer), 0); char buffer[16];
if (rc == -1) int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
if (rc == -1) {
TEST_ASSERT_EQUAL_INT (EAGAIN, zmq_errno ());
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 (send_count, 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);
assert (rc == 0);
} }
void test_send_disconnected_with_delay () void test_send_disconnected_with_delay ()
{ {
int rc;
char buffer[16];
// TEST 3 // 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.
void *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 DELAY_ATTACH_ON_CONNECT // Frontend connects to backend using DELAY_ATTACH_ON_CONNECT
int on = 1; int on = 1;
rc = TEST_ASSERT_SUCCESS_ERRNO (
zmq_setsockopt (frontend, ZMQ_DELAY_ATTACH_ON_CONNECT, &on, sizeof (on)); zmq_setsockopt (frontend, ZMQ_DELAY_ATTACH_ON_CONNECT, &on, sizeof (on)));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tipc://{5560,0,0}"));
rc = zmq_bind (backend, "tipc://{5560,0,0}"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (frontend, "tipc://{5560,0}@0.0.0"));
assert (rc == 0);
rc = zmq_connect (frontend, "tipc://{5560,0}@0.0.0");
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); msleep (SETTLE_TIME);
// 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, "tipc://{5560,0,0}"));
rc = zmq_bind (backend, "tipc://{5560,0,0}");
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);
rc = zmq_ctx_term (context);
assert (rc == 0);
} }
int main (void) int main (void)