0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 15:41:05 +08:00

Problem: test_xpub_nodrop not using test framework

Solution: migrate to unity
This commit is contained in:
Simon Giesecke 2018-05-22 11:12:15 +02:00
parent f13377de05
commit ad100b0b9f
2 changed files with 42 additions and 40 deletions

View File

@ -630,7 +630,8 @@ tests_test_capabilities_SOURCES = tests/test_capabilities.cpp
tests_test_capabilities_LDADD = src/libzmq.la tests_test_capabilities_LDADD = src/libzmq.la
tests_test_xpub_nodrop_SOURCES = tests/test_xpub_nodrop.cpp tests_test_xpub_nodrop_SOURCES = tests/test_xpub_nodrop.cpp
tests_test_xpub_nodrop_LDADD = src/libzmq.la tests_test_xpub_nodrop_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_xpub_nodrop_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_xpub_manual_SOURCES = tests/test_xpub_manual.cpp tests_test_xpub_manual_SOURCES = tests/test_xpub_manual.cpp
tests_test_xpub_manual_LDADD = src/libzmq.la tests_test_xpub_manual_LDADD = src/libzmq.la

View File

@ -28,69 +28,68 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
int main (void) void setUp ()
{ {
setup_test_environment (); setup_test_context ();
void *ctx = zmq_ctx_new (); }
assert (ctx);
void tearDown ()
{
teardown_test_context ();
}
void test ()
{
// Create a publisher // Create a publisher
void *pub = zmq_socket (ctx, ZMQ_PUB); void *pub = test_context_socket (ZMQ_PUB);
assert (pub);
int hwm = 2000; int hwm = 2000;
int rc = zmq_setsockopt (pub, ZMQ_SNDHWM, &hwm, 4); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (pub, ZMQ_SNDHWM, &hwm, 4));
assert (rc == 0);
rc = zmq_bind (pub, "inproc://soname"); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pub, "inproc://soname"));
assert (rc == 0);
// set pub socket options // set pub socket options
int wait = 1; int wait = 1;
rc = zmq_setsockopt (pub, ZMQ_XPUB_NODROP, &wait, 4); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (pub, ZMQ_XPUB_NODROP, &wait, 4));
assert (rc == 0);
// Create a subscriber // Create a subscriber
void *sub = zmq_socket (ctx, ZMQ_SUB); void *sub = test_context_socket (ZMQ_SUB);
assert (sub); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sub, "inproc://soname"));
rc = zmq_connect (sub, "inproc://soname");
assert (rc == 0);
// Subscribe for all messages. // Subscribe for all messages.
rc = zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0));
assert (rc == 0);
int hwmlimit = hwm - 1; int hwmlimit = hwm - 1;
int send_count = 0; int send_count = 0;
// Send an empty message // Send an empty message
for (int i = 0; i < hwmlimit; i++) { for (int i = 0; i < hwmlimit; i++) {
rc = zmq_send (pub, NULL, 0, 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_send (pub, NULL, 0, 0));
assert (rc == 0);
send_count++; send_count++;
} }
int recv_count = 0; int recv_count = 0;
do { do {
// Receive the message in the subscriber // Receive the message in the subscriber
rc = zmq_recv (sub, NULL, 0, ZMQ_DONTWAIT); int rc = zmq_recv (sub, NULL, 0, ZMQ_DONTWAIT);
if (rc == -1) if (rc == -1) {
assert (errno == EAGAIN); TEST_ASSERT_EQUAL_INT (EAGAIN, errno);
else { break;
assert (rc == 0); } else {
TEST_ASSERT_EQUAL_INT (0, rc);
recv_count++; recv_count++;
} }
} while (rc == 0); } while (true);
assert (send_count == recv_count); TEST_ASSERT_EQUAL_INT (send_count, recv_count);
// Now test real blocking behavior // Now test real blocking behavior
// Set a timeout, default is infinite // Set a timeout, default is infinite
int timeout = 0; int timeout = 0;
rc = zmq_setsockopt (pub, ZMQ_SNDTIMEO, &timeout, 4); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (pub, ZMQ_SNDTIMEO, &timeout, 4));
assert (rc == 0);
send_count = 0; send_count = 0;
recv_count = 0; recv_count = 0;
@ -99,19 +98,21 @@ int main (void)
// Send an empty message until we get an error, which must be EAGAIN // Send an empty message until we get an error, which must be EAGAIN
while (zmq_send (pub, "", 0, 0) == 0) while (zmq_send (pub, "", 0, 0) == 0)
send_count++; send_count++;
assert (errno == EAGAIN); TEST_ASSERT_EQUAL_INT (EAGAIN, errno);
while (zmq_recv (sub, NULL, 0, ZMQ_DONTWAIT) == 0) while (zmq_recv (sub, NULL, 0, ZMQ_DONTWAIT) == 0)
recv_count++; recv_count++;
assert (send_count == recv_count); TEST_ASSERT_EQUAL_INT (send_count, recv_count);
// Clean up. // Clean up.
rc = zmq_close (pub); test_context_socket_close (pub);
assert (rc == 0); test_context_socket_close (sub);
rc = zmq_close (sub); }
assert (rc == 0);
rc = zmq_ctx_term (ctx); int main ()
assert (rc == 0); {
setup_test_environment ();
return 0; UNITY_BEGIN ();
RUN_TEST (test);
return UNITY_END ();
} }