mirror of
https://github.com/zeromq/cppzmq.git
synced 2025-10-15 23:20:09 +02:00
Problem: Dependency on googletest framework
Currently cppzmq as relatively simple and header only library depends on rather complex unit test framework googletest. Current issues: - Googletest requires downloading and building it every time on travis as cache support is limited there - Googletest build is signifficant with comparison to cppzmq unittests total runtime Solution: Port existing tests to Catch - header only C++ framework and gain ~20% build speed up on travis. Why Catch? It is well know C++ header only testing framework. It works well, it is being kept up to date and maintainers seem to pay attention to community's comments and issues. We can not use Catch2 currently as we still support pre-C++11 compilers.
This commit is contained in:
@@ -1,26 +1,39 @@
|
||||
#include "testutil.hpp"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#ifdef ZMQ_CPP11
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#endif
|
||||
|
||||
class mock_monitor_t : public zmq::monitor_t
|
||||
{
|
||||
public:
|
||||
MOCK_METHOD2(on_event_connect_delayed, void(const zmq_event_t &, const char *));
|
||||
MOCK_METHOD2(on_event_connected, void(const zmq_event_t &, const char *));
|
||||
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};
|
||||
};
|
||||
|
||||
TEST(monitor, create_destroy)
|
||||
#endif
|
||||
|
||||
TEST_CASE("monitor create destroy", "[monitor]")
|
||||
{
|
||||
zmq::monitor_t monitor;
|
||||
}
|
||||
|
||||
#if defined(ZMQ_CPP11)
|
||||
TEST(monitor, init_check)
|
||||
TEST_CASE("monitor init event count", "[monitor]")
|
||||
{
|
||||
common_server_client_setup s{false};
|
||||
mock_monitor_t monitor;
|
||||
@@ -31,41 +44,48 @@ TEST(monitor, init_check)
|
||||
++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));
|
||||
CHECK_FALSE(monitor.check_event(0));
|
||||
s.init();
|
||||
|
||||
while (monitor.check_event(100) && event_count < expected_event_count) {
|
||||
while (monitor.check_event(100) && monitor.total < expected_event_count) {
|
||||
}
|
||||
CHECK(monitor.connect_delayed == 1);
|
||||
CHECK(monitor.connected == 1);
|
||||
}
|
||||
|
||||
TEST(monitor, init_abort)
|
||||
TEST_CASE("monitor init abort", "[monitor]")
|
||||
{
|
||||
class mock_monitor : public mock_monitor_t
|
||||
{
|
||||
public:
|
||||
mock_monitor(std::function<void(void)> handle_connected)
|
||||
: handle_connected{std::move(handle_connected)}
|
||||
{}
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
common_server_client_setup s(false);
|
||||
mock_monitor_t monitor;
|
||||
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::_))
|
||||
.Times(1);
|
||||
EXPECT_CALL(monitor, on_event_connected(testing::_, testing::_))
|
||||
.Times(1)
|
||||
.WillOnce(testing::Invoke([&](const zmq_event_t &, const char *) {
|
||||
mock_monitor monitor([&]() {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
done = true;
|
||||
cond_var.notify_one();
|
||||
}));
|
||||
});
|
||||
monitor.init(s.client, "inproc://foo");
|
||||
|
||||
auto thread = std::thread([&monitor] {
|
||||
while (monitor.check_event(-1)) {
|
||||
@@ -75,10 +95,11 @@ TEST(monitor, init_abort)
|
||||
s.init();
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
EXPECT_TRUE(cond_var.wait_for(lock, std::chrono::seconds(1),
|
||||
CHECK(cond_var.wait_for(lock, std::chrono::seconds(1),
|
||||
[&done] { return done; }));
|
||||
}
|
||||
|
||||
CHECK(monitor.connect_delayed == 1);
|
||||
CHECK(monitor.connected == 1);
|
||||
monitor.abort();
|
||||
thread.join();
|
||||
}
|
||||
|
Reference in New Issue
Block a user