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