cppzmq/tests/testutil.hpp
Pawel Kurdybacha 099bcfc4a4 Problem: cppzmq build broken with C++11 compiler and git cloned repo.
Default build, ./ci_build.sh without any arguments, which causes to run
with draft disabled, does not work properly for git cloned repository and
C++11 compiler.

Two issues:
1. For git cloned repository ENABLE_DRAFTS is ON by default but libzmq
compiled build without drafts .Travis did not catch that because default build
runs on non C++11 compiler.
2. testutil.hpp does not build because of missing draft guards.

Solution 1: Remove check for presence of .git for enabling draft API as it
is confusing to use with ENABLE_DRAFTS flag and there should be only one
explicit way to enable draft build.

Solution 2: add missing draft guards in testutil.hpp for server/client
socket in use there.

Solution 3: add extra Travis build covering C++11 compiler and non
draft enabled build.
2018-05-20 07:53:56 +01:00

48 lines
1.3 KiB
C++

#pragma once
#include <gtest/gtest.h>
#include <zmq.hpp>
#if defined(ZMQ_BUILD_DRAFT_API) && 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)
{
ASSERT_NO_THROW(socket.bind("tcp://127.0.0.1:*"));
std::array<char, 100> endpoint{};
size_t endpoint_size = endpoint.size();
ASSERT_NO_THROW(
socket.getsockopt(ZMQ_LAST_ENDPOINT, endpoint.data(), &endpoint_size));
ASSERT_TRUE(endpoint_size < endpoint.size());
endpoint_ = std::string{endpoint.data()};
}
std::string endpoint_;
};
struct common_server_client_setup
{
common_server_client_setup() { init(); }
void init()
{
endpoint = loopback_ip4_binder{server}.endpoint();
ASSERT_NO_THROW(client.connect(endpoint));
}
zmq::context_t context;
zmq::socket_t server{context, zmq::socket_type::server};
zmq::socket_t client{context, zmq::socket_type::client};
std::string endpoint;
};
#endif