0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-26 23:01:04 +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_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_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_LDADD = src/libzmq.la ${UNITY_LIBS}

View File

@ -28,92 +28,81 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
void setUp ()
{
setup_test_context ();
}
void tearDown ()
{
teardown_test_context ();
}
// This is our server task.
// 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];
void *rep = zmq_socket (ctx_, ZMQ_REP);
assert (rep);
int rc = zmq_bind (rep, "tcp://127.0.0.1:*");
assert (rc == 0);
rc = zmq_getsockopt (rep, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
assert (rc == 0);
void *rep = zmq_socket (get_test_context (), ZMQ_REP);
TEST_ASSERT_NOT_NULL (rep);
bind_loopback_ipv4 (rep, my_endpoint, sizeof my_endpoint);
// Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx_, ZMQ_REQ);
assert (control);
rc = zmq_connect (control, "inproc://control");
assert (rc == 0);
rc = s_send (control, my_endpoint);
assert (rc > 0);
void *control = zmq_socket (get_test_context (), ZMQ_REQ);
TEST_ASSERT_NOT_NULL (control);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
TEST_ASSERT_GREATER_THAN_INT (
0, TEST_ASSERT_SUCCESS_ERRNO (s_send (control, my_endpoint)));
// Use rep as both frontend and backend
rc = zmq_proxy_steerable (rep, rep, NULL, control);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_proxy_steerable (rep, rep, NULL, control));
rc = zmq_close (rep);
assert (rc == 0);
rc = zmq_close (control);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (rep));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
}
// The main thread simply starts several clients and a server, and then
// 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)
{
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
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;
UNITY_BEGIN ();
RUN_TEST (test_proxy_single_socket);
return UNITY_END ();
}

View File

@ -28,104 +28,96 @@
*/
#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
// steerable proxy. The main process then sends messages to the SUB
// but there is no pull on the other side, previously the proxy blocks
// 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];
// Frontend socket talks to main process
void *frontend = zmq_socket (ctx_, ZMQ_SUB);
assert (frontend);
int rc = zmq_setsockopt (frontend, ZMQ_SUBSCRIBE, "", 0);
assert (rc == 0);
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);
void *frontend = zmq_socket (get_test_context (), ZMQ_SUB);
TEST_ASSERT_NOT_NULL (frontend);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (frontend, ZMQ_SUBSCRIBE, "", 0));
bind_loopback_ipv4 (frontend, my_endpoint, sizeof my_endpoint);
// Nice socket which is never read
void *backend = zmq_socket (ctx_, ZMQ_PUSH);
assert (backend);
rc = zmq_bind (backend, "tcp://127.0.0.1:*");
assert (rc == 0);
void *backend = zmq_socket (get_test_context (), ZMQ_PUSH);
TEST_ASSERT_NOT_NULL (backend);
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, "tcp://127.0.0.1:*"));
// Control socket receives terminate command from main over inproc
void *control = zmq_socket (ctx_, ZMQ_REQ);
assert (control);
rc = zmq_connect (control, "inproc://control");
assert (rc == 0);
rc = s_send (control, my_endpoint);
assert (rc > 0);
void *control = zmq_socket (get_test_context (), ZMQ_REQ);
TEST_ASSERT_NOT_NULL (control);
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (control, "inproc://control"));
TEST_ASSERT_GREATER_THAN_INT (
0, TEST_ASSERT_SUCCESS_ERRNO (s_send (control, my_endpoint)));
// Connect backend to frontend via a proxy
rc = zmq_proxy_steerable (frontend, backend, NULL, control);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_proxy_steerable (frontend, backend, NULL, control));
rc = zmq_close (frontend);
assert (rc == 0);
rc = zmq_close (backend);
assert (rc == 0);
rc = zmq_close (control);
assert (rc == 0);
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (frontend));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (backend));
TEST_ASSERT_SUCCESS_ERRNO (zmq_close (control));
}
// The main thread simply starts a basic steerable proxy server, publishes some messages, and then
// 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)
{
setup_test_environment ();
void *ctx = zmq_ctx_new ();
assert (ctx);
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;
UNITY_BEGIN ();
RUN_TEST (test_proxy_terminate);
return UNITY_END ();
}