Unverified Commit a30133d8 authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #2935 from ZMQers/unity-base

Problem: no test framework, no unit tests
parents d0e01b4b 9ec95f9d
...@@ -948,6 +948,11 @@ set (ZMQ_BUILD_TESTS ON CACHE BOOL "Build the tests for ZeroMQ") ...@@ -948,6 +948,11 @@ set (ZMQ_BUILD_TESTS ON CACHE BOOL "Build the tests for ZeroMQ")
if (ZMQ_BUILD_TESTS) if (ZMQ_BUILD_TESTS)
enable_testing () # Enable testing only works in root scope enable_testing () # Enable testing only works in root scope
ADD_SUBDIRECTORY (tests) ADD_SUBDIRECTORY (tests)
if (BUILD_STATIC)
add_subdirectory (unittests)
else ()
message (WARNING "Not building unit tests, since BUILD_STATIC is not enabled")
endif ()
endif () endif ()
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
......
...@@ -426,6 +426,13 @@ test_apps = \ ...@@ -426,6 +426,13 @@ test_apps = \
tests/test_reconnect_ivl \ tests/test_reconnect_ivl \
tests/test_socket_null tests/test_socket_null
UNITY_CPPFLAGS = -I$(top_srcdir)/external/unity
UNITY_LIBS = $(top_builddir)/external/unity/libunity.a
noinst_LIBRARIES = external/unity/libunity.a
external_unity_libunity_a_SOURCES = external/unity/unity.c \
external/unity/unity.h \
external/unity/unity_internals.h
tests_test_ancillaries_SOURCES = tests/test_ancillaries.cpp tests_test_ancillaries_SOURCES = tests/test_ancillaries.cpp
tests_test_ancillaries_LDADD = src/libzmq.la tests_test_ancillaries_LDADD = src/libzmq.la
...@@ -642,7 +649,8 @@ tests_test_sodium_SOURCES = tests/test_sodium.cpp ...@@ -642,7 +649,8 @@ tests_test_sodium_SOURCES = tests/test_sodium.cpp
tests_test_sodium_LDADD = src/libzmq.la tests_test_sodium_LDADD = src/libzmq.la
tests_test_socket_null_SOURCES = tests/test_socket_null.cpp tests_test_socket_null_SOURCES = tests/test_socket_null.cpp
tests_test_socket_null_LDADD = src/libzmq.la tests_test_socket_null_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_socket_null_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_reconnect_ivl_SOURCES = tests/test_reconnect_ivl.cpp tests_test_reconnect_ivl_SOURCES = tests/test_reconnect_ivl.cpp
tests_test_reconnect_ivl_LDADD = src/libzmq.la tests_test_reconnect_ivl_LDADD = src/libzmq.la
...@@ -670,8 +678,9 @@ tests_test_security_curve_SOURCES += \ ...@@ -670,8 +678,9 @@ tests_test_security_curve_SOURCES += \
endif endif
tests_test_security_curve_LDADD = \ tests_test_security_curve_LDADD = \
src/libzmq.la $(LIBUNWIND_LIBS) src/libzmq.la ${UNITY_LIBS} $(LIBUNWIND_LIBS)
tests_test_security_curve_CPPFLAGS = \ tests_test_security_curve_CPPFLAGS = \
${UNITY_CPPFLAGS} \
${LIBUNWIND_CFLAGS} ${LIBUNWIND_CFLAGS}
if USE_LIBSODIUM if USE_LIBSODIUM
...@@ -851,6 +860,25 @@ tests_test_dgram_SOURCES = tests/test_dgram.cpp ...@@ -851,6 +860,25 @@ tests_test_dgram_SOURCES = tests/test_dgram.cpp
tests_test_dgram_LDADD = src/libzmq.la tests_test_dgram_LDADD = src/libzmq.la
endif endif
if ENABLE_STATIC
# unit tests - these include individual source files and test the internal functions
test_apps += \
unittests/unittest_poller \
unittests/unittest_ypipe
unittests_unittest_poller_SOURCES = unittests/unittest_poller.cpp
unittests_unittest_poller_CPPFLAGS = -I$(top_srcdir)/src ${UNITY_CPPFLAGS}
unittests_unittest_poller_LDADD = $(top_builddir)/src/.libs/libzmq.a \
${src_libzmq_la_LIBADD} \
${UNITY_LIBS}
unittests_unittest_ypipe_SOURCES = unittests/unittest_ypipe.cpp
unittests_unittest_ypipe_CPPFLAGS = -I$(top_srcdir)/src ${UNITY_CPPFLAGS}
unittests_unittest_ypipe_LDADD = $(top_builddir)/src/.libs/libzmq.a \
${src_libzmq_la_LIBADD} \
${UNITY_LIBS}
endif
check_PROGRAMS = ${test_apps} check_PROGRAMS = ${test_apps}
# Run the test cases # Run the test cases
...@@ -867,6 +895,8 @@ XFAIL_TESTS += test_ipc_wildcard \ ...@@ -867,6 +895,8 @@ XFAIL_TESTS += test_ipc_wildcard \
endif endif
EXTRA_DIST = \ EXTRA_DIST = \
external/unity/license.txt \
external/unity/version.txt \
CMakeLists.txt \ CMakeLists.txt \
autogen.sh \ autogen.sh \
version.sh \ version.sh \
...@@ -874,6 +904,7 @@ EXTRA_DIST = \ ...@@ -874,6 +904,7 @@ EXTRA_DIST = \
src/libzmq.vers \ src/libzmq.vers \
src/version.rc.in \ src/version.rc.in \
tests/CMakeLists.txt \ tests/CMakeLists.txt \
unittests/CMakeLists.txt \
tools/curve_keygen.cpp tools/curve_keygen.cpp
MAINTAINERCLEANFILES = \ MAINTAINERCLEANFILES = \
......
...@@ -813,6 +813,9 @@ AS_IF([test "$WITH_CLANG_FORMAT" = fail], ...@@ -813,6 +813,9 @@ AS_IF([test "$WITH_CLANG_FORMAT" = fail],
[AC_MSG_ERROR([Caller explicitly referenced CLANG_FORMAT=$CLANG_FORMAT which was not found])]) [AC_MSG_ERROR([Caller explicitly referenced CLANG_FORMAT=$CLANG_FORMAT which was not found])])
AM_CONDITIONAL([WITH_CLANG_FORMAT], [$WITH_CLANG_FORMAT]) AM_CONDITIONAL([WITH_CLANG_FORMAT], [$WITH_CLANG_FORMAT])
# unittests will not build without the static libzmq.a
AM_CONDITIONAL(ENABLE_STATIC, test "x$enable_static" = "xyes")
# Subst LIBZMQ_EXTRA_CFLAGS & CXXFLAGS & LDFLAGS # Subst LIBZMQ_EXTRA_CFLAGS & CXXFLAGS & LDFLAGS
AC_SUBST(LIBZMQ_EXTRA_CFLAGS) AC_SUBST(LIBZMQ_EXTRA_CFLAGS)
AC_SUBST(LIBZMQ_EXTRA_CXXFLAGS) AC_SUBST(LIBZMQ_EXTRA_CXXFLAGS)
......
The MIT License (MIT)
Copyright (c) <year> 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
https://github.com/ThrowTheSwitch/Unity/commit/b4aca70fd9e0ddf0afbdafb1b826f5edcfc1049b
...@@ -44,6 +44,29 @@ Copyright: 2009-2011, 250bpm s.r.o ...@@ -44,6 +44,29 @@ Copyright: 2009-2011, 250bpm s.r.o
2007-2011, Other contributors as noted in the AUTHORS file 2007-2011, Other contributors as noted in the AUTHORS file
License: LGPL-3.0+ License: LGPL-3.0+
Files: external/unity/*
Copyright: 2007-2014 Mike Karlesky
2007-2014 Mark VanderVoord
2007-2014 Greg Williams
License: MIT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Files: debian/* Files: debian/*
Copyright: 2014- , Laszlo Boszormenyi (GCS) <gcs@debian.org> Copyright: 2014- , Laszlo Boszormenyi (GCS) <gcs@debian.org>
2012-2014, Alessandro Ghedini <ghedo@debian.org> 2012-2014, Alessandro Ghedini <ghedo@debian.org>
......
...@@ -76,9 +76,7 @@ zmq::ctx_t::ctx_t () : ...@@ -76,9 +76,7 @@ zmq::ctx_t::ctx_t () :
max_msgsz (INT_MAX), max_msgsz (INT_MAX),
io_thread_count (ZMQ_IO_THREADS_DFLT), io_thread_count (ZMQ_IO_THREADS_DFLT),
blocky (true), blocky (true),
ipv6 (false), ipv6 (false)
thread_priority (ZMQ_THREAD_PRIORITY_DFLT),
thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT)
{ {
#ifdef HAVE_FORK #ifdef HAVE_FORK
pid = getpid (); pid = getpid ();
...@@ -241,29 +239,6 @@ int zmq::ctx_t::set (int option_, int optval_) ...@@ -241,29 +239,6 @@ int zmq::ctx_t::set (int option_, int optval_)
} else if (option_ == ZMQ_IPV6 && optval_ >= 0) { } else if (option_ == ZMQ_IPV6 && optval_ >= 0) {
scoped_lock_t locker (opt_sync); scoped_lock_t locker (opt_sync);
ipv6 = (optval_ != 0); ipv6 = (optval_ != 0);
} else if (option_ == ZMQ_THREAD_PRIORITY && optval_ >= 0) {
scoped_lock_t locker (opt_sync);
thread_priority = optval_;
} else if (option_ == ZMQ_THREAD_SCHED_POLICY && optval_ >= 0) {
scoped_lock_t locker (opt_sync);
thread_sched_policy = optval_;
} else if (option_ == ZMQ_THREAD_AFFINITY_CPU_ADD && optval_ >= 0) {
scoped_lock_t locker (opt_sync);
thread_affinity_cpus.insert (optval_);
} else if (option_ == ZMQ_THREAD_AFFINITY_CPU_REMOVE && optval_ >= 0) {
scoped_lock_t locker (opt_sync);
std::set<int>::iterator it = thread_affinity_cpus.find (optval_);
if (it != thread_affinity_cpus.end ()) {
thread_affinity_cpus.erase (it);
} else {
errno = EINVAL;
rc = -1;
}
} else if (option_ == ZMQ_THREAD_NAME_PREFIX && optval_ >= 0) {
std::ostringstream s;
s << optval_;
scoped_lock_t locker (opt_sync);
thread_name_prefix = s.str ();
} else if (option_ == ZMQ_BLOCKY && optval_ >= 0) { } else if (option_ == ZMQ_BLOCKY && optval_ >= 0) {
scoped_lock_t locker (opt_sync); scoped_lock_t locker (opt_sync);
blocky = (optval_ != 0); blocky = (optval_ != 0);
...@@ -271,8 +246,7 @@ int zmq::ctx_t::set (int option_, int optval_) ...@@ -271,8 +246,7 @@ int zmq::ctx_t::set (int option_, int optval_)
scoped_lock_t locker (opt_sync); scoped_lock_t locker (opt_sync);
max_msgsz = optval_ < INT_MAX ? optval_ : INT_MAX; max_msgsz = optval_ < INT_MAX ? optval_ : INT_MAX;
} else { } else {
errno = EINVAL; rc = thread_ctx_t::set (option_, optval_);
rc = -1;
} }
return rc; return rc;
} }
...@@ -434,9 +408,15 @@ zmq::object_t *zmq::ctx_t::get_reaper () ...@@ -434,9 +408,15 @@ zmq::object_t *zmq::ctx_t::get_reaper ()
return reaper; return reaper;
} }
void zmq::ctx_t::start_thread (thread_t &thread_, zmq::thread_ctx_t::thread_ctx_t () :
thread_fn *tfn_, thread_priority (ZMQ_THREAD_PRIORITY_DFLT),
void *arg_) const thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT)
{
}
void zmq::thread_ctx_t::start_thread (thread_t &thread_,
thread_fn *tfn_,
void *arg_) const
{ {
static unsigned int nthreads_started = 0; static unsigned int nthreads_started = 0;
...@@ -453,6 +433,39 @@ void zmq::ctx_t::start_thread (thread_t &thread_, ...@@ -453,6 +433,39 @@ void zmq::ctx_t::start_thread (thread_t &thread_,
nthreads_started++; nthreads_started++;
} }
int zmq::thread_ctx_t::set (int option_, int optval_)
{
int rc = 0;
if (option_ == ZMQ_THREAD_SCHED_POLICY && optval_ >= 0) {
scoped_lock_t locker (opt_sync);
thread_sched_policy = optval_;
} else if (option_ == ZMQ_THREAD_AFFINITY_CPU_ADD && optval_ >= 0) {
scoped_lock_t locker (opt_sync);
thread_affinity_cpus.insert (optval_);
} else if (option_ == ZMQ_THREAD_AFFINITY_CPU_REMOVE && optval_ >= 0) {
scoped_lock_t locker (opt_sync);
std::set<int>::iterator it = thread_affinity_cpus.find (optval_);
if (it != thread_affinity_cpus.end ()) {
thread_affinity_cpus.erase (it);
} else {
errno = EINVAL;
rc = -1;
}
} else if (option_ == ZMQ_THREAD_NAME_PREFIX && optval_ >= 0) {
std::ostringstream s;
s << optval_;
scoped_lock_t locker (opt_sync);
thread_name_prefix = s.str ();
} else if (option_ == ZMQ_THREAD_PRIORITY && optval_ >= 0) {
scoped_lock_t locker (opt_sync);
thread_priority = optval_;
} else {
errno = EINVAL;
rc = -1;
}
return rc;
}
void zmq::ctx_t::send_command (uint32_t tid_, const command_t &command_) void zmq::ctx_t::send_command (uint32_t tid_, const command_t &command_)
{ {
slots[tid_]->send (command_); slots[tid_]->send (command_);
......
...@@ -61,10 +61,32 @@ struct endpoint_t ...@@ -61,10 +61,32 @@ struct endpoint_t
options_t options; options_t options;
}; };
class thread_ctx_t
{
public:
thread_ctx_t ();
// Start a new thread with proper scheduling parameters.
void start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const;
int set (int option_, int optval_);
protected:
// Synchronisation of access to context options.
mutex_t opt_sync;
private:
// Thread parameters.
int thread_priority;
int thread_sched_policy;
std::set<int> thread_affinity_cpus;
std::string thread_name_prefix;
};
// Context object encapsulates all the global state associated with // Context object encapsulates all the global state associated with
// the library. // the library.
class ctx_t class ctx_t : public thread_ctx_t
{ {
public: public:
// Create the context object. // Create the context object.
...@@ -96,9 +118,6 @@ class ctx_t ...@@ -96,9 +118,6 @@ class ctx_t
zmq::socket_base_t *create_socket (int type_); zmq::socket_base_t *create_socket (int type_);
void destroy_socket (zmq::socket_base_t *socket_); void destroy_socket (zmq::socket_base_t *socket_);
// Start a new thread with proper scheduling parameters.
void start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const;
// Send command to the destination thread. // Send command to the destination thread.
void send_command (uint32_t tid_, const command_t &command_); void send_command (uint32_t tid_, const command_t &command_);
...@@ -215,15 +234,6 @@ class ctx_t ...@@ -215,15 +234,6 @@ class ctx_t
// Is IPv6 enabled on this context? // Is IPv6 enabled on this context?
bool ipv6; bool ipv6;
// Thread parameters.
int thread_priority;
int thread_sched_policy;
std::set<int> thread_affinity_cpus;
std::string thread_name_prefix;
// Synchronisation of access to context options.
mutex_t opt_sync;
ctx_t (const ctx_t &); ctx_t (const ctx_t &);
const ctx_t &operator= (const ctx_t &); const ctx_t &operator= (const ctx_t &);
......
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
#include "config.hpp" #include "config.hpp"
#include "i_poll_events.hpp" #include "i_poll_events.hpp"
zmq::devpoll_t::devpoll_t (const zmq::ctx_t &ctx_) : zmq::devpoll_t::devpoll_t (const zmq::thread_ctx_t &ctx_) :
ctx (ctx_), ctx (ctx_),
stopping (false) stopping (false)
{ {
......
...@@ -52,7 +52,7 @@ class devpoll_t : public poller_base_t ...@@ -52,7 +52,7 @@ class devpoll_t : public poller_base_t
public: public:
typedef fd_t handle_t; typedef fd_t handle_t;
devpoll_t (const ctx_t &ctx_); devpoll_t (const thread_ctx_t &ctx_);
~devpoll_t (); ~devpoll_t ();
// "poller" concept. // "poller" concept.
...@@ -75,7 +75,7 @@ class devpoll_t : public poller_base_t ...@@ -75,7 +75,7 @@ class devpoll_t : public poller_base_t
void loop (); void loop ();
// Reference to ZMQ context. // Reference to ZMQ context.
const ctx_t &ctx; const thread_ctx_t &ctx;
// File descriptor referring to "/dev/poll" pseudo-device. // File descriptor referring to "/dev/poll" pseudo-device.
fd_t devpoll_fd; fd_t devpoll_fd;
......
...@@ -44,7 +44,9 @@ ...@@ -44,7 +44,9 @@
#include "config.hpp" #include "config.hpp"
#include "i_poll_events.hpp" #include "i_poll_events.hpp"
zmq::epoll_t::epoll_t (const zmq::ctx_t &ctx_) : ctx (ctx_), stopping (false) zmq::epoll_t::epoll_t (const zmq::thread_ctx_t &ctx_) :
ctx (ctx_),
stopping (false)
{ {
#ifdef ZMQ_USE_EPOLL_CLOEXEC #ifdef ZMQ_USE_EPOLL_CLOEXEC
// Setting this option result in sane behaviour when exec() functions // Setting this option result in sane behaviour when exec() functions
......
...@@ -55,7 +55,7 @@ class epoll_t : public poller_base_t ...@@ -55,7 +55,7 @@ class epoll_t : public poller_base_t
public: public:
typedef void *handle_t; typedef void *handle_t;
epoll_t (const ctx_t &ctx_); epoll_t (const thread_ctx_t &ctx_);
~epoll_t (); ~epoll_t ();
// "poller" concept. // "poller" concept.
...@@ -78,7 +78,7 @@ class epoll_t : public poller_base_t ...@@ -78,7 +78,7 @@ class epoll_t : public poller_base_t
void loop (); void loop ();
// Reference to ZMQ context. // Reference to ZMQ context.
const ctx_t &ctx; const thread_ctx_t &ctx;
// Main epoll file descriptor // Main epoll file descriptor
fd_t epoll_fd; fd_t epoll_fd;
......
...@@ -54,7 +54,9 @@ ...@@ -54,7 +54,9 @@
#define kevent_udata_t void * #define kevent_udata_t void *
#endif #endif
zmq::kqueue_t::kqueue_t (const zmq::ctx_t &ctx_) : ctx (ctx_), stopping (false) zmq::kqueue_t::kqueue_t (const zmq::thread_ctx_t &ctx_) :
ctx (ctx_),
stopping (false)
{ {
// Create event queue // Create event queue
kqueue_fd = kqueue (); kqueue_fd = kqueue ();
......
...@@ -54,7 +54,7 @@ class kqueue_t : public poller_base_t ...@@ -54,7 +54,7 @@ class kqueue_t : public poller_base_t
public: public:
typedef void *handle_t; typedef void *handle_t;
kqueue_t (const ctx_t &ctx_); kqueue_t (const thread_ctx_t &ctx_);
~kqueue_t (); ~kqueue_t ();
// "poller" concept. // "poller" concept.
...@@ -77,7 +77,7 @@ class kqueue_t : public poller_base_t ...@@ -77,7 +77,7 @@ class kqueue_t : public poller_base_t
void loop (); void loop ();
// Reference to ZMQ context. // Reference to ZMQ context.
const ctx_t &ctx; const thread_ctx_t &ctx;
// File descriptor referring to the kernel event queue. // File descriptor referring to the kernel event queue.
fd_t kqueue_fd; fd_t kqueue_fd;
......
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
#include "config.hpp" #include "config.hpp"
#include "i_poll_events.hpp" #include "i_poll_events.hpp"
zmq::poll_t::poll_t (const zmq::ctx_t &ctx_) : zmq::poll_t::poll_t (const zmq::thread_ctx_t &ctx_) :
ctx (ctx_), ctx (ctx_),
retired (false), retired (false),
stopping (false) stopping (false)
......
...@@ -57,7 +57,7 @@ class poll_t : public poller_base_t ...@@ -57,7 +57,7 @@ class poll_t : public poller_base_t
public: public:
typedef fd_t handle_t; typedef fd_t handle_t;
poll_t (const ctx_t &ctx_); poll_t (const thread_ctx_t &ctx_);
~poll_t (); ~poll_t ();
// "poller" concept. // "poller" concept.
...@@ -82,7 +82,7 @@ class poll_t : public poller_base_t ...@@ -82,7 +82,7 @@ class poll_t : public poller_base_t
void cleanup_retired(); void cleanup_retired();
// Reference to ZMQ context. // Reference to ZMQ context.
const ctx_t &ctx; const thread_ctx_t &ctx;
struct fd_entry_t struct fd_entry_t
{ {
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
#include "config.hpp" #include "config.hpp"
#include "i_poll_events.hpp" #include "i_poll_events.hpp"
zmq::pollset_t::pollset_t (const zmq::ctx_t &ctx_) : zmq::pollset_t::pollset_t (const zmq::thread_ctx_t &ctx_) :
ctx (ctx_), ctx (ctx_),
stopping (false) stopping (false)
{ {
......
...@@ -55,7 +55,7 @@ class pollset_t : public poller_base_t ...@@ -55,7 +55,7 @@ class pollset_t : public poller_base_t
public: public:
typedef void *handle_t; typedef void *handle_t;
pollset_t (const ctx_t &ctx_); pollset_t (const thread_ctx_t &ctx_);
~pollset_t (); ~pollset_t ();
// "poller" concept. // "poller" concept.
...@@ -78,7 +78,7 @@ class pollset_t : public poller_base_t ...@@ -78,7 +78,7 @@ class pollset_t : public poller_base_t
void loop (); void loop ();
// Reference to ZMQ context. // Reference to ZMQ context.
const ctx_t &ctx; const thread_ctx_t &ctx;
// Main pollset file descriptor // Main pollset file descriptor
::pollset_t pollset_fd; ::pollset_t pollset_fd;
......
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
#include <limits> #include <limits>
#include <climits> #include <climits>
zmq::select_t::select_t (const zmq::ctx_t &ctx_) : zmq::select_t::select_t (const zmq::thread_ctx_t &ctx_) :
ctx (ctx_), ctx (ctx_),
#if defined ZMQ_HAVE_WINDOWS #if defined ZMQ_HAVE_WINDOWS
// Fine as long as map is not cleared. // Fine as long as map is not cleared.
......
...@@ -63,7 +63,7 @@ class select_t : public poller_base_t ...@@ -63,7 +63,7 @@ class select_t : public poller_base_t
public: public:
typedef fd_t handle_t; typedef fd_t handle_t;
select_t (const ctx_t &ctx_); select_t (const thread_ctx_t &ctx_);
~select_t (); ~select_t ();
// "poller" concept. // "poller" concept.
...@@ -86,7 +86,7 @@ class select_t : public poller_base_t ...@@ -86,7 +86,7 @@ class select_t : public poller_base_t
void loop (); void loop ();
// Reference to ZMQ context. // Reference to ZMQ context.
const ctx_t &ctx; const thread_ctx_t &ctx;
// Internal state. // Internal state.
struct fds_set_t struct fds_set_t
......
...@@ -59,14 +59,17 @@ void zmq::thread_t::start (thread_fn *tfn_, void *arg_) ...@@ -59,14 +59,17 @@ void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
(HANDLE) _beginthreadex (NULL, 0, &::thread_routine, this, 0, NULL); (HANDLE) _beginthreadex (NULL, 0, &::thread_routine, this, 0, NULL);
#endif #endif
win_assert (descriptor != NULL); win_assert (descriptor != NULL);
started = true;
} }
void zmq::thread_t::stop () void zmq::thread_t::stop ()
{ {
DWORD rc = WaitForSingleObject (descriptor, INFINITE); if (started) {
win_assert (rc != WAIT_FAILED); DWORD rc = WaitForSingleObject (descriptor, INFINITE);
BOOL rc2 = CloseHandle (descriptor); win_assert (rc != WAIT_FAILED);
win_assert (rc2 != 0); BOOL rc2 = CloseHandle (descriptor);
win_assert (rc2 != 0);
}
} }
void zmq::thread_t::setSchedulingParameters ( void zmq::thread_t::setSchedulingParameters (
...@@ -116,12 +119,15 @@ void zmq::thread_t::start (thread_fn *tfn_, void *arg_) ...@@ -116,12 +119,15 @@ void zmq::thread_t::start (thread_fn *tfn_, void *arg_)
arg = arg_; arg = arg_;
int rc = pthread_create (&descriptor, NULL, thread_routine, this); int rc = pthread_create (&descriptor, NULL, thread_routine, this);
posix_assert (rc); posix_assert (rc);
started = true;
} }
void zmq::thread_t::stop () void zmq::thread_t::stop ()
{ {
int rc = pthread_join (descriptor, NULL); if (started) {
posix_assert (rc); int rc = pthread_join (descriptor, NULL);
posix_assert (rc);
}
} }
void zmq::thread_t::setSchedulingParameters ( void zmq::thread_t::setSchedulingParameters (
......
...@@ -52,6 +52,7 @@ class thread_t ...@@ -52,6 +52,7 @@ class thread_t
inline thread_t () : inline thread_t () :
tfn (NULL), tfn (NULL),
arg (NULL), arg (NULL),
started (false),
thread_priority (ZMQ_THREAD_PRIORITY_DFLT), thread_priority (ZMQ_THREAD_PRIORITY_DFLT),
thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT) thread_sched_policy (ZMQ_THREAD_SCHED_POLICY_DFLT)
{ {
...@@ -81,6 +82,8 @@ class thread_t ...@@ -81,6 +82,8 @@ class thread_t
void *arg; void *arg;
private: private:
bool started;
#ifdef ZMQ_HAVE_WINDOWS #ifdef ZMQ_HAVE_WINDOWS
HANDLE descriptor; HANDLE descriptor;
#else #else
......
...@@ -144,11 +144,21 @@ if(WIN32) ...@@ -144,11 +144,21 @@ if(WIN32)
link_libraries(ws2_32.lib) link_libraries(ws2_32.lib)
endif() endif()
add_library (unity
STATIC
"${CMAKE_CURRENT_LIST_DIR}/../external/unity/unity.c"
"${CMAKE_CURRENT_LIST_DIR}/../external/unity/unity.h"
"${CMAKE_CURRENT_LIST_DIR}/../external/unity/unity_internals.h")
set_target_properties (unity PROPERTIES
PUBLIC_HEADER "${CMAKE_CURRENT_LIST_DIR}/../external/unity/unity.h")
target_include_directories (unity
PUBLIC "${CMAKE_CURRENT_LIST_DIR}/../external/unity")
# add library and include dirs for all targets # add library and include dirs for all targets
if (BUILD_SHARED) if (BUILD_SHARED)
link_libraries(libzmq ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) link_libraries(libzmq ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} unity)
else () else ()
link_libraries(libzmq-static ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) link_libraries(libzmq-static ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} unity)
endif () endif ()
include_directories("${CMAKE_SOURCE_DIR}/../include" "${CMAKE_BINARY_DIR}") include_directories("${CMAKE_SOURCE_DIR}/../include" "${CMAKE_BINARY_DIR}")
......
This diff is collapsed.
...@@ -29,54 +29,118 @@ ...@@ -29,54 +29,118 @@
#include "testutil.hpp" #include "testutil.hpp"
#include <unity.h>
void setUp ()
{
}
void tearDown ()
{
}
// tests all socket-related functions with a NULL socket argument // tests all socket-related functions with a NULL socket argument
int main (void) void test_zmq_socket_null_context ()
{ {
void *s = zmq_socket (NULL, ZMQ_PAIR); void *s = zmq_socket (NULL, ZMQ_PAIR);
assert (s == NULL); TEST_ASSERT_NULL (s);
assert (errno == EFAULT); // TODO use EINVAL instead? TEST_ASSERT_EQUAL_INT (EFAULT, errno); // TODO use EINVAL instead?
}
void test_zmq_close_null_socket ()
{
int rc = zmq_close (NULL); int rc = zmq_close (NULL);
assert (rc == -1); TEST_ASSERT_EQUAL_INT (-1, rc);
assert (errno == ENOTSOCK); // TODO use EINVAL instead? TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
void test_zmq_setsockopt_null_socket ()
{
int hwm = 100; int hwm = 100;
size_t hwm_size = sizeof hwm; size_t hwm_size = sizeof hwm;
rc = zmq_setsockopt (NULL, ZMQ_SNDHWM, &hwm, hwm_size); int rc = zmq_setsockopt (NULL, ZMQ_SNDHWM, &hwm, hwm_size);
assert (rc == -1); TEST_ASSERT_EQUAL_INT (-1, rc);
assert (errno == ENOTSOCK); // TODO use EINVAL instead? TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
rc = zmq_getsockopt (NULL, ZMQ_SNDHWM, &hwm, &hwm_size); void test_zmq_getsockopt_null_socket ()
assert (rc == -1); {
assert (errno == ENOTSOCK); // TODO use EINVAL instead? int hwm;
size_t hwm_size = sizeof hwm;
int rc = zmq_getsockopt (NULL, ZMQ_SNDHWM, &hwm, &hwm_size);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
rc = zmq_socket_monitor (NULL, "inproc://monitor", ZMQ_EVENT_ALL); void test_zmq_socket_monitor_null_socket ()
assert (rc == -1); {
assert (errno == ENOTSOCK); // TODO use EINVAL instead? int rc = zmq_socket_monitor (NULL, "inproc://monitor", ZMQ_EVENT_ALL);
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
#ifdef ZMQ_BUILD_DRAFT_API #ifdef ZMQ_BUILD_DRAFT_API
rc = zmq_join (NULL, "group"); void test_zmq_join_null_socket ()
assert (rc == -1); {
assert (errno == ENOTSOCK); // TODO use EINVAL instead? int rc = zmq_join (NULL, "group");
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
rc = zmq_leave (NULL, "group"); void test_zmq_leave_null_socket ()
assert (rc == -1); {
assert (errno == ENOTSOCK); // TODO use EINVAL instead? int rc = zmq_leave (NULL, "group");
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
#endif #endif
rc = zmq_bind (NULL, "inproc://socket");
assert (rc == -1);
assert (errno == ENOTSOCK); // TODO use EINVAL instead?
rc = zmq_connect (NULL, "inproc://socket"); void test_zmq_bind_null_socket ()
assert (rc == -1); {
assert (errno == ENOTSOCK); // TODO use EINVAL instead? int rc = zmq_bind (NULL, "inproc://socket");
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
void test_zmq_connect_null_socket ()
{
int rc = zmq_connect (NULL, "inproc://socket");
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
rc = zmq_unbind (NULL, "inproc://socket"); void test_zmq_unbind_null_socket ()
assert (rc == -1); {
assert (errno == ENOTSOCK); // TODO use EINVAL instead? int rc = zmq_unbind (NULL, "inproc://socket");
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
void test_zmq_disconnect_null_socket ()
{
int rc = zmq_disconnect (NULL, "inproc://socket");
TEST_ASSERT_EQUAL_INT (-1, rc);
TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno); // TODO use EINVAL instead?
}
int main (void)
{
UNITY_BEGIN ();
RUN_TEST (test_zmq_socket_null_context);
RUN_TEST (test_zmq_close_null_socket);
RUN_TEST (test_zmq_setsockopt_null_socket);
RUN_TEST (test_zmq_getsockopt_null_socket);
RUN_TEST (test_zmq_socket_monitor_null_socket);
RUN_TEST (test_zmq_bind_null_socket);
RUN_TEST (test_zmq_connect_null_socket);
RUN_TEST (test_zmq_unbind_null_socket);
RUN_TEST (test_zmq_disconnect_null_socket);
#ifdef ZMQ_BUILD_DRAFT_API
RUN_TEST (test_zmq_join_null_socket);
RUN_TEST (test_zmq_leave_null_socket);
#endif
rc = zmq_disconnect (NULL, "inproc://socket"); return UNITY_END ();
assert (rc == -1);
assert (errno == ENOTSOCK); // TODO use EINVAL instead?
} }
# CMake build script for ZeroMQ unit tests
cmake_minimum_required(VERSION "2.8.1")
set(unittests
unittest_ypipe
unittest_poller
)
#IF (ENABLE_DRAFTS)
# list(APPEND tests
# )
#ENDIF (ENABLE_DRAFTS)
# add location of platform.hpp for Windows builds
if(WIN32)
add_definitions(-DZMQ_CUSTOM_PLATFORM_HPP)
add_definitions(-D_WINSOCK_DEPRECATED_NO_WARNINGS)
# Same name on 64bit systems
link_libraries(ws2_32.lib)
endif()
include_directories("${CMAKE_SOURCE_DIR}/include" "${CMAKE_SOURCE_DIR}/src" "${CMAKE_BINARY_DIR}")
foreach(test ${unittests})
# target_sources not supported before CMake 3.1
add_executable(${test} ${test}.cpp)
# per-test directories not generated on OS X / Darwin
if (NOT ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang.*")
link_directories(${test} PRIVATE "${CMAKE_SOURCE_DIR}/../lib")
endif()
target_link_libraries(${test} libzmq-static ${OPTIONAL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} unity)
if(RT_LIBRARY)
target_link_libraries(${test} ${RT_LIBRARY})
endif()
if(WIN32)
add_test(NAME ${test} WORKING_DIRECTORY ${LIBRARY_OUTPUT_PATH} COMMAND ${test})
else()
add_test(NAME ${test} COMMAND ${test})
endif()
set_tests_properties(${test} PROPERTIES TIMEOUT 10)
# TODO prevent libzmq (non-static) being in the list of link libraries at all
get_target_property(LIBS ${test} LINK_LIBRARIES)
list(REMOVE_ITEM LIBS libzmq)
message("Link libraries of ${test}: ${LIBS}")
set_target_properties(${test} PROPERTIES LINK_LIBRARIES "${LIBS}")
endforeach()
#Check whether all tests in the current folder are present
#TODO duplicated with tests/CMakeLists.txt, define as a function?
file(READ "${CMAKE_CURRENT_LIST_FILE}" CURRENT_LIST_FILE_CONTENT)
file(GLOB ALL_TEST_SOURCES "test_*.cpp")
foreach(TEST_SOURCE ${ALL_TEST_SOURCES})
get_filename_component(TESTNAME "${TEST_SOURCE}" NAME_WE)
string(REGEX MATCH "${TESTNAME}" MATCH_TESTNAME "${CURRENT_LIST_FILE_CONTENT}")
if (NOT MATCH_TESTNAME)
message(AUTHOR_WARNING "Test '${TESTNAME}' is not known to CTest.")
endif()
endforeach()
/*
Copyright (c) 2018 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 <http://www.gnu.org/licenses/>.
*/
#include "../tests/testutil.hpp"
#include <poller.hpp>
#include <unity.h>
void setUp ()
{
}
void tearDown ()
{
}
void test_create ()
{
zmq::thread_ctx_t thread_ctx;
zmq::poller_t poller (thread_ctx);
}
int main (void)
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_create);
return UNITY_END ();
}
/*
Copyright (c) 2018 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 <http://www.gnu.org/licenses/>.
*/
#include "../tests/testutil.hpp"
#include <ypipe.hpp>
#include <unity.h>
void setUp ()
{
}
void tearDown ()
{
}
void test_create ()
{
zmq::ypipe_t<int, 1> ypipe;
}
int main (void)
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_create);
return UNITY_END ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment