From 56619463ac26dc6c3e576d2fbf6b544eebeb5148 Mon Sep 17 00:00:00 2001 From: Martin Lucina Date: Sun, 20 May 2012 11:24:59 +0200 Subject: Imported Upstream version 2.2.0 --- tests/Makefile.am | 4 +- tests/Makefile.in | 21 +++++++-- tests/test_timeo.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 5 deletions(-) create mode 100644 tests/test_timeo.cpp (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index ebbc46c..785b7c5 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,7 +10,8 @@ noinst_PROGRAMS = test_pair_inproc \ if !ON_MINGW noinst_PROGRAMS += test_shutdown_stress \ test_pair_ipc \ - test_reqrep_ipc + test_reqrep_ipc \ + test_timeo endif test_pair_inproc_SOURCES = test_pair_inproc.cpp testutil.hpp @@ -25,6 +26,7 @@ if !ON_MINGW test_shutdown_stress_SOURCES = test_shutdown_stress.cpp test_pair_ipc_SOURCES = test_pair_ipc.cpp testutil.hpp test_reqrep_ipc_SOURCES = test_reqrep_ipc.cpp testutil.hpp +test_timeo_SOURCES = test_timeo.cpp endif TESTS = $(noinst_PROGRAMS) diff --git a/tests/Makefile.in b/tests/Makefile.in index 2bf15e0..c5a95a7 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -39,7 +39,8 @@ noinst_PROGRAMS = test_pair_inproc$(EXEEXT) test_pair_tcp$(EXEEXT) \ test_hwm$(EXEEXT) $(am__EXEEXT_1) @ON_MINGW_FALSE@am__append_1 = test_shutdown_stress \ @ON_MINGW_FALSE@ test_pair_ipc \ -@ON_MINGW_FALSE@ test_reqrep_ipc +@ON_MINGW_FALSE@ test_reqrep_ipc \ +@ON_MINGW_FALSE@ test_timeo subdir = tests DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in @@ -58,7 +59,7 @@ CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = @ON_MINGW_FALSE@am__EXEEXT_1 = test_shutdown_stress$(EXEEXT) \ @ON_MINGW_FALSE@ test_pair_ipc$(EXEEXT) \ -@ON_MINGW_FALSE@ test_reqrep_ipc$(EXEEXT) +@ON_MINGW_FALSE@ test_reqrep_ipc$(EXEEXT) test_timeo$(EXEEXT) PROGRAMS = $(noinst_PROGRAMS) am_test_hwm_OBJECTS = test_hwm.$(OBJEXT) test_hwm_OBJECTS = $(am_test_hwm_OBJECTS) @@ -100,6 +101,11 @@ am__test_shutdown_stress_SOURCES_DIST = test_shutdown_stress.cpp test_shutdown_stress_OBJECTS = $(am_test_shutdown_stress_OBJECTS) test_shutdown_stress_LDADD = $(LDADD) test_shutdown_stress_DEPENDENCIES = $(top_builddir)/src/libzmq.la +am__test_timeo_SOURCES_DIST = test_timeo.cpp +@ON_MINGW_FALSE@am_test_timeo_OBJECTS = test_timeo.$(OBJEXT) +test_timeo_OBJECTS = $(am_test_timeo_OBJECTS) +test_timeo_LDADD = $(LDADD) +test_timeo_DEPENDENCIES = $(top_builddir)/src/libzmq.la DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/config/depcomp am__depfiles_maybe = depfiles @@ -145,12 +151,14 @@ am__v_GEN_0 = @echo " GEN " $@; SOURCES = $(test_hwm_SOURCES) $(test_pair_inproc_SOURCES) \ $(test_pair_ipc_SOURCES) $(test_pair_tcp_SOURCES) \ $(test_reqrep_inproc_SOURCES) $(test_reqrep_ipc_SOURCES) \ - $(test_reqrep_tcp_SOURCES) $(test_shutdown_stress_SOURCES) + $(test_reqrep_tcp_SOURCES) $(test_shutdown_stress_SOURCES) \ + $(test_timeo_SOURCES) DIST_SOURCES = $(test_hwm_SOURCES) $(test_pair_inproc_SOURCES) \ $(am__test_pair_ipc_SOURCES_DIST) $(test_pair_tcp_SOURCES) \ $(test_reqrep_inproc_SOURCES) \ $(am__test_reqrep_ipc_SOURCES_DIST) $(test_reqrep_tcp_SOURCES) \ - $(am__test_shutdown_stress_SOURCES_DIST) + $(am__test_shutdown_stress_SOURCES_DIST) \ + $(am__test_timeo_SOURCES_DIST) ETAGS = etags CTAGS = ctags am__tty_colors = \ @@ -303,6 +311,7 @@ test_hwm_SOURCES = test_hwm.cpp @ON_MINGW_FALSE@test_shutdown_stress_SOURCES = test_shutdown_stress.cpp @ON_MINGW_FALSE@test_pair_ipc_SOURCES = test_pair_ipc.cpp testutil.hpp @ON_MINGW_FALSE@test_reqrep_ipc_SOURCES = test_reqrep_ipc.cpp testutil.hpp +@ON_MINGW_FALSE@test_timeo_SOURCES = test_timeo.cpp TESTS = $(noinst_PROGRAMS) all: all-am @@ -371,6 +380,9 @@ test_reqrep_tcp$(EXEEXT): $(test_reqrep_tcp_OBJECTS) $(test_reqrep_tcp_DEPENDENC test_shutdown_stress$(EXEEXT): $(test_shutdown_stress_OBJECTS) $(test_shutdown_stress_DEPENDENCIES) @rm -f test_shutdown_stress$(EXEEXT) $(AM_V_CXXLD)$(CXXLINK) $(test_shutdown_stress_OBJECTS) $(test_shutdown_stress_LDADD) $(LIBS) +test_timeo$(EXEEXT): $(test_timeo_OBJECTS) $(test_timeo_DEPENDENCIES) + @rm -f test_timeo$(EXEEXT) + $(AM_V_CXXLD)$(CXXLINK) $(test_timeo_OBJECTS) $(test_timeo_LDADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -386,6 +398,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_reqrep_ipc.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_reqrep_tcp.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_shutdown_stress.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_timeo.Po@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/tests/test_timeo.cpp b/tests/test_timeo.cpp new file mode 100644 index 0000000..b9cd512 --- /dev/null +++ b/tests/test_timeo.cpp @@ -0,0 +1,125 @@ +/* + Copyright (c) 2010-2011 250bpm s.r.o. + Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file + + This file is part of 0MQ. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + 0MQ is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include + +#include "../include/zmq.h" +#include "../include/zmq_utils.h" + +extern "C" +{ + void *worker(void *ctx) + { + // Worker thread connects after delay of 1 second. Then it waits + // for 1 more second, so that async connect has time to succeed. + zmq_sleep (1); + void *sc = zmq_socket (ctx, ZMQ_PUSH); + assert (sc); + int rc = zmq_connect (sc, "inproc://timeout_test"); + assert (rc == 0); + zmq_sleep (1); + rc = zmq_close (sc); + assert (rc == 0); + return NULL; + } +} + +int main (int argc, char *argv []) +{ + fprintf (stderr, "test_timeo running...\n"); + + void *ctx = zmq_init (1); + assert (ctx); + + // Create a disconnected socket. + void *sb = zmq_socket (ctx, ZMQ_PULL); + assert (sb); + int rc = zmq_bind (sb, "inproc://timeout_test"); + assert (rc == 0); + + // Check whether non-blocking recv returns immediately. + zmq_msg_t msg; + zmq_msg_init (&msg); + rc = zmq_recv (sb, &msg, ZMQ_NOBLOCK); + assert (rc == -1); + assert (zmq_errno() == EAGAIN); + + // Check whether recv timeout is honoured. + int timeout = 500; + size_t timeout_size = sizeof timeout; + rc = zmq_setsockopt(sb, ZMQ_RCVTIMEO, &timeout, timeout_size); + assert (rc == 0); + void *watch = zmq_stopwatch_start (); + rc = zmq_recv (sb, &msg, 0); + assert (rc == -1); + assert (zmq_errno () == EAGAIN); + unsigned long elapsed = zmq_stopwatch_stop (watch); + assert (elapsed > 440000 && elapsed < 550000); + + // Check whether connection during the wait doesn't distort the timeout. + timeout = 2000; + rc = zmq_setsockopt(sb, ZMQ_RCVTIMEO, &timeout, timeout_size); + assert (rc == 0); + pthread_t thread; + rc = pthread_create (&thread, NULL, worker, ctx); + assert (rc == 0); + watch = zmq_stopwatch_start (); + rc = zmq_recv (sb, &msg, 0); + assert (rc == -1); + assert (zmq_errno () == EAGAIN); + elapsed = zmq_stopwatch_stop (watch); + assert (elapsed > 1900000 && elapsed < 2100000); + rc = pthread_join (thread, NULL); + assert (rc == 0); + + // Check that timeouts don't break normal message transfer. + void *sc = zmq_socket (ctx, ZMQ_PUSH); + assert (sc); + rc = zmq_setsockopt(sb, ZMQ_RCVTIMEO, &timeout, timeout_size); + assert (rc == 0); + rc = zmq_setsockopt(sb, ZMQ_SNDTIMEO, &timeout, timeout_size); + assert (rc == 0); + rc = zmq_connect (sc, "inproc://timeout_test"); + assert (rc == 0); + + zmq_msg_init (&msg); + zmq_msg_init_size (&msg, 32); + memcpy (zmq_msg_data (&msg), "12345678ABCDEFGH12345678abcdefgh", 32); + rc = zmq_send (sc, &msg, 0); + assert (rc == 0); + + rc = zmq_recv (sb, &msg, 0); + assert (rc == 0); + assert (32 == zmq_msg_size(&msg)); + + // Clean-up. + rc = zmq_close (sc); + assert (rc == 0); + rc = zmq_close (sb); + assert (rc == 0); + rc = zmq_term (ctx); + assert (rc == 0); + + return 0 ; +} + -- cgit v1.2.3