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

Problem: tests without test framework

Solution: migrate to Unity
This commit is contained in:
Simon Giesecke 2019-03-22 11:09:08 -04:00
parent 6ed03e9333
commit 90a4d268d9
3 changed files with 125 additions and 142 deletions

View File

@ -672,10 +672,12 @@ tests_test_proxy_hwm_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_proxy_hwm_CPPFLAGS = ${UNITY_CPPFLAGS} tests_test_proxy_hwm_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_proxy_single_socket_SOURCES = tests/test_proxy_single_socket.cpp tests_test_proxy_single_socket_SOURCES = tests/test_proxy_single_socket.cpp
tests_test_proxy_single_socket_LDADD = src/libzmq.la tests_test_proxy_single_socket_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_proxy_single_socket_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_proxy_terminate_SOURCES = tests/test_proxy_terminate.cpp tests_test_proxy_terminate_SOURCES = tests/test_proxy_terminate.cpp
tests_test_proxy_terminate_LDADD = src/libzmq.la tests_test_proxy_terminate_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_proxy_terminate_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_getsockopt_memset_SOURCES = tests/test_getsockopt_memset.cpp tests_test_getsockopt_memset_SOURCES = tests/test_getsockopt_memset.cpp
tests_test_getsockopt_memset_LDADD = src/libzmq.la ${UNITY_LIBS} tests_test_getsockopt_memset_LDADD = src/libzmq.la ${UNITY_LIBS}

View File

@ -28,92 +28,81 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// This is our server task. // This is our server task.
// It runs a proxy with a single REP socket as both frontend and backend. // It runs a proxy with a single REP socket as both frontend and backend.
void server_task (void *ctx_) void server_task (void * /*unused_*/)
{ {
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
void *rep = zmq_socket (ctx_, ZMQ_REP); void *rep = zmq_socket (get_test_context (), ZMQ_REP);
assert (rep); TEST_ASSERT_NOT_NULL (rep);
int rc = zmq_bind (rep, "tcp://127.0.0.1:*"); bind_loopback_ipv4 (rep, my_endpoint, sizeof my_endpoint);
assert (rc == 0);
rc = zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
// Control socket receives terminate command from main over inproc // Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx_, ZMQ_REQ); void *control = zmq_socket (get_test_context (), ZMQ_REQ);
assert (control); TEST_ASSERT_NOT_NULL (control);
rc = zmq_connect (control, "inproc://control"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
assert (rc == 0); TEST_ASSERT_GREATER_THAN_INT (
rc = s_send (control, my_endpoint); 0, TEST_ASSERT_SUCCESS_ERRNO (s_send (control, my_endpoint)));
assert (rc > 0);
// Use rep as both frontend and backend // Use rep as both frontend and backend
rc = zmq_proxy_steerable (rep, rep, NULL, control); TEST_ASSERT_SUCCESS_ERRNO (zmq_proxy_steerable (rep, rep, NULL, control));
assert (rc == 0);
rc = zmq_close (rep); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (rep));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
rc = zmq_close (control);
assert (rc == 0);
} }
// The main thread simply starts several clients and a server, and then // The main thread simply starts several clients and a server, and then
// waits for the server to finish. // waits for the server to finish.
void test_proxy_single_socket ()
{
void *server_thread = zmq_threadstart (&server_task, NULL);
// Control socket receives terminate command from main over inproc
void *control = test_context_socket (ZMQ_REP);
TEST_ASSERT_NOT_NULL (control);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (control, "inproc://control"));
char *my_endpoint = s_recv (control);
TEST_ASSERT_NOT_NULL (my_endpoint);
// client socket pings proxy over tcp
void *req = test_context_socket (ZMQ_REQ);
TEST_ASSERT_NOT_NULL (req);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req, my_endpoint));
send_string_expect_success (req, "msg1", 0);
recv_string_expect_success (req, "msg1", 0);
send_string_expect_success (req, "msg22", 0);
recv_string_expect_success (req, "msg22", 0);
send_string_expect_success (control, "TERMINATE", 0);
test_context_socket_close (control);
test_context_socket_close (req);
free (my_endpoint);
zmq_threadclose (server_thread);
}
int main (void) int main (void)
{ {
setup_test_environment (); setup_test_environment ();
void *ctx = zmq_ctx_new (); UNITY_BEGIN ();
assert (ctx); RUN_TEST (test_proxy_single_socket);
return UNITY_END ();
void *server_thread = zmq_threadstart (&server_task, ctx);
// Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx, ZMQ_REP);
assert (control);
int rc = zmq_bind (control, "inproc://control");
assert (rc == 0);
char *my_endpoint = s_recv (control);
assert (my_endpoint);
// client socket pings proxy over tcp
void *req = zmq_socket (ctx, ZMQ_REQ);
assert (req);
rc = zmq_connect (req, my_endpoint);
assert (rc == 0);
char buf[255];
rc = zmq_send (req, "msg1", 4, 0);
assert (rc == 4);
rc = zmq_recv (req, buf, 255, 0);
assert (rc == 4);
assert (memcmp (buf, "msg1", 4) == 0);
rc = zmq_send (req, "msg22", 5, 0);
assert (rc == 5);
rc = zmq_recv (req, buf, 255, 0);
assert (rc == 5);
assert (memcmp (buf, "msg22", 5) == 0);
rc = zmq_send (control, "TERMINATE", 9, 0);
assert (rc == 9);
rc = zmq_close (control);
assert (rc == 0);
rc = zmq_close (req);
assert (rc == 0);
free (my_endpoint);
zmq_threadclose (server_thread);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
} }

View File

@ -28,104 +28,96 @@
*/ */
#include "testutil.hpp" #include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// This is a test for issue #1382. The server thread creates a SUB-PUSH // This is a test for issue #1382. The server thread creates a SUB-PUSH
// steerable proxy. The main process then sends messages to the SUB // steerable proxy. The main process then sends messages to the SUB
// but there is no pull on the other side, previously the proxy blocks // but there is no pull on the other side, previously the proxy blocks
// in writing to the backend, preventing the proxy from terminating // in writing to the backend, preventing the proxy from terminating
void server_task (void *ctx_) void server_task (void * /*unused_*/)
{ {
size_t len = MAX_SOCKET_STRING;
char my_endpoint[MAX_SOCKET_STRING]; char my_endpoint[MAX_SOCKET_STRING];
// Frontend socket talks to main process // Frontend socket talks to main process
void *frontend = zmq_socket (ctx_, ZMQ_SUB); void *frontend = zmq_socket (get_test_context (), ZMQ_SUB);
assert (frontend); TEST_ASSERT_NOT_NULL (frontend);
int rc = zmq_setsockopt (frontend, ZMQ_SUBSCRIBE, "", 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (frontend, ZMQ_SUBSCRIBE, "", 0));
assert (rc == 0); bind_loopback_ipv4 (frontend, my_endpoint, sizeof my_endpoint);
rc = zmq_bind (frontend, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (frontend, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
// Nice socket which is never read // Nice socket which is never read
void *backend = zmq_socket (ctx_, ZMQ_PUSH); void *backend = zmq_socket (get_test_context (), ZMQ_PUSH);
assert (backend); TEST_ASSERT_NOT_NULL (backend);
rc = zmq_bind (backend, "tcp://127.0.0.1:*"); TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tcp://127.0.0.1:*"));
assert (rc == 0);
// Control socket receives terminate command from main over inproc // Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx_, ZMQ_REQ); void *control = zmq_socket (get_test_context (), ZMQ_REQ);
assert (control); TEST_ASSERT_NOT_NULL (control);
rc = zmq_connect (control, "inproc://control"); TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
assert (rc == 0); TEST_ASSERT_GREATER_THAN_INT (
rc = s_send (control, my_endpoint); 0, TEST_ASSERT_SUCCESS_ERRNO (s_send (control, my_endpoint)));
assert (rc > 0);
// Connect backend to frontend via a proxy // Connect backend to frontend via a proxy
rc = zmq_proxy_steerable (frontend, backend, NULL, control); TEST_ASSERT_SUCCESS_ERRNO (
assert (rc == 0); zmq_proxy_steerable (frontend, backend, NULL, control));
rc = zmq_close (frontend); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (frontend));
assert (rc == 0); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (backend));
rc = zmq_close (backend); TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
assert (rc == 0);
rc = zmq_close (control);
assert (rc == 0);
} }
// The main thread simply starts a basic steerable proxy server, publishes some messages, and then // The main thread simply starts a basic steerable proxy server, publishes some messages, and then
// waits for the server to terminate. // waits for the server to terminate.
void test_proxy_terminate ()
{
void *thread = zmq_threadstart (&server_task, NULL);
// Control socket receives terminate command from main over inproc
void *control = test_context_socket (ZMQ_REP);
TEST_ASSERT_NOT_NULL (control);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (control, "inproc://control"));
char *my_endpoint = s_recv (control);
TEST_ASSERT_NOT_NULL (my_endpoint);
msleep (500); // Run for 500 ms
// Start a secondary publisher which writes data to the SUB-PUSH server socket
void *publisher = test_context_socket (ZMQ_PUB);
TEST_ASSERT_NOT_NULL (publisher);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (publisher, my_endpoint));
msleep (SETTLE_TIME);
send_string_expect_success (publisher, "This is a test", 0);
msleep (50);
send_string_expect_success (publisher, "This is a test", 0);
msleep (50);
send_string_expect_success (publisher, "This is a test", 0);
send_string_expect_success (control, "TERMINATE", 0);
test_context_socket_close (publisher);
test_context_socket_close (control);
free (my_endpoint);
zmq_threadclose (thread);
}
int main (void) int main (void)
{ {
setup_test_environment (); setup_test_environment ();
void *ctx = zmq_ctx_new (); UNITY_BEGIN ();
assert (ctx); RUN_TEST (test_proxy_terminate);
return UNITY_END ();
void *thread = zmq_threadstart (&server_task, ctx);
// Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx, ZMQ_REP);
assert (control);
int rc = zmq_bind (control, "inproc://control");
assert (rc == 0);
char *my_endpoint = s_recv (control);
assert (my_endpoint);
msleep (500); // Run for 500 ms
// Start a secondary publisher which writes data to the SUB-PUSH server socket
void *publisher = zmq_socket (ctx, ZMQ_PUB);
assert (publisher);
rc = zmq_connect (publisher, my_endpoint);
assert (rc == 0);
msleep (SETTLE_TIME);
rc = zmq_send (publisher, "This is a test", 14, 0);
assert (rc == 14);
msleep (50);
rc = zmq_send (publisher, "This is a test", 14, 0);
assert (rc == 14);
msleep (50);
rc = zmq_send (publisher, "This is a test", 14, 0);
assert (rc == 14);
rc = zmq_send (control, "TERMINATE", 9, 0);
assert (rc == 9);
rc = zmq_close (publisher);
assert (rc == 0);
rc = zmq_close (control);
assert (rc == 0);
free (my_endpoint);
zmq_threadclose (thread);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0;
} }