mirror of
https://github.com/zeromq/cppzmq.git
synced 2025-05-28 23:24:12 +02:00
Merge pull request #255 from kurdybacha/issue_fix
Problem: #209 and monitor_t tests not event driven
This commit is contained in:
commit
73f171abb2
@ -30,9 +30,7 @@ if(NOT ZeroMQ_FOUND)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(NOT TARGET @PROJECT_NAME@)
|
if(NOT TARGET @PROJECT_NAME@)
|
||||||
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
|
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
|
||||||
|
get_target_property(@PROJECT_NAME@_INCLUDE_DIR cppzmq INTERFACE_INCLUDE_DIRECTORIES)
|
||||||
get_target_property(@PROJECT_NAME@_INCLUDE_DIR cppzmq INTERFACE_INCLUDE_DIRECTORIES)
|
|
||||||
get_target_property(@PROJECT_NAME@_LIBRARY libzmq LOCATION)
|
|
||||||
get_target_property(@PROJECT_NAME@_STATIC_LIBRARY libzmq-static LOCATION)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
#include <gtest/gtest.h>
|
#include "testutil.hpp"
|
||||||
#include <gmock/gmock.h>
|
|
||||||
#include <zmq.hpp>
|
|
||||||
|
|
||||||
|
#include <gmock/gmock.h>
|
||||||
#ifdef ZMQ_CPP11
|
#ifdef ZMQ_CPP11
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class mock_monitor_t : public zmq::monitor_t
|
class mock_monitor_t : public zmq::monitor_t
|
||||||
@ -18,63 +19,65 @@ TEST(monitor, create_destroy)
|
|||||||
zmq::monitor_t monitor;
|
zmq::monitor_t monitor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(ZMQ_CPP11)
|
||||||
TEST(monitor, init_check)
|
TEST(monitor, init_check)
|
||||||
{
|
{
|
||||||
zmq::context_t ctx;
|
common_server_client_setup s{false};
|
||||||
zmq::socket_t bind_socket(ctx, ZMQ_DEALER);
|
|
||||||
|
|
||||||
bind_socket.bind("tcp://127.0.0.1:*");
|
|
||||||
char endpoint[255];
|
|
||||||
size_t endpoint_len = sizeof(endpoint);
|
|
||||||
bind_socket.getsockopt(ZMQ_LAST_ENDPOINT, &endpoint, &endpoint_len);
|
|
||||||
|
|
||||||
zmq::socket_t connect_socket(ctx, ZMQ_DEALER);
|
|
||||||
|
|
||||||
mock_monitor_t monitor;
|
mock_monitor_t monitor;
|
||||||
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
|
|
||||||
.Times(testing::AtLeast(1));
|
|
||||||
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
|
|
||||||
.Times(testing::AtLeast(1));
|
|
||||||
|
|
||||||
monitor.init(connect_socket, "inproc://foo");
|
const int expected_event_count = 2;
|
||||||
|
int event_count = 0;
|
||||||
|
auto count_event = [&event_count](const zmq_event_t &, const char *) {
|
||||||
|
++event_count;
|
||||||
|
};
|
||||||
|
|
||||||
|
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(testing::Invoke(count_event));
|
||||||
|
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(testing::Invoke(count_event));
|
||||||
|
|
||||||
|
monitor.init(s.client, "inproc://foo");
|
||||||
|
|
||||||
ASSERT_FALSE(monitor.check_event(0));
|
ASSERT_FALSE(monitor.check_event(0));
|
||||||
connect_socket.connect(endpoint);
|
s.init();
|
||||||
|
|
||||||
while (monitor.check_event(100)) {
|
while (monitor.check_event(100) && event_count < expected_event_count) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ZMQ_CPP11
|
|
||||||
TEST(monitor, init_abort)
|
TEST(monitor, init_abort)
|
||||||
{
|
{
|
||||||
zmq::context_t ctx;
|
common_server_client_setup s(false);
|
||||||
zmq::socket_t bind_socket(ctx, zmq::socket_type::dealer);
|
|
||||||
|
|
||||||
bind_socket.bind("tcp://127.0.0.1:*");
|
|
||||||
char endpoint[255];
|
|
||||||
size_t endpoint_len = sizeof(endpoint);
|
|
||||||
bind_socket.getsockopt(ZMQ_LAST_ENDPOINT, &endpoint, &endpoint_len);
|
|
||||||
|
|
||||||
zmq::socket_t connect_socket(ctx, zmq::socket_type::dealer);
|
|
||||||
|
|
||||||
mock_monitor_t monitor;
|
mock_monitor_t monitor;
|
||||||
monitor.init(connect_socket, "inproc://foo");
|
monitor.init(s.client, "inproc://foo");
|
||||||
|
|
||||||
|
std::mutex mutex;
|
||||||
|
std::condition_variable cond_var;
|
||||||
|
bool done{false};
|
||||||
|
|
||||||
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
|
EXPECT_CALL(monitor, on_event_connect_delayed(testing::_, testing::_))
|
||||||
.Times(testing::AtLeast(1));
|
.Times(1);
|
||||||
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
|
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
|
||||||
.Times(testing::AtLeast(1));
|
.Times(1)
|
||||||
|
.WillOnce(testing::Invoke([&](const zmq_event_t &, const char *) {
|
||||||
|
std::lock_guard<std::mutex> lock(mutex);
|
||||||
|
done = true;
|
||||||
|
cond_var.notify_one();
|
||||||
|
}));
|
||||||
|
|
||||||
auto thread = std::thread([&monitor] {
|
auto thread = std::thread([&monitor] {
|
||||||
while (monitor.check_event(-1)) {
|
while (monitor.check_event(-1)) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
connect_socket.connect(endpoint);
|
s.init();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
{
|
||||||
// TODO instead of sleeping an arbitrary amount of time, we should better
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
// wait until the expectations have met. How can this be done with
|
EXPECT_TRUE(cond_var.wait_for(lock, std::chrono::seconds(1),
|
||||||
// googlemock?
|
[&done] { return done; }));
|
||||||
|
}
|
||||||
|
|
||||||
monitor.abort();
|
monitor.abort();
|
||||||
thread.join();
|
thread.join();
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <zmq.hpp>
|
#include <zmq.hpp>
|
||||||
|
|
||||||
#if defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11)
|
#if defined(ZMQ_CPP11)
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
class loopback_ip4_binder
|
class loopback_ip4_binder
|
||||||
@ -31,7 +31,11 @@ class loopback_ip4_binder
|
|||||||
|
|
||||||
struct common_server_client_setup
|
struct common_server_client_setup
|
||||||
{
|
{
|
||||||
common_server_client_setup() { init(); }
|
common_server_client_setup(bool initialize = true)
|
||||||
|
{
|
||||||
|
if (initialize)
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
void init()
|
void init()
|
||||||
{
|
{
|
||||||
@ -40,8 +44,8 @@ struct common_server_client_setup
|
|||||||
}
|
}
|
||||||
|
|
||||||
zmq::context_t context;
|
zmq::context_t context;
|
||||||
zmq::socket_t server{context, zmq::socket_type::server};
|
zmq::socket_t server{context, zmq::socket_type::pair};
|
||||||
zmq::socket_t client{context, zmq::socket_type::client};
|
zmq::socket_t client{context, zmq::socket_type::pair};
|
||||||
std::string endpoint;
|
std::string endpoint;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user