cppzmq/tests/testutil.hpp
Pawel Kurdybacha ae15964907 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.
2018-10-17 15:22:07 +01:00

52 lines
1.3 KiB
C++

#pragma once
#include <catch.hpp>
#include <zmq.hpp>
#if defined(ZMQ_CPP11)
#include <array>
class loopback_ip4_binder
{
public:
loopback_ip4_binder(zmq::socket_t &socket) { bind(socket); }
std::string endpoint() { return endpoint_; }
private:
// Helper function used in constructor
// as Gtest allows ASSERT_* only in void returning functions
// and constructor/destructor are not.
void bind(zmq::socket_t &socket)
{
REQUIRE_NOTHROW(socket.bind("tcp://127.0.0.1:*"));
std::array<char, 100> endpoint{};
size_t endpoint_size = endpoint.size();
REQUIRE_NOTHROW(
socket.getsockopt(ZMQ_LAST_ENDPOINT, endpoint.data(), &endpoint_size));
REQUIRE(endpoint_size < endpoint.size());
endpoint_ = std::string{endpoint.data()};
}
std::string endpoint_;
};
struct common_server_client_setup
{
common_server_client_setup(bool initialize = true)
{
if (initialize)
init();
}
void init()
{
endpoint = loopback_ip4_binder{server}.endpoint();
REQUIRE_NOTHROW(client.connect(endpoint));
}
zmq::context_t context;
zmq::socket_t server{context, zmq::socket_type::pair};
zmq::socket_t client{context, zmq::socket_type::pair};
std::string endpoint;
};
#endif