mirror of
https://github.com/zeromq/libzmq.git
synced 2025-10-29 12:18:04 +01:00
Problem: tests without test framework
Solution: migrate to Unity
This commit is contained in:
@@ -865,7 +865,8 @@ if !VALGRIND_ENABLED
|
|||||||
test_apps += tests/test_fork
|
test_apps += tests/test_fork
|
||||||
|
|
||||||
tests_test_fork_SOURCES = tests/test_fork.cpp
|
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
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -28,41 +28,42 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "testutil.hpp"
|
#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];
|
char connect_address[MAX_SOCKET_STRING];
|
||||||
|
|
||||||
#define NUM_MESSAGES 5
|
#define NUM_MESSAGES 5
|
||||||
|
|
||||||
int main (void)
|
void test_fork ()
|
||||||
{
|
{
|
||||||
#if !defined(ZMQ_HAVE_WINDOWS)
|
#if !defined(ZMQ_HAVE_WINDOWS)
|
||||||
setup_test_environment ();
|
|
||||||
void *ctx = zmq_ctx_new ();
|
|
||||||
assert (ctx);
|
|
||||||
|
|
||||||
// Create and bind pull socket to receive messages
|
// Create and bind pull socket to receive messages
|
||||||
void *pull = zmq_socket (ctx, ZMQ_PULL);
|
void *pull = test_context_socket (ZMQ_PULL);
|
||||||
assert (pull);
|
bind_loopback_ipv4 (pull, connect_address, sizeof connect_address);
|
||||||
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);
|
|
||||||
|
|
||||||
int pid = fork ();
|
int pid = fork ();
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
// Child process
|
// Child process
|
||||||
// Immediately close parent sockets and context
|
// Immediately close parent sockets and context
|
||||||
zmq_close (pull);
|
zmq_close (pull);
|
||||||
zmq_ctx_term (ctx);
|
zmq_ctx_term (get_test_context ());
|
||||||
|
|
||||||
// Create new context, socket, connect and send some messages
|
// Create new context, socket, connect and send some messages
|
||||||
void *child_ctx = zmq_ctx_new ();
|
void *child_ctx = zmq_ctx_new ();
|
||||||
assert (child_ctx);
|
assert (child_ctx);
|
||||||
void *push = zmq_socket (child_ctx, ZMQ_PUSH);
|
void *push = zmq_socket (child_ctx, ZMQ_PUSH);
|
||||||
assert (push);
|
assert (push);
|
||||||
rc = zmq_connect (push, connect_address);
|
int rc = zmq_connect (push, connect_address);
|
||||||
assert (rc == 0);
|
assert (rc == 0);
|
||||||
int count;
|
int count;
|
||||||
for (count = 0; count < NUM_MESSAGES; count++)
|
for (count = 0; count < NUM_MESSAGES; count++)
|
||||||
@@ -75,24 +76,28 @@ int main (void)
|
|||||||
// Parent process
|
// Parent process
|
||||||
int count;
|
int count;
|
||||||
for (count = 0; count < NUM_MESSAGES; count++) {
|
for (count = 0; count < NUM_MESSAGES; count++) {
|
||||||
char buffer[5];
|
recv_string_expect_success (pull, "Hello", 0);
|
||||||
int num_bytes = zmq_recv (pull, buffer, 5, 0);
|
|
||||||
assert (num_bytes == 5);
|
|
||||||
}
|
}
|
||||||
int child_status;
|
int child_status;
|
||||||
while (true) {
|
while (true) {
|
||||||
rc = waitpid (pid, &child_status, 0);
|
int rc = waitpid (pid, &child_status, 0);
|
||||||
if (rc == -1 && errno == EINTR)
|
if (rc == -1 && errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
assert (rc > 0);
|
TEST_ASSERT_GREATER_THAN (0, rc);
|
||||||
// Verify the status code of the child was zero
|
// Verify the status code of the child was zero
|
||||||
assert (WEXITSTATUS (child_status) == 0);
|
TEST_ASSERT_EQUAL (0, WEXITSTATUS (child_status));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
zmq_close (pull);
|
test_context_socket_close (pull);
|
||||||
zmq_ctx_term (ctx);
|
|
||||||
exit (0);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
}
|
||||||
|
|
||||||
|
int main (void)
|
||||||
|
{
|
||||||
|
setup_test_environment ();
|
||||||
|
|
||||||
|
UNITY_BEGIN ();
|
||||||
|
RUN_TEST (test_fork);
|
||||||
|
return UNITY_END ();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user