mirror of
https://github.com/zeromq/libzmq.git
synced 2025-03-10 07:48:15 +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_client_server_CPPFLAGS = ${UNITY_CPPFLAGS}
|
||||||
|
|
||||||
tests_test_thread_safe_SOURCES = tests/test_thread_safe.cpp
|
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_SOURCES = tests/test_timers.cpp
|
||||||
tests_test_timers_LDADD = src/libzmq.la
|
tests_test_timers_LDADD = src/libzmq.la
|
||||||
|
@ -28,41 +28,42 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "testutil.hpp"
|
#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
|
// Client threads loop on send/recv until told to exit
|
||||||
void client_thread (void *client)
|
void client_thread (void *client)
|
||||||
{
|
{
|
||||||
char data = 0;
|
|
||||||
for (int count = 0; count < 15000; count++) {
|
for (int count = 0; count < 15000; count++) {
|
||||||
int rc = zmq_send (client, &data, 1, 0);
|
send_string_expect_success (client, "0", 0);
|
||||||
assert (rc == 1);
|
|
||||||
}
|
}
|
||||||
data = 1;
|
send_string_expect_success (client, "1", 0);
|
||||||
int rc = zmq_send (client, &data, 1, 0);
|
|
||||||
assert (rc == 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (void)
|
void test_thread_safe ()
|
||||||
{
|
{
|
||||||
setup_test_environment ();
|
|
||||||
size_t len = MAX_SOCKET_STRING;
|
size_t len = MAX_SOCKET_STRING;
|
||||||
char my_endpoint[MAX_SOCKET_STRING];
|
char my_endpoint[MAX_SOCKET_STRING];
|
||||||
void *ctx = zmq_ctx_new ();
|
|
||||||
assert (ctx);
|
|
||||||
|
|
||||||
void *server = zmq_socket (ctx, ZMQ_SERVER);
|
void *server = test_context_socket (ZMQ_SERVER);
|
||||||
int rc = zmq_bind (server, "tcp://127.0.0.1:*");
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, "tcp://127.0.0.1:*"));
|
||||||
assert (rc == 0);
|
TEST_ASSERT_SUCCESS_ERRNO (
|
||||||
rc = zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
|
zmq_getsockopt (server, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
|
||||||
assert (rc == 0);
|
|
||||||
|
|
||||||
void *client = zmq_socket (ctx, ZMQ_CLIENT);
|
void *client = test_context_socket (ZMQ_CLIENT);
|
||||||
int thread_safe;
|
|
||||||
size_t size = sizeof (int);
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint));
|
||||||
zmq_getsockopt (client, ZMQ_THREAD_SAFE, &thread_safe, &size);
|
|
||||||
assert (thread_safe == 1);
|
|
||||||
rc = zmq_connect (client, my_endpoint);
|
|
||||||
assert (rc == 0);
|
|
||||||
|
|
||||||
void *t1 = zmq_threadstart (client_thread, client);
|
void *t1 = zmq_threadstart (client_thread, client);
|
||||||
void *t2 = zmq_threadstart (client_thread, client);
|
void *t2 = zmq_threadstart (client_thread, client);
|
||||||
@ -70,21 +71,49 @@ int main (void)
|
|||||||
char data;
|
char data;
|
||||||
int threads_completed = 0;
|
int threads_completed = 0;
|
||||||
while (threads_completed < 2) {
|
while (threads_completed < 2) {
|
||||||
zmq_recv (server, &data, 1, 0);
|
TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, &data, 1, 0));
|
||||||
if (data == 1)
|
if (data == '1')
|
||||||
threads_completed++; // Thread ended
|
threads_completed++; // Thread ended
|
||||||
}
|
}
|
||||||
zmq_threadclose (t1);
|
zmq_threadclose (t1);
|
||||||
zmq_threadclose (t2);
|
zmq_threadclose (t2);
|
||||||
|
|
||||||
rc = zmq_close (server);
|
test_context_socket_close (server);
|
||||||
assert (rc == 0);
|
test_context_socket_close (client);
|
||||||
|
}
|
||||||
rc = zmq_close (client);
|
|
||||||
assert (rc == 0);
|
void test_getsockopt_thread_safe (void *const socket)
|
||||||
|
{
|
||||||
rc = zmq_ctx_term (ctx);
|
int thread_safe;
|
||||||
assert (rc == 0);
|
size_t size = sizeof (int);
|
||||||
|
TEST_ASSERT_SUCCESS_ERRNO (
|
||||||
return 0;
|
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