diff --git a/Makefile.am b/Makefile.am index a674e55c..661986a5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -865,7 +865,8 @@ if !VALGRIND_ENABLED test_apps += tests/test_fork tests_test_fork_SOURCES = tests/test_fork.cpp -tests_test_fork_LDADD = src/libzmq.la +tests_test_fork_LDADD = src/libzmq.la ${UNITY_LIBS} +tests_test_fork_CPPFLAGS = ${UNITY_CPPFLAGS} endif endif diff --git a/tests/test_fork.cpp b/tests/test_fork.cpp index 4dbe7853..156f8409 100644 --- a/tests/test_fork.cpp +++ b/tests/test_fork.cpp @@ -28,41 +28,42 @@ */ #include "testutil.hpp" +#include "testutil_unity.hpp" + +void setUp () +{ + setup_test_context (); +} + +void tearDown () +{ + teardown_test_context (); +} -const char *address = "tcp://127.0.0.1:*"; char connect_address[MAX_SOCKET_STRING]; #define NUM_MESSAGES 5 -int main (void) +void test_fork () { #if !defined(ZMQ_HAVE_WINDOWS) - setup_test_environment (); - void *ctx = zmq_ctx_new (); - assert (ctx); - // Create and bind pull socket to receive messages - void *pull = zmq_socket (ctx, ZMQ_PULL); - assert (pull); - int rc = zmq_bind (pull, address); - assert (rc == 0); - size_t len = MAX_SOCKET_STRING; - rc = zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, connect_address, &len); - assert (rc == 0); + void *pull = test_context_socket (ZMQ_PULL); + bind_loopback_ipv4 (pull, connect_address, sizeof connect_address); int pid = fork (); if (pid == 0) { // Child process // Immediately close parent sockets and context zmq_close (pull); - zmq_ctx_term (ctx); + zmq_ctx_term (get_test_context ()); // Create new context, socket, connect and send some messages void *child_ctx = zmq_ctx_new (); assert (child_ctx); void *push = zmq_socket (child_ctx, ZMQ_PUSH); assert (push); - rc = zmq_connect (push, connect_address); + int rc = zmq_connect (push, connect_address); assert (rc == 0); int count; for (count = 0; count < NUM_MESSAGES; count++) @@ -75,24 +76,28 @@ int main (void) // Parent process int count; for (count = 0; count < NUM_MESSAGES; count++) { - char buffer[5]; - int num_bytes = zmq_recv (pull, buffer, 5, 0); - assert (num_bytes == 5); + recv_string_expect_success (pull, "Hello", 0); } int child_status; while (true) { - rc = waitpid (pid, &child_status, 0); + int rc = waitpid (pid, &child_status, 0); if (rc == -1 && errno == EINTR) continue; - assert (rc > 0); + TEST_ASSERT_GREATER_THAN (0, rc); // Verify the status code of the child was zero - assert (WEXITSTATUS (child_status) == 0); + TEST_ASSERT_EQUAL (0, WEXITSTATUS (child_status)); break; } - zmq_close (pull); - zmq_ctx_term (ctx); - exit (0); + test_context_socket_close (pull); } #endif - return 0; +} + +int main (void) +{ + setup_test_environment (); + + UNITY_BEGIN (); + RUN_TEST (test_fork); + return UNITY_END (); }