From 7f74fc7c998cba2efd2bc286ab2aad711fb5c475 Mon Sep 17 00:00:00 2001 From: Richard Newton Date: Sat, 17 Aug 2013 13:43:45 +0100 Subject: [PATCH] Port tests to windows and add to cmake build. --- CMakeLists.txt | 52 ++++++++++++++++++++++++++++++-- include/zmq_utils.h | 6 ++++ src/zmq_utils.cpp | 15 +++++++++ tests/test_connect_delay.cpp | 12 +++----- tests/test_connect_resolve.cpp | 5 ++- tests/test_ctx_options.cpp | 5 ++- tests/test_disconnect_inproc.cpp | 3 +- tests/test_hwm.cpp | 4 +-- tests/test_invalid_rep.cpp | 5 ++- tests/test_iov.cpp | 10 +++--- tests/test_last_endpoint.cpp | 5 ++- tests/test_monitor.cpp | 23 ++++++-------- tests/test_msg_flags.cpp | 5 ++- tests/test_pair_inproc.cpp | 1 + tests/test_pair_ipc.cpp | 1 + tests/test_pair_tcp.cpp | 1 + tests/test_probe_router.cpp | 4 +-- tests/test_raw_sock.cpp | 5 ++- tests/test_req_request_ids.cpp | 2 +- tests/test_req_strict.cpp | 7 ++--- tests/test_reqrep_device.cpp | 5 ++- tests/test_reqrep_inproc.cpp | 1 + tests/test_reqrep_ipc.cpp | 1 + tests/test_reqrep_tcp.cpp | 1 + tests/test_router_mandatory.cpp | 4 +-- tests/test_security.cpp | 9 +++--- tests/test_security_curve.cpp | 9 +++--- tests/test_shutdown_stress.cpp | 15 ++++----- tests/test_spec_dealer.cpp | 1 + tests/test_spec_pushpull.cpp | 1 + tests/test_spec_rep.cpp | 1 + tests/test_spec_req.cpp | 6 ++-- tests/test_spec_router.cpp | 1 + tests/test_stream.cpp | 5 ++- tests/test_sub_forward.cpp | 8 ++--- tests/test_term_endpoint.cpp | 12 +++----- tests/test_timeo.cpp | 16 +++------- tests/testutil.hpp | 12 ++++++++ 38 files changed, 171 insertions(+), 108 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ce46024..f4ade04b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -308,19 +308,19 @@ set(cxx-sources kqueue.cpp lb.cpp mailbox.cpp - mechanism.cpp + mechanism.cpp msg.cpp mtrie.cpp object.cpp options.cpp own.cpp - null_mechanism.cpp + null_mechanism.cpp pair.cpp pgm_receiver.cpp pgm_sender.cpp pgm_socket.cpp pipe.cpp - plain_mechanism.cpp + plain_mechanism.cpp poll.cpp poller_base.cpp precompiled.cpp @@ -576,6 +576,52 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") # Why? endforeach() endif() +enable_testing() +set(tests + test_connect_delay + test_connect_resolve + test_ctx_options + test_disconnect_inproc + test_hwm + test_invalid_rep + test_iov + test_last_endpoint + test_monitor + test_msg_flags + test_pair_inproc + test_pair_ipc + test_pair_tcp + test_probe_router + test_raw_sock + test_req_request_ids + test_req_strict + test_reqrep_device + test_reqrep_inproc + test_reqrep_ipc + test_reqrep_tcp + test_router_mandatory + test_security + test_security_curve + test_shutdown_stress + test_spec_dealer + test_spec_pushpull + test_spec_rep + test_spec_req + test_spec_router + test_stream + test_sub_forward + test_term_endpoint + test_timeo) + +foreach(test ${tests}) + add_executable(${test} tests/${test}.cpp) + target_link_libraries(${test} libzmq) + + if(RT_LIBRARY) + target_link_libraries(${test} ${RT_LIBRARY}) + endif() + add_test(NAME ${test} WORKING_DIRECTORY ${LIBRARY_OUTPUT_PATH}/Debug COMMAND ${test}) +endforeach() #----------------------------------------------------------------------------- # installer diff --git a/include/zmq_utils.h b/include/zmq_utils.h index 10b5b18c..55a7be18 100644 --- a/include/zmq_utils.h +++ b/include/zmq_utils.h @@ -56,6 +56,12 @@ ZMQ_EXPORT unsigned long zmq_stopwatch_stop (void *watch_); /* Sleeps for specified number of seconds. */ ZMQ_EXPORT void zmq_sleep (int seconds_); +/* Start a thread. Returns a handle to the thread. */ +ZMQ_EXPORT void *zmq_threadstart(void* func, void* arg); + +/* Wait for thread to complete then free up resources. */ +ZMQ_EXPORT void zmq_threadclose(void* thread); + #undef ZMQ_EXPORT #ifdef __cplusplus diff --git a/src/zmq_utils.cpp b/src/zmq_utils.cpp index 53638756..0da11bb6 100644 --- a/src/zmq_utils.cpp +++ b/src/zmq_utils.cpp @@ -26,6 +26,7 @@ #include "stdint.hpp" #include "clock.hpp" #include "err.hpp" +#include "thread.hpp" #if !defined ZMQ_HAVE_WINDOWS #include @@ -57,3 +58,17 @@ unsigned long zmq_stopwatch_stop (void *watch_) free (watch_); return (unsigned long) (end - start); } + +void *zmq_threadstart(void* func, void* arg) +{ + zmq::thread_t* thread = new zmq::thread_t; + thread->start(static_cast(func), arg); + return thread; +} + +void zmq_threadclose(void* thread) +{ + zmq::thread_t* pThread = static_cast(thread); + pThread->stop(); + delete pThread; +} diff --git a/tests/test_connect_delay.cpp b/tests/test_connect_delay.cpp index b85aced1..77ed5fd1 100644 --- a/tests/test_connect_delay.cpp +++ b/tests/test_connect_delay.cpp @@ -18,17 +18,16 @@ */ #include "../include/zmq.h" +#include "../include/zmq_utils.h" #include #include #include -#include #include - -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); int val; int rc; char buffer[16]; @@ -198,11 +197,10 @@ int main (void) rc = zmq_close (backend); assert (rc == 0); - + // Give time to process disconnect // There's no way to do this except with a sleep - struct timespec t = { 0, 250 * 1000000 }; - nanosleep (&t, NULL); + zmq_sleep(1); // Send a message, should fail rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT); diff --git a/tests/test_connect_resolve.cpp b/tests/test_connect_resolve.cpp index dd4e58f1..99fb4adb 100644 --- a/tests/test_connect_resolve.cpp +++ b/tests/test_connect_resolve.cpp @@ -20,12 +20,11 @@ #include "../include/zmq.h" #include #include - -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_ctx_options.cpp b/tests/test_ctx_options.cpp index 7d71ea14..2fde234c 100644 --- a/tests/test_ctx_options.cpp +++ b/tests/test_ctx_options.cpp @@ -19,12 +19,11 @@ #include "../include/zmq.h" #include -#include -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); int rc; // Set up our context and sockets diff --git a/tests/test_disconnect_inproc.cpp b/tests/test_disconnect_inproc.cpp index bd22ed49..fe2a72b0 100644 --- a/tests/test_disconnect_inproc.cpp +++ b/tests/test_disconnect_inproc.cpp @@ -19,7 +19,7 @@ #include #include -#include +#include "testutil.hpp" /// Initialize a zeromq message with a given null-terminated string #define ZMQ_PREPARE_STRING(msg, data, size) \ @@ -31,6 +31,7 @@ int publicationsReceived = 0; bool isSubscribed = false; int main(int argc, char** argv) { + setup_test_environment(); void* context = zmq_ctx_new(); void* pubSocket; void* subSocket; diff --git a/tests/test_hwm.cpp b/tests/test_hwm.cpp index 0c85d3f1..7f26176a 100644 --- a/tests/test_hwm.cpp +++ b/tests/test_hwm.cpp @@ -20,11 +20,11 @@ #include "../include/zmq.h" #include #include -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_invalid_rep.cpp b/tests/test_invalid_rep.cpp index 91061e09..54a32e81 100644 --- a/tests/test_invalid_rep.cpp +++ b/tests/test_invalid_rep.cpp @@ -19,12 +19,11 @@ #include "../include/zmq.h" #include - -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); // Create REQ/ROUTER wiring. void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_iov.cpp b/tests/test_iov.cpp index b5ac817b..6d647123 100644 --- a/tests/test_iov.cpp +++ b/tests/test_iov.cpp @@ -18,12 +18,11 @@ */ #include "../include/zmq.h" +#include "../include/zmq_utils.h" #include -#include #include #include -#undef NDEBUG -#include +#include "testutil.hpp" // XSI vector I/O #if defined ZMQ_HAVE_UIO @@ -37,6 +36,7 @@ struct iovec { void do_check(void* sb, void* sc, unsigned int msgsz) { + setup_test_environment(); int rc; int sum =0; for (int i = 0; i < 10; i++) @@ -85,7 +85,7 @@ int main (void) rc = zmq_bind (sb, "inproc://a"); assert (rc == 0); - ::sleep(1); + zmq_sleep(1); void *sc = zmq_socket (ctx, ZMQ_PUSH); rc = zmq_connect (sc, "inproc://a"); @@ -107,5 +107,5 @@ int main (void) rc = zmq_ctx_term (ctx); assert (rc == 0); - return 0; + return 0; } diff --git a/tests/test_last_endpoint.cpp b/tests/test_last_endpoint.cpp index c364dc81..dc836869 100644 --- a/tests/test_last_endpoint.cpp +++ b/tests/test_last_endpoint.cpp @@ -19,9 +19,7 @@ #include "../include/zmq.h" #include - -#undef NDEBUG -#include +#include "testutil.hpp" static void do_bind_and_verify (void *s, const char *endpoint) { @@ -35,6 +33,7 @@ static void do_bind_and_verify (void *s, const char *endpoint) int main (void) { + setup_test_environment(); // Create the infrastructure void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_monitor.cpp b/tests/test_monitor.cpp index 499c6a99..ce7b19df 100644 --- a/tests/test_monitor.cpp +++ b/tests/test_monitor.cpp @@ -19,9 +19,8 @@ #include #include "../include/zmq.h" -#include +#include "../include/zmq_utils.h" #include -#include #include "testutil.hpp" // REQ socket events handled @@ -180,11 +179,12 @@ static void *rep_socket_monitor (void *ctx) int main (void) { + setup_test_environment(); int rc; void *req; void *req2; void *rep; - pthread_t threads [3]; + void* threads [3]; addr = "tcp://127.0.0.1:5560"; @@ -208,8 +208,7 @@ int main (void) // REP socket monitor, all events rc = zmq_socket_monitor (rep, "inproc://monitor.rep", ZMQ_EVENT_ALL); assert (rc == 0); - rc = pthread_create (&threads [0], NULL, rep_socket_monitor, ctx); - assert (rc == 0); + threads [0] = zmq_threadstart(rep_socket_monitor, ctx); // REQ socket req = zmq_socket (ctx, ZMQ_REQ); @@ -218,9 +217,8 @@ int main (void) // REQ socket monitor, all events rc = zmq_socket_monitor (req, "inproc://monitor.req", ZMQ_EVENT_ALL); assert (rc == 0); - rc = pthread_create (&threads [1], NULL, req_socket_monitor, ctx); - assert (rc == 0); - sleep (1); + threads [1] = zmq_threadstart(req_socket_monitor, ctx); + zmq_sleep(1); // Bind REQ and REP rc = zmq_bind (rep, addr.c_str()); @@ -238,8 +236,7 @@ int main (void) // 2nd REQ socket monitor, connected event only rc = zmq_socket_monitor (req2, "inproc://monitor.req2", ZMQ_EVENT_CONNECTED); assert (rc == 0); - rc = pthread_create (&threads [2], NULL, req2_socket_monitor, ctx); - assert (rc == 0); + threads [2] = zmq_threadstart(req2_socket_monitor, ctx); rc = zmq_connect (req2, addr.c_str()); assert (rc == 0); @@ -249,8 +246,7 @@ int main (void) assert (rc == 0); // Allow some time for detecting error states - struct timespec t = { 0, 250 * 1000000 }; - nanosleep (&t, NULL); + zmq_sleep(1); // Close the REQ socket rc = zmq_close (req); @@ -276,7 +272,8 @@ int main (void) assert (req2_socket_events & ZMQ_EVENT_CONNECTED); assert (!(req2_socket_events & ZMQ_EVENT_CLOSED)); - pthread_exit (NULL); + for (unsigned int i = 0; i < 3; ++i) + zmq_threadclose(threads [i]); return 0 ; } diff --git a/tests/test_msg_flags.cpp b/tests/test_msg_flags.cpp index 8d40cb71..172724e5 100644 --- a/tests/test_msg_flags.cpp +++ b/tests/test_msg_flags.cpp @@ -19,12 +19,11 @@ #include "../include/zmq.h" #include - -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); // Create the infrastructure void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_pair_inproc.cpp b/tests/test_pair_inproc.cpp index 6ee651d1..f041ca74 100644 --- a/tests/test_pair_inproc.cpp +++ b/tests/test_pair_inproc.cpp @@ -22,6 +22,7 @@ int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_pair_ipc.cpp b/tests/test_pair_ipc.cpp index e4d38805..6ee23953 100644 --- a/tests/test_pair_ipc.cpp +++ b/tests/test_pair_ipc.cpp @@ -22,6 +22,7 @@ int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_pair_tcp.cpp b/tests/test_pair_tcp.cpp index 6d204c57..ca79e503 100644 --- a/tests/test_pair_tcp.cpp +++ b/tests/test_pair_tcp.cpp @@ -22,6 +22,7 @@ int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_probe_router.cpp b/tests/test_probe_router.cpp index 0c2f553e..6a2ae8c9 100644 --- a/tests/test_probe_router.cpp +++ b/tests/test_probe_router.cpp @@ -20,11 +20,11 @@ #include "../include/zmq.h" #include #include -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_raw_sock.cpp b/tests/test_raw_sock.cpp index f18d3b4a..05d1c891 100644 --- a/tests/test_raw_sock.cpp +++ b/tests/test_raw_sock.cpp @@ -19,9 +19,7 @@ #include "../include/zmq.h" #include -#include -#undef NDEBUG -#include +#include "testutil.hpp" // ZMTP protocol greeting structure @@ -45,6 +43,7 @@ static zmtp_greeting_t greeting int main (void) { + setup_test_environment(); int rc; // Set up our context and sockets diff --git a/tests/test_req_request_ids.cpp b/tests/test_req_request_ids.cpp index 92658573..6f7f9490 100644 --- a/tests/test_req_request_ids.cpp +++ b/tests/test_req_request_ids.cpp @@ -18,12 +18,12 @@ */ #include -#include #include #include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_req_strict.cpp b/tests/test_req_strict.cpp index 9ece8a6c..fe3969f2 100644 --- a/tests/test_req_strict.cpp +++ b/tests/test_req_strict.cpp @@ -17,13 +17,14 @@ along with this program. If not, see . */ +#include "../include/zmq_utils.h" #include -#include #include #include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); @@ -57,9 +58,7 @@ int main (void) // We have to give the connects time to finish otherwise the requests // will not properly round-robin. We could alternatively connect the // REQ sockets to the REP sockets. - struct timespec t = { 0, 250 * 1000000 }; - nanosleep (&t, NULL); - + zmq_sleep(1); // Case 1: Second send() before a reply arrives in a pipe. diff --git a/tests/test_reqrep_device.cpp b/tests/test_reqrep_device.cpp index c7d0a442..d5945668 100644 --- a/tests/test_reqrep_device.cpp +++ b/tests/test_reqrep_device.cpp @@ -20,12 +20,11 @@ #include "../include/zmq.h" #include #include - -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_reqrep_inproc.cpp b/tests/test_reqrep_inproc.cpp index 0c9f50c1..2172df12 100644 --- a/tests/test_reqrep_inproc.cpp +++ b/tests/test_reqrep_inproc.cpp @@ -22,6 +22,7 @@ int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_reqrep_ipc.cpp b/tests/test_reqrep_ipc.cpp index 9af3972c..c07117ae 100644 --- a/tests/test_reqrep_ipc.cpp +++ b/tests/test_reqrep_ipc.cpp @@ -22,6 +22,7 @@ int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_reqrep_tcp.cpp b/tests/test_reqrep_tcp.cpp index 2640af30..8097b2ce 100644 --- a/tests/test_reqrep_tcp.cpp +++ b/tests/test_reqrep_tcp.cpp @@ -22,6 +22,7 @@ int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_router_mandatory.cpp b/tests/test_router_mandatory.cpp index 8b6e38ec..512e3ed9 100644 --- a/tests/test_router_mandatory.cpp +++ b/tests/test_router_mandatory.cpp @@ -20,11 +20,11 @@ #include "../include/zmq.h" #include #include -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); void *router = zmq_socket (ctx, ZMQ_ROUTER); diff --git a/tests/test_security.cpp b/tests/test_security.cpp index 225d857e..037f7c3d 100644 --- a/tests/test_security.cpp +++ b/tests/test_security.cpp @@ -17,7 +17,7 @@ along with this program. If not, see . */ -#include +#include "../include/zmq_utils.h" #include #include #include "testutil.hpp" @@ -68,6 +68,7 @@ zap_handler (void *zap) int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); @@ -177,9 +178,7 @@ int main (void) assert (rc == 0); // Spawn ZAP handler - pthread_t zap_thread; - rc = pthread_create (&zap_thread, NULL, &zap_handler, zap); - assert (rc == 0); + void* zap_thread = zmq_threadstart(&zap_handler, zap); rc = zmq_bind (server, "tcp://*:9998"); assert (rc == 0); @@ -194,7 +193,7 @@ int main (void) assert (rc == 0); // Wait until ZAP handler terminates. - pthread_join (zap_thread, NULL); + zmq_threadclose(zap_thread); // Check PLAIN security -- two servers trying to talk to each other server = zmq_socket (ctx, ZMQ_DEALER); diff --git a/tests/test_security_curve.cpp b/tests/test_security_curve.cpp index 94ab9f93..2350b435 100644 --- a/tests/test_security_curve.cpp +++ b/tests/test_security_curve.cpp @@ -18,7 +18,7 @@ */ #include "platform.hpp" -#include +#include "../include/zmq_utils.h" #include #include #include "testutil.hpp" @@ -62,6 +62,7 @@ int main (void) printf ("libsodium not installed, skipping CURVE test\n"); return 0; #endif + setup_test_environment(); int rc; size_t optsize; int mechanism; @@ -122,9 +123,7 @@ int main (void) assert (rc == 0); // Spawn ZAP handler - pthread_t zap_thread; - rc = pthread_create (&zap_thread, NULL, &zap_handler, zap); - assert (rc == 0); + void* zap_thread = zmq_threadstart(&zap_handler, zap); rc = zmq_bind (server, "tcp://*:9998"); assert (rc == 0); @@ -139,7 +138,7 @@ int main (void) assert (rc == 0); // Wait until ZAP handler terminates. - pthread_join (zap_thread, NULL); + zmq_threadclose(zap_thread); // Shutdown rc = zmq_ctx_term (ctx); diff --git a/tests/test_shutdown_stress.cpp b/tests/test_shutdown_stress.cpp index 8e8a3099..88808b13 100644 --- a/tests/test_shutdown_stress.cpp +++ b/tests/test_shutdown_stress.cpp @@ -18,12 +18,10 @@ */ #include "../include/zmq.h" -#include +#include "../include/zmq_utils.h" #include #include - -#undef NDEBUG -#include +#include "testutil.hpp" #define THREAD_COUNT 100 @@ -46,12 +44,13 @@ extern "C" int main (void) { + setup_test_environment(); void *s1; void *s2; int i; int j; int rc; - pthread_t threads [THREAD_COUNT]; + void* threads [THREAD_COUNT]; for (j = 0; j != 10; j++) { @@ -69,13 +68,11 @@ int main (void) for (i = 0; i != THREAD_COUNT; i++) { s2 = zmq_socket (ctx, ZMQ_SUB); assert (s2); - rc = pthread_create (&threads [i], NULL, worker, s2); - assert (rc == 0); + threads [i] = zmq_threadstart(worker, s2); } for (i = 0; i != THREAD_COUNT; i++) { - rc = pthread_join (threads [i], NULL); - assert (rc == 0); + zmq_threadclose(threads [i]); } rc = zmq_close (s1); diff --git a/tests/test_spec_dealer.cpp b/tests/test_spec_dealer.cpp index 50359399..fe442d24 100644 --- a/tests/test_spec_dealer.cpp +++ b/tests/test_spec_dealer.cpp @@ -220,6 +220,7 @@ void test_block_on_send_no_peers (void *ctx) int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_spec_pushpull.cpp b/tests/test_spec_pushpull.cpp index 5af091ea..f9d50102 100644 --- a/tests/test_spec_pushpull.cpp +++ b/tests/test_spec_pushpull.cpp @@ -258,6 +258,7 @@ void test_destroy_queue_on_disconnect (void *ctx) int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_spec_rep.cpp b/tests/test_spec_rep.cpp index 3459b743..2aadc172 100644 --- a/tests/test_spec_rep.cpp +++ b/tests/test_spec_rep.cpp @@ -123,6 +123,7 @@ void test_envelope (void *ctx) int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_spec_req.cpp b/tests/test_spec_req.cpp index 1c0384ed..416c419d 100644 --- a/tests/test_spec_req.cpp +++ b/tests/test_spec_req.cpp @@ -17,8 +17,8 @@ along with this program. If not, see . */ +#include "../include/zmq_utils.h" #include -#include #include #include "testutil.hpp" @@ -49,8 +49,7 @@ void test_round_robin_out (void *ctx) // We have to give the connects time to finish otherwise the requests // will not properly round-robin. We could alternatively connect the // REQ sockets to the REP sockets. - struct timespec t = { 0, 250 * 1000000 }; - nanosleep (&t, NULL); + zmq_sleep(1); // Send our peer-replies, and expect every REP it used once in order for (size_t peer = 0; peer < services; peer++) { @@ -217,6 +216,7 @@ void test_block_on_send_no_peers (void *ctx) int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_spec_router.cpp b/tests/test_spec_router.cpp index 8083e29d..f733285c 100644 --- a/tests/test_spec_router.cpp +++ b/tests/test_spec_router.cpp @@ -177,6 +177,7 @@ void test_destroy_queue_on_disconnect (void *ctx) int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); diff --git a/tests/test_stream.cpp b/tests/test_stream.cpp index 58812b62..93f1e7db 100644 --- a/tests/test_stream.cpp +++ b/tests/test_stream.cpp @@ -19,9 +19,7 @@ #include "../include/zmq.h" #include -#include -#undef NDEBUG -#include +#include "testutil.hpp" // ZMTP protocol greeting structure @@ -222,6 +220,7 @@ test_stream_to_stream (void) int main (void) { + setup_test_environment(); test_stream_to_dealer (); test_stream_to_stream (); } diff --git a/tests/test_sub_forward.cpp b/tests/test_sub_forward.cpp index 8caa874e..acf91314 100644 --- a/tests/test_sub_forward.cpp +++ b/tests/test_sub_forward.cpp @@ -18,13 +18,14 @@ */ #include "../include/zmq.h" +#include "../include/zmq_utils.h" #include #include -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); @@ -62,8 +63,7 @@ int main (void) assert (rc >= 0); // Wait a bit till the subscription gets to the publisher - struct timespec t = { 0, 250 * 1000000 }; - nanosleep (&t, NULL); + zmq_sleep(1); // Send an empty message rc = zmq_send (pub, NULL, 0, 0); diff --git a/tests/test_term_endpoint.cpp b/tests/test_term_endpoint.cpp index d5743c57..ad43df51 100644 --- a/tests/test_term_endpoint.cpp +++ b/tests/test_term_endpoint.cpp @@ -18,15 +18,14 @@ */ #include "../include/zmq.h" +#include "../include/zmq_utils.h" #include -#include #include - -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); int rc; char buf[32]; const char *ep = "tcp://127.0.0.1:5560"; @@ -54,8 +53,7 @@ int main (void) assert (rc == 0); // Allow unbind to settle - struct timespec t = { 0, 250 * 1000000 }; - nanosleep (&t, NULL); + zmq_sleep(1); // Check that sending would block (there's no outbound connection) rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT); @@ -92,7 +90,7 @@ int main (void) assert (rc == 0); // Allow disconnect to settle - nanosleep (&t, NULL); + zmq_sleep(1); // Check that sending would block (there's no inbound connections). rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT); diff --git a/tests/test_timeo.cpp b/tests/test_timeo.cpp index 2e81a058..b30b6518 100644 --- a/tests/test_timeo.cpp +++ b/tests/test_timeo.cpp @@ -18,15 +18,15 @@ */ #include "../include/zmq.h" -#include +#include "../include/zmq_utils.h" #include #include -#undef NDEBUG -#include +#include "testutil.hpp" int main (void) { + setup_test_environment(); void *ctx = zmq_ctx_new (); assert (ctx); @@ -46,17 +46,11 @@ int main (void) rc = zmq_setsockopt (frontend, ZMQ_RCVTIMEO, &timeout, sizeof (int)); assert (rc == 0); - struct timeval before, after; - gettimeofday (&before, NULL); + void* stopwatch = zmq_stopwatch_start(); rc = zmq_recv (frontend, buffer, 32, 0); assert (rc == -1); assert (zmq_errno () == EAGAIN); - gettimeofday (&after, NULL); - - long elapsed = (long) - ((after.tv_sec * 1000 + after.tv_usec / 1000) - - (before.tv_sec * 1000 + before.tv_usec / 1000)); - + unsigned int elapsed = zmq_stopwatch_stop(stopwatch) / 1000; assert (elapsed > 200 && elapsed < 300); // Check that normal message flow works as expected diff --git a/tests/testutil.hpp b/tests/testutil.hpp index 0a938078..b6ae9a20 100644 --- a/tests/testutil.hpp +++ b/tests/testutil.hpp @@ -22,10 +22,15 @@ #include "../include/zmq.h" #include + #undef NDEBUG #include #include +#if defined _WIN32 +#pragma warning(disable:4996) +#endif + // Bounce a message from client to server and back // For REQ/REP or DEALER/DEALER pairs only @@ -191,4 +196,11 @@ void close_zero_linger (void *socket) assert (rc == 0); } +void setup_test_environment() +{ +#if defined _WIN32 + _set_abort_behavior( 0, _WRITE_ABORT_MSG); +#endif +} + #endif