mirror of
https://github.com/zeromq/libzmq.git
synced 2025-03-09 15:26:04 +00:00
Problem: test_thread_safe not using unity
Solution: migrate to unity, and split test cases
This commit is contained in:
parent
5d32828bbf
commit
991b2336e4
@ -865,7 +865,8 @@ tests_test_client_server_LDADD = src/libzmq.la ${UNITY_LIBS}
|
||||
tests_test_client_server_CPPFLAGS = ${UNITY_CPPFLAGS}
|
||||
|
||||
tests_test_thread_safe_SOURCES = tests/test_thread_safe.cpp
|
||||
tests_test_thread_safe_LDADD = src/libzmq.la
|
||||
tests_test_thread_safe_LDADD = src/libzmq.la ${UNITY_LIBS}
|
||||
tests_test_thread_safe_CPPFLAGS = ${UNITY_CPPFLAGS}
|
||||
|
||||
tests_test_timers_SOURCES = tests/test_timers.cpp
|
||||
tests_test_timers_LDADD = src/libzmq.la
|
||||
|
@ -28,41 +28,42 @@
|
||||
*/
|
||||
|
||||
#include "testutil.hpp"
|
||||
#include "testutil_unity.hpp"
|
||||
|
||||
#include <unity.h>
|
||||
|
||||
void setUp ()
|
||||
{
|
||||
setup_test_context ();
|
||||
}
|
||||
|
||||
void tearDown ()
|
||||
{
|
||||
teardown_test_context ();
|
||||
}
|
||||
|
||||
// Client threads loop on send/recv until told to exit
|
||||
void client_thread (void *client)
|
||||
{
|
||||
char data = 0;
|
||||
for (int count = 0; count < 15000; count++) {
|
||||
int rc = zmq_send (client, &data, 1, 0);
|
||||
assert (rc == 1);
|
||||
send_string_expect_success (client, "0", 0);
|
||||
}
|
||||
data = 1;
|
||||
int rc = zmq_send (client, &data, 1, 0);
|
||||
assert (rc == 1);
|
||||
send_string_expect_success (client, "1", 0);
|
||||
}
|
||||
|
||||
int main (void)
|
||||
void test_thread_safe ()
|
||||
{
|
||||
setup_test_environment ();
|
||||
size_t len = MAX_SOCKET_STRING;
|
||||
char my_endpoint[MAX_SOCKET_STRING];
|
||||
void *ctx = zmq_ctx_new ();
|
||||
assert (ctx);
|
||||
|
||||
void *server = zmq_socket (ctx, ZMQ_SERVER);
|
||||
int rc = zmq_bind (server, "tcp://127.0.0.1:*");
|
||||
assert (rc == 0);
|
||||
rc = zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
|
||||
assert (rc == 0);
|
||||
void *server = test_context_socket (ZMQ_SERVER);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, "tcp://127.0.0.1:*"));
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
|
||||
|
||||
void *client = zmq_socket (ctx, ZMQ_CLIENT);
|
||||
int thread_safe;
|
||||
size_t size = sizeof (int);
|
||||
zmq_getsockopt (client, ZMQ_THREAD_SAFE, &thread_safe, &size);
|
||||
assert (thread_safe == 1);
|
||||
rc = zmq_connect (client, my_endpoint);
|
||||
assert (rc == 0);
|
||||
void *client = test_context_socket (ZMQ_CLIENT);
|
||||
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
|
||||
|
||||
void *t1 = zmq_threadstart (client_thread, client);
|
||||
void *t2 = zmq_threadstart (client_thread, client);
|
||||
@ -70,21 +71,49 @@ int main (void)
|
||||
char data;
|
||||
int threads_completed = 0;
|
||||
while (threads_completed < 2) {
|
||||
zmq_recv (server, &data, 1, 0);
|
||||
if (data == 1)
|
||||
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, &data, 1, 0));
|
||||
if (data == '1')
|
||||
threads_completed++; // Thread ended
|
||||
}
|
||||
zmq_threadclose (t1);
|
||||
zmq_threadclose (t2);
|
||||
|
||||
rc = zmq_close (server);
|
||||
assert (rc == 0);
|
||||
|
||||
rc = zmq_close (client);
|
||||
assert (rc == 0);
|
||||
|
||||
rc = zmq_ctx_term (ctx);
|
||||
assert (rc == 0);
|
||||
|
||||
return 0;
|
||||
test_context_socket_close (server);
|
||||
test_context_socket_close (client);
|
||||
}
|
||||
|
||||
void test_getsockopt_thread_safe (void *const socket)
|
||||
{
|
||||
int thread_safe;
|
||||
size_t size = sizeof (int);
|
||||
TEST_ASSERT_SUCCESS_ERRNO (
|
||||
zmq_getsockopt (socket, ZMQ_THREAD_SAFE, &thread_safe, &size));
|
||||
TEST_ASSERT_EQUAL_INT (1, thread_safe);
|
||||
}
|
||||
|
||||
void test_client_getsockopt_thread_safe ()
|
||||
{
|
||||
void *client = test_context_socket (ZMQ_CLIENT);
|
||||
test_getsockopt_thread_safe (client);
|
||||
test_context_socket_close (client);
|
||||
}
|
||||
|
||||
void test_server_getsockopt_thread_safe ()
|
||||
{
|
||||
void *server = test_context_socket (ZMQ_SERVER);
|
||||
test_getsockopt_thread_safe (server);
|
||||
test_context_socket_close (server);
|
||||
}
|
||||
|
||||
int main (void)
|
||||
{
|
||||
setup_test_environment ();
|
||||
|
||||
// TODO this file could be merged with test_client_server
|
||||
UNITY_BEGIN ();
|
||||
RUN_TEST (test_client_getsockopt_thread_safe);
|
||||
RUN_TEST (test_server_getsockopt_thread_safe);
|
||||
RUN_TEST (test_thread_safe);
|
||||
|
||||
return UNITY_END ();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user