0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-14 01:37:56 +08:00

Problem: tests without test framework

Solution: migrate to Unity
This commit is contained in:
Simon Giesecke 2019-03-22 06:48:49 -04:00
parent f4b9cc994d
commit 924d47f818
6 changed files with 256 additions and 242 deletions

View File

@ -473,7 +473,8 @@ tests_test_ancillaries_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_ancillaries_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_system_SOURCES = tests/test_system.cpp
tests_test_system_LDADD = src/libzmq.la
tests_test_system_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_system_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_pair_inproc_SOURCES = \
tests/test_pair_inproc.cpp \
@ -520,10 +521,12 @@ tests_test_invalid_rep_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_invalid_rep_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_msg_flags_SOURCES = tests/test_msg_flags.cpp
tests_test_msg_flags_LDADD = src/libzmq.la
tests_test_msg_flags_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_msg_flags_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_msg_ffn_SOURCES = tests/test_msg_ffn.cpp
tests_test_msg_ffn_LDADD = src/libzmq.la
tests_test_msg_ffn_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_msg_ffn_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_connect_resolve_SOURCES = tests/test_connect_resolve.cpp
tests_test_connect_resolve_LDADD = src/libzmq.la ${UNITY_LIBS}
@ -673,7 +676,8 @@ tests_test_proxy_terminate_SOURCES = tests/test_proxy_terminate.cpp
tests_test_proxy_terminate_LDADD = src/libzmq.la
tests_test_getsockopt_memset_SOURCES = tests/test_getsockopt_memset.cpp
tests_test_getsockopt_memset_LDADD = src/libzmq.la
tests_test_getsockopt_memset_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_getsockopt_memset_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_many_sockets_SOURCES = tests/test_many_sockets.cpp
tests_test_many_sockets_LDADD = src/libzmq.la
@ -895,7 +899,8 @@ tests_test_reqrep_tipc_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_reqrep_tipc_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_router_mandatory_tipc_SOURCES = tests/test_router_mandatory_tipc.cpp
tests_test_router_mandatory_tipc_LDADD = src/libzmq.la
tests_test_router_mandatory_tipc_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_router_mandatory_tipc_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_shutdown_stress_tipc_SOURCES = tests/test_shutdown_stress_tipc.cpp
tests_test_shutdown_stress_tipc_LDADD = src/libzmq.la ${UNITY_LIBS}

View File

@ -23,42 +23,45 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void)
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void test_getsockopt_memset ()
{
int64_t more;
size_t more_size = sizeof (more);
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb = test_context_socket (ZMQ_PUB);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "inproc://a"));
void *sb = zmq_socket (ctx, ZMQ_PUB);
assert (sb);
int rc = zmq_bind (sb, "inproc://a");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_SUB);
assert (sc);
rc = zmq_connect (sc, "inproc://a");
assert (rc == 0);
void *sc = test_context_socket (ZMQ_SUB);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "inproc://a"));
memset (&more, 0xFF, sizeof (int64_t));
zmq_getsockopt (sc, ZMQ_RCVMORE, &more, &more_size);
assert (more_size == sizeof (int));
assert (more == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sc, ZMQ_RCVMORE, &more, &more_size));
TEST_ASSERT_EQUAL_INT (sizeof (int), more_size);
TEST_ASSERT_EQUAL_INT (0, more);
// Cleanup
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
test_context_socket_close (sc);
test_context_socket_close (sb);
}
int main (void)
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_getsockopt_memset);
return UNITY_END ();
}

View File

@ -28,6 +28,17 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void ffn (void *data_, void *hint_)
{
@ -36,29 +47,16 @@ void ffn (void *data_, void *hint_)
memcpy (hint_, (void *) "freed", 5);
}
int main (void)
void test_msg_ffn ()
{
setup_test_environment ();
// Create the infrastructure
void *ctx = zmq_ctx_new ();
assert (ctx);
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING];
void *router = zmq_socket (ctx, ZMQ_ROUTER);
assert (router);
void *router = test_context_socket (ZMQ_ROUTER);
bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
int rc = zmq_bind (router, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
assert (dealer);
rc = zmq_connect (dealer, my_endpoint);
assert (rc == 0);
void *dealer = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
// Test that creating and closing a message triggers ffn
zmq_msg_t msg;
@ -67,80 +65,68 @@ int main (void)
memset (data, 0, 255);
memcpy (data, (void *) "data", 4);
memcpy (hint, (void *) "hint", 4);
rc = zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint);
assert (rc == 0);
rc = zmq_msg_close (&msg);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
msleep (SETTLE_TIME);
assert (memcmp (hint, "freed", 5) == 0);
TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
memcpy (hint, (void *) "hint", 4);
// Making and closing a copy triggers ffn
zmq_msg_t msg2;
zmq_msg_init (&msg2);
rc = zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint);
assert (rc == 0);
rc = zmq_msg_copy (&msg2, &msg);
assert (rc == 0);
rc = zmq_msg_close (&msg2);
assert (rc == 0);
rc = zmq_msg_close (&msg);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg2, &msg));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg2));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
msleep (SETTLE_TIME);
assert (memcmp (hint, "freed", 5) == 0);
TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
memcpy (hint, (void *) "hint", 4);
// Test that sending a message triggers ffn
rc = zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
zmq_msg_send (&msg, dealer, 0);
char buf[255];
rc = zmq_recv (router, buf, 255, 0);
assert (rc > -1);
rc = zmq_recv (router, buf, 255, 0);
assert (rc == 255);
assert (memcmp (data, buf, 4) == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (router, buf, 255, 0));
TEST_ASSERT_EQUAL_INT (255, zmq_recv (router, buf, 255, 0));
TEST_ASSERT_EQUAL_STRING_LEN (data, buf, 4);
msleep (SETTLE_TIME);
assert (memcmp (hint, "freed", 5) == 0);
TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
memcpy (hint, (void *) "hint", 4);
rc = zmq_msg_close (&msg);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
// Sending a copy of a message triggers ffn
rc = zmq_msg_init (&msg2);
assert (rc == 0);
rc = zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint);
assert (rc == 0);
rc = zmq_msg_copy (&msg2, &msg);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg2));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_data (&msg, (void *) data, 255, ffn, (void *) hint));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg2, &msg));
zmq_msg_send (&msg, dealer, 0);
rc = zmq_recv (router, buf, 255, 0);
assert (rc > -1);
rc = zmq_recv (router, buf, 255, 0);
assert (rc == 255);
assert (memcmp (data, buf, 4) == 0);
rc = zmq_msg_close (&msg2);
assert (rc == 0);
rc = zmq_msg_close (&msg);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (router, buf, 255, 0));
TEST_ASSERT_EQUAL_INT (255, zmq_recv (router, buf, 255, 0));
TEST_ASSERT_EQUAL_STRING_LEN (data, buf, 4);
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg2));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
msleep (SETTLE_TIME);
assert (memcmp (hint, "freed", 5) == 0);
memcpy (hint, (void *) "hint", 4);
TEST_ASSERT_EQUAL_STRING_LEN ("freed", hint, 5);
// Deallocate the infrastructure.
rc = zmq_close (router);
assert (rc == 0);
rc = zmq_close (dealer);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
test_context_socket_close (router);
test_context_socket_close (dealer);
}
int main (void)
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_msg_ffn);
return UNITY_END ();
}

View File

@ -28,99 +28,98 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void)
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void test_more ()
{
setup_test_environment ();
// Create the infrastructure
void *ctx = zmq_ctx_new ();
assert (ctx);
void *sb = test_context_socket (ZMQ_ROUTER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "inproc://a"));
void *sb = zmq_socket (ctx, ZMQ_ROUTER);
assert (sb);
int rc = zmq_bind (sb, "inproc://a");
assert (rc == 0);
void *sc = zmq_socket (ctx, ZMQ_DEALER);
assert (sc);
rc = zmq_connect (sc, "inproc://a");
assert (rc == 0);
void *sc = test_context_socket (ZMQ_DEALER);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, "inproc://a"));
// Send 2-part message.
rc = zmq_send (sc, "A", 1, ZMQ_SNDMORE);
assert (rc == 1);
rc = zmq_send (sc, "B", 1, 0);
assert (rc == 1);
send_string_expect_success (sc, "A", ZMQ_SNDMORE);
send_string_expect_success (sc, "B", 0);
// Routing id comes first.
zmq_msg_t msg;
rc = zmq_msg_init (&msg);
assert (rc == 0);
rc = zmq_msg_recv (&msg, sb, 0);
assert (rc >= 0);
int more = zmq_msg_more (&msg);
assert (more == 1);
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sb, 0));
TEST_ASSERT_EQUAL_INT (1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_more (&msg)));
// Then the first part of the message body.
rc = zmq_msg_recv (&msg, sb, 0);
assert (rc == 1);
more = zmq_msg_more (&msg);
assert (more == 1);
TEST_ASSERT_EQUAL_INT (
1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sb, 0)));
TEST_ASSERT_EQUAL_INT (1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_more (&msg)));
// And finally, the second part of the message body.
rc = zmq_msg_recv (&msg, sb, 0);
assert (rc == 1);
more = zmq_msg_more (&msg);
assert (more == 0);
// Test ZMQ_SHARED property (case 1, refcounted messages)
zmq_msg_t msg_a;
rc = zmq_msg_init_size (&msg_a, 1024); // large enough to be a type_lmsg
assert (rc == 0);
// Message is not shared
rc = zmq_msg_get (&msg_a, ZMQ_SHARED);
assert (rc == 0);
zmq_msg_t msg_b;
rc = zmq_msg_init (&msg_b);
assert (rc == 0);
rc = zmq_msg_copy (&msg_b, &msg_a);
assert (rc == 0);
// Message is now shared
rc = zmq_msg_get (&msg_b, ZMQ_SHARED);
assert (rc == 1);
// cleanup
rc = zmq_msg_close (&msg_a);
assert (rc == 0);
rc = zmq_msg_close (&msg_b);
assert (rc == 0);
// Test ZMQ_SHARED property (case 2, constant data messages)
rc = zmq_msg_init_data (&msg_a, (void *) "TEST", 5, 0, 0);
assert (rc == 0);
// Message reports as shared
rc = zmq_msg_get (&msg_a, ZMQ_SHARED);
assert (rc == 1);
// cleanup
rc = zmq_msg_close (&msg_a);
assert (rc == 0);
TEST_ASSERT_EQUAL_INT (
1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sb, 0)));
TEST_ASSERT_EQUAL_INT (0, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_more (&msg)));
// Deallocate the infrastructure.
rc = zmq_close (sc);
assert (rc == 0);
rc = zmq_close (sb);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
test_context_socket_close (sc);
test_context_socket_close (sb);
}
void test_shared_refcounted ()
{
// Test ZMQ_SHARED property (case 1, refcounted messages)
zmq_msg_t msg_a;
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_size (&msg_a, 1024)); // large enough to be a type_lmsg
// Message is not shared
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_get (&msg_a, ZMQ_SHARED));
zmq_msg_t msg_b;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg_b));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_copy (&msg_b, &msg_a));
// Message is now shared
TEST_ASSERT_EQUAL_INT (
1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_get (&msg_b, ZMQ_SHARED)));
// cleanup
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg_a));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg_b));
}
void test_shared_const ()
{
zmq_msg_t msg_a;
// Test ZMQ_SHARED property (case 2, constant data messages)
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_data (&msg_a, (void *) "TEST", 5, 0, 0));
// Message reports as shared
TEST_ASSERT_EQUAL_INT (
1, TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_get (&msg_a, ZMQ_SHARED)));
// cleanup
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg_a));
}
int main ()
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_more);
RUN_TEST (test_shared_refcounted);
RUN_TEST (test_shared_const);
return UNITY_END ();
}

View File

@ -30,47 +30,49 @@
#include <stdio.h>
#include "testutil.hpp"
int main (void)
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
void test_router_mandatory_tipc ()
{
if (!is_tipc_available ()) {
printf ("TIPC environment unavailable, skipping test\n");
return 77;
TEST_IGNORE_MESSAGE ("TIPC environment unavailable, skipping test");
}
fprintf (stderr, "test_router_mandatory_tipc running...\n");
void *ctx = zmq_init (1);
assert (ctx);
// Creating the first socket.
void *sa = zmq_socket (ctx, ZMQ_ROUTER);
assert (sa);
void *sa = test_context_socket (ZMQ_ROUTER);
int rc = zmq_bind (sa, "tipc://{15560,0,0}");
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sa, "tipc://{15560,0,0}"));
// Sending a message to an unknown peer with the default setting
rc = zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE);
assert (rc == 7);
rc = zmq_send (sa, "DATA", 4, 0);
assert (rc == 4);
send_string_expect_success (sa, "UNKNOWN", ZMQ_SNDMORE);
send_string_expect_success (sa, "DATA", 0);
int mandatory = 1;
// Set mandatory routing on socket
rc =
zmq_setsockopt (sa, ZMQ_ROUTER_MANDATORY, &mandatory, sizeof (mandatory));
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (sa, ZMQ_ROUTER_MANDATORY,
&mandatory, sizeof (mandatory)));
// Send a message and check that it fails
rc = zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE | ZMQ_DONTWAIT);
assert (rc == -1 && errno == EHOSTUNREACH);
TEST_ASSERT_FAILURE_ERRNO (
EHOSTUNREACH, zmq_send (sa, "UNKNOWN", 7, ZMQ_SNDMORE | ZMQ_DONTWAIT));
rc = zmq_close (sa);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
test_context_socket_close (sa);
}
int main (void)
{
UNITY_BEGIN ();
RUN_TEST (test_router_mandatory_tipc);
return UNITY_END ();
}

View File

@ -28,6 +28,7 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#if defined(ZMQ_HAVE_WINDOWS)
#include <winsock2.h>
@ -38,6 +39,16 @@
#include <unistd.h>
#endif
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// Solaris has a default of 256 max files per process
#ifdef ZMQ_HAVE_SOLARIS
#define MAX_SOCKETS 200
@ -62,6 +73,43 @@ void initialise_network (void)
#endif
void test_localhost ()
{
// Check that we have local networking via ZeroMQ
void *dealer = test_context_socket (ZMQ_DEALER);
if (zmq_bind (dealer, "tcp://127.0.0.1:*") == -1) {
TEST_FAIL_MESSAGE (
"E: Cannot find 127.0.0.1 -- your system does not have local\n"
"E: networking. Please fix this before running libzmq checks.\n");
}
test_context_socket_close (dealer);
}
void test_max_sockets ()
{
// Check that we can create 1,000 sockets
fd_t handle[MAX_SOCKETS];
int count;
for (count = 0; count < MAX_SOCKETS; count++) {
handle[count] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (handle[count] == -1) {
printf ("W: Only able to create %d sockets on this box\n", count);
const char msg[] =
"I: Tune your system to increase maximum allowed file handles\n"
#if !defined(ZMQ_HAVE_WINDOWS)
"I: Run 'ulimit -n 1200' in bash\n"
#endif
;
TEST_FAIL_MESSAGE (msg);
}
}
// Release the socket handles
for (count = 0; count < MAX_SOCKETS; count++) {
close (handle[count]);
}
}
// This test case stresses the system to shake out known configuration
// problems. We're direct system calls when necessary. Some code may
// need wrapping to be properly portable.
@ -70,37 +118,8 @@ int main (void)
{
initialise_network ();
// Check that we have local networking via ZeroMQ
void *ctx = zmq_ctx_new ();
assert (ctx);
void *dealer = zmq_socket (ctx, ZMQ_DEALER);
if (zmq_bind (dealer, "tcp://127.0.0.1:*") == -1) {
printf (
"E: Cannot find 127.0.0.1 -- your system does not have local\n");
printf (
"E: networking. Please fix this before running libzmq checks.\n");
return -1;
}
// Check that we can create 1,000 sockets
fd_t handle[MAX_SOCKETS];
int count;
for (count = 0; count < MAX_SOCKETS; count++) {
handle[count] = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (handle[count] == -1) {
printf ("W: Only able to create %d sockets on this box\n", count);
printf (
"I: Tune your system to increase maximum allowed file handles\n");
#if !defined(ZMQ_HAVE_WINDOWS)
printf ("I: Run 'ulimit -n 1200' in bash\n");
#endif
return -1;
}
}
// Release the socket handles
for (count = 0; count < MAX_SOCKETS; count++) {
close (handle[count]);
}
zmq_close (dealer);
zmq_ctx_term (ctx);
UNITY_BEGIN ();
RUN_TEST (test_localhost);
RUN_TEST (test_max_sockets);
return UNITY_END ();
}