Commit 6f083df5 authored by Simon Giesecke's avatar Simon Giesecke

Problem: tests without test framework

Solution: migrate to Unity
parent 2f40d216
...@@ -585,7 +585,8 @@ tests_test_stream_disconnect_LDADD = src/libzmq.la ${UNITY_LIBS} ...@@ -585,7 +585,8 @@ tests_test_stream_disconnect_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_stream_disconnect_CPPFLAGS = ${UNITY_CPPFLAGS} tests_test_stream_disconnect_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_disconnect_inproc_SOURCES = tests/test_disconnect_inproc.cpp tests_test_disconnect_inproc_SOURCES = tests/test_disconnect_inproc.cpp
tests_test_disconnect_inproc_LDADD = src/libzmq.la tests_test_disconnect_inproc_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_disconnect_inproc_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_unbind_wildcard_SOURCES = tests/test_unbind_wildcard.cpp tests_test_unbind_wildcard_SOURCES = tests/test_unbind_wildcard.cpp
tests_test_unbind_wildcard_LDADD = src/libzmq.la ${UNITY_LIBS} tests_test_unbind_wildcard_LDADD = src/libzmq.la ${UNITY_LIBS}
......
...@@ -28,46 +28,41 @@ ...@@ -28,46 +28,41 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
/// Initialize a zeromq message with a given null-terminated string /// Initialize a zeromq message with a given null-terminated string
#define ZMQ_PREPARE_STRING(msg, data, size) \ #define ZMQ_PREPARE_STRING(msg, data, size) \
\ TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg)); \
zmq_msg_init (&msg) \ TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, size + 1)); \
&& printf ("zmq_msg_init: %s\n", zmq_strerror (errno)); \
\
zmq_msg_init_size (&msg, size + 1) \
&& printf ("zmq_msg_init_size: %s\n", zmq_strerror (errno)); \
\
memcpy (zmq_msg_data (&msg), data, size + 1); memcpy (zmq_msg_data (&msg), data, size + 1);
// TODO: this code fails to meet our style guidelines, and needs rewriting
static int publicationsReceived = 0; static int publicationsReceived = 0;
static bool isSubscribed = false; static bool isSubscribed = false;
int main (int, char **) void test_disconnect_inproc ()
{ {
setup_test_environment (); void *pub_socket = test_context_socket (ZMQ_XPUB);
void *context = zmq_ctx_new (); void *sub_socket = test_context_socket (ZMQ_SUB);
void *pub_socket; TEST_ASSERT_SUCCESS_ERRNO (
void *sub_socket; zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, "foo", 3));
(pub_socket = zmq_socket (context, ZMQ_XPUB))
|| printf ("zmq_socket: %s\n", zmq_strerror (errno));
(sub_socket = zmq_socket (context, ZMQ_SUB))
|| printf ("zmq_socket: %s\n", zmq_strerror (errno));
zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, "foo", 3)
&& printf ("zmq_setsockopt: %s\n", zmq_strerror (errno));
zmq_bind (pub_socket, "inproc://someInProcDescriptor") TEST_ASSERT_SUCCESS_ERRNO (
&& printf ("zmq_bind: %s\n", zmq_strerror (errno)); zmq_bind (pub_socket, "inproc://someInProcDescriptor"));
//zmq_bind(pubSocket, "tcp://127.0.0.1:30010") && printf("zmq_bind: %s\n", zmq_strerror(errno));
int more; int more;
size_t more_size = sizeof (more); size_t more_size = sizeof (more);
int iteration = 0;
while (1) { for (int iteration = 0;; ++iteration) {
zmq_pollitem_t items[] = { zmq_pollitem_t items[] = {
{sub_socket, 0, ZMQ_POLLIN, 0}, // read publications {sub_socket, 0, ZMQ_POLLIN, 0}, // read publications
{pub_socket, 0, ZMQ_POLLIN, 0}, // read subscriptions {pub_socket, 0, ZMQ_POLLIN, 0}, // read subscriptions
...@@ -75,77 +70,73 @@ int main (int, char **) ...@@ -75,77 +70,73 @@ int main (int, char **)
int rc = zmq_poll (items, 2, 100); int rc = zmq_poll (items, 2, 100);
if (items[1].revents & ZMQ_POLLIN) { if (items[1].revents & ZMQ_POLLIN) {
while (1) { for (more = 1; more;) {
zmq_msg_t msg; zmq_msg_t msg;
zmq_msg_init (&msg); zmq_msg_init (&msg);
zmq_msg_recv (&msg, pub_socket, 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, pub_socket, 0));
char *buffer = (char *) zmq_msg_data (&msg); char *buffer = (char *) zmq_msg_data (&msg);
if (buffer[0] == 0) { if (buffer[0] == 0) {
assert (isSubscribed); TEST_ASSERT_TRUE (isSubscribed);
isSubscribed = false; isSubscribed = false;
} else { } else {
assert (!isSubscribed); TEST_ASSERT_FALSE (isSubscribed);
isSubscribed = true; isSubscribed = true;
} }
zmq_getsockopt (pub_socket, ZMQ_RCVMORE, &more, &more_size); TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_close (&msg); zmq_getsockopt (pub_socket, ZMQ_RCVMORE, &more, &more_size));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
if (!more)
break; // Last message part
} }
} }
if (items[0].revents & ZMQ_POLLIN) { if (items[0].revents & ZMQ_POLLIN) {
while (1) { more = 1;
for (more = 1; more;) {
zmq_msg_t msg; zmq_msg_t msg;
zmq_msg_init (&msg); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
zmq_msg_recv (&msg, sub_socket, 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sub_socket, 0));
zmq_getsockopt (sub_socket, ZMQ_RCVMORE, &more, &more_size); TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_close (&msg); zmq_getsockopt (sub_socket, ZMQ_RCVMORE, &more, &more_size));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
if (!more) {
publicationsReceived++;
break; // Last message part
}
} }
publicationsReceived++;
} }
if (iteration == 1) { if (iteration == 1) {
zmq_connect (sub_socket, "inproc://someInProcDescriptor") TEST_ASSERT_SUCCESS_ERRNO (
&& printf ("zmq_connect: %s\n", zmq_strerror (errno)); zmq_connect (sub_socket, "inproc://someInProcDescriptor"));
msleep (SETTLE_TIME); msleep (SETTLE_TIME);
} }
if (iteration == 4) { if (iteration == 4) {
zmq_disconnect (sub_socket, "inproc://someInProcDescriptor") TEST_ASSERT_SUCCESS_ERRNO (
&& printf ("zmq_disconnect(%d): %s\n", errno, zmq_disconnect (sub_socket, "inproc://someInProcDescriptor"));
zmq_strerror (errno));
} }
if (iteration > 4 && rc == 0) if (iteration > 4 && rc == 0)
break; break;
zmq_msg_t channel_envlp; zmq_msg_t channel_envlp;
ZMQ_PREPARE_STRING (channel_envlp, "foo", 3); ZMQ_PREPARE_STRING (channel_envlp, "foo", 3);
zmq_msg_send (&channel_envlp, pub_socket, ZMQ_SNDMORE) >= 0 TEST_ASSERT_SUCCESS_ERRNO (
|| printf ("zmq_msg_send: %s\n", zmq_strerror (errno)); zmq_msg_send (&channel_envlp, pub_socket, ZMQ_SNDMORE));
zmq_msg_close (&channel_envlp) TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&channel_envlp));
&& printf ("zmq_msg_close: %s\n", zmq_strerror (errno));
zmq_msg_t message; zmq_msg_t message;
ZMQ_PREPARE_STRING (message, "this is foo!", 12); ZMQ_PREPARE_STRING (message, "this is foo!", 12);
zmq_msg_send (&message, pub_socket, 0) >= 0 TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&message, pub_socket, 0));
|| printf ("zmq_msg_send: %s\n", zmq_strerror (errno)); TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&message));
zmq_msg_close (&message)
&& printf ("zmq_msg_close: %s\n", zmq_strerror (errno));
iteration++;
} }
assert (publicationsReceived == 3); TEST_ASSERT_EQUAL_INT (3, publicationsReceived);
assert (!isSubscribed); TEST_ASSERT_FALSE (isSubscribed);
zmq_close (pub_socket) && printf ("zmq_close: %s", zmq_strerror (errno)); test_context_socket_close (pub_socket);
zmq_close (sub_socket) && printf ("zmq_close: %s", zmq_strerror (errno)); test_context_socket_close (sub_socket);
}
int main (int, char **)
{
setup_test_environment ();
zmq_ctx_term (context); UNITY_BEGIN ();
return 0; RUN_TEST (test_disconnect_inproc);
return UNITY_END ();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment