2018-07-05 00:46:47 +02:00
|
|
|
#include "testutil.hpp"
|
2018-06-04 10:41:17 +02:00
|
|
|
|
2018-06-05 10:18:05 +02:00
|
|
|
#ifdef ZMQ_CPP11
|
|
|
|
#include <thread>
|
2018-07-05 00:46:47 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2019-04-02 16:01:10 +02:00
|
|
|
#include <functional>
|
2018-06-05 10:18:05 +02:00
|
|
|
|
2018-06-04 10:41:17 +02:00
|
|
|
class mock_monitor_t : public zmq::monitor_t
|
|
|
|
{
|
2020-01-26 10:09:20 +01:00
|
|
|
public:
|
2018-09-23 18:14:25 +02:00
|
|
|
void on_event_connect_delayed(const zmq_event_t &, const char *) ZMQ_OVERRIDE
|
|
|
|
{
|
|
|
|
++connect_delayed;
|
|
|
|
++total;
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_event_connected(const zmq_event_t &, const char *) ZMQ_OVERRIDE
|
|
|
|
{
|
|
|
|
++connected;
|
|
|
|
++total;
|
|
|
|
}
|
|
|
|
|
|
|
|
int total{0};
|
|
|
|
int connect_delayed{0};
|
|
|
|
int connected{0};
|
2018-06-04 10:41:17 +02:00
|
|
|
};
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_CASE("monitor create destroy", "[monitor]")
|
2018-06-04 10:41:17 +02:00
|
|
|
{
|
|
|
|
zmq::monitor_t monitor;
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:46:47 +02:00
|
|
|
#if defined(ZMQ_CPP11)
|
2019-04-02 20:11:59 +02:00
|
|
|
TEST_CASE("monitor move construct", "[monitor]")
|
|
|
|
{
|
|
|
|
zmq::context_t ctx;
|
|
|
|
zmq::socket_t sock(ctx, ZMQ_DEALER);
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("move ctor empty") {
|
2019-04-02 20:11:59 +02:00
|
|
|
zmq::monitor_t monitor1;
|
|
|
|
zmq::monitor_t monitor2 = std::move(monitor1);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("move ctor init") {
|
2019-04-02 20:11:59 +02:00
|
|
|
zmq::monitor_t monitor1;
|
|
|
|
monitor1.init(sock, "inproc://monitor-client");
|
|
|
|
zmq::monitor_t monitor2 = std::move(monitor1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("monitor move assign", "[monitor]")
|
|
|
|
{
|
|
|
|
zmq::context_t ctx;
|
|
|
|
zmq::socket_t sock(ctx, ZMQ_DEALER);
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("move assign empty") {
|
|
|
|
zmq::monitor_t monitor1;
|
|
|
|
zmq::monitor_t monitor2;
|
|
|
|
monitor1 = std::move(monitor2);
|
2019-04-02 20:11:59 +02:00
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("move assign init") {
|
2019-04-02 20:11:59 +02:00
|
|
|
zmq::monitor_t monitor1;
|
|
|
|
monitor1.init(sock, "inproc://monitor-client");
|
|
|
|
zmq::monitor_t monitor2;
|
|
|
|
monitor2 = std::move(monitor1);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("move assign init both") {
|
2019-04-02 20:11:59 +02:00
|
|
|
zmq::monitor_t monitor1;
|
|
|
|
monitor1.init(sock, "inproc://monitor-client");
|
|
|
|
zmq::monitor_t monitor2;
|
|
|
|
zmq::socket_t sock2(ctx, ZMQ_DEALER);
|
|
|
|
monitor2.init(sock2, "inproc://monitor-client2");
|
|
|
|
monitor2 = std::move(monitor1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("monitor init event count", "[monitor]")
|
2018-06-04 10:41:17 +02:00
|
|
|
{
|
2018-07-05 00:46:47 +02:00
|
|
|
common_server_client_setup s{false};
|
|
|
|
mock_monitor_t monitor;
|
2018-06-04 10:41:17 +02:00
|
|
|
|
2018-07-05 00:46:47 +02:00
|
|
|
const int expected_event_count = 2;
|
|
|
|
monitor.init(s.client, "inproc://foo");
|
2018-06-04 10:41:17 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK_FALSE(monitor.check_event(0));
|
2018-07-05 00:46:47 +02:00
|
|
|
s.init();
|
2018-06-04 10:41:17 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
while (monitor.check_event(100) && monitor.total < expected_event_count) {
|
2018-06-04 10:41:17 +02:00
|
|
|
}
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(monitor.connect_delayed == 1);
|
|
|
|
CHECK(monitor.connected == 1);
|
2019-04-14 23:34:15 +02:00
|
|
|
CHECK(monitor.total == expected_event_count);
|
2018-06-04 10:41:17 +02:00
|
|
|
}
|
2018-06-05 10:18:05 +02:00
|
|
|
|
2018-09-23 18:14:25 +02:00
|
|
|
TEST_CASE("monitor init abort", "[monitor]")
|
2018-06-05 10:18:05 +02:00
|
|
|
{
|
2018-09-23 18:14:25 +02:00
|
|
|
class mock_monitor : public mock_monitor_t
|
|
|
|
{
|
2020-01-26 10:09:20 +01:00
|
|
|
public:
|
|
|
|
mock_monitor(std::function<void(void)> handle_connected) :
|
|
|
|
handle_connected{std::move(handle_connected)}
|
|
|
|
{
|
|
|
|
}
|
2018-09-23 18:14:25 +02:00
|
|
|
|
|
|
|
void on_event_connected(const zmq_event_t &e, const char *m) ZMQ_OVERRIDE
|
|
|
|
{
|
|
|
|
mock_monitor_t::on_event_connected(e, m);
|
|
|
|
handle_connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::function<void(void)> handle_connected;
|
|
|
|
};
|
|
|
|
|
2018-07-05 00:46:47 +02:00
|
|
|
common_server_client_setup s(false);
|
2018-06-05 10:18:05 +02:00
|
|
|
|
2018-07-05 00:46:47 +02:00
|
|
|
std::mutex mutex;
|
|
|
|
std::condition_variable cond_var;
|
|
|
|
bool done{false};
|
2018-06-05 10:18:05 +02:00
|
|
|
|
2020-01-26 10:09:20 +01:00
|
|
|
mock_monitor monitor([&]()
|
|
|
|
{
|
2018-07-05 00:46:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
done = true;
|
|
|
|
cond_var.notify_one();
|
2018-09-23 18:14:25 +02:00
|
|
|
});
|
|
|
|
monitor.init(s.client, "inproc://foo");
|
2018-06-05 10:18:05 +02:00
|
|
|
|
2020-01-26 10:09:20 +01:00
|
|
|
auto thread = std::thread([&monitor]
|
|
|
|
{
|
2018-06-05 10:18:05 +02:00
|
|
|
while (monitor.check_event(-1)) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-05 00:46:47 +02:00
|
|
|
s.init();
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(cond_var.wait_for(lock, std::chrono::seconds(1),
|
2018-07-05 00:46:47 +02:00
|
|
|
[&done] { return done; }));
|
|
|
|
}
|
2018-09-23 18:14:25 +02:00
|
|
|
CHECK(monitor.connect_delayed == 1);
|
|
|
|
CHECK(monitor.connected == 1);
|
2018-06-05 10:18:05 +02:00
|
|
|
monitor.abort();
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
#endif
|