2018-05-11 11:43:41 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <zmq.hpp>
|
|
|
|
|
2018-07-05 00:46:47 +02:00
|
|
|
#if defined(ZMQ_CPP11)
|
2018-05-11 11:43:41 +02:00
|
|
|
#include <array>
|
|
|
|
|
|
|
|
class loopback_ip4_binder
|
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
public:
|
2018-05-11 11:43:41 +02:00
|
|
|
loopback_ip4_binder(zmq::socket_t &socket) { bind(socket); }
|
|
|
|
std::string endpoint() { return endpoint_; }
|
2018-05-12 18:28:28 +02:00
|
|
|
|
|
|
|
private:
|
2018-05-11 11:43:41 +02:00
|
|
|
// 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();
|
2018-05-12 18:28:28 +02:00
|
|
|
ASSERT_NO_THROW(
|
|
|
|
socket.getsockopt(ZMQ_LAST_ENDPOINT, endpoint.data(), &endpoint_size));
|
2018-05-11 11:43:41 +02:00
|
|
|
ASSERT_TRUE(endpoint_size < endpoint.size());
|
|
|
|
endpoint_ = std::string{endpoint.data()};
|
|
|
|
}
|
|
|
|
std::string endpoint_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct common_server_client_setup
|
|
|
|
{
|
2018-07-05 00:46:47 +02:00
|
|
|
common_server_client_setup(bool initialize = true)
|
|
|
|
{
|
|
|
|
if (initialize)
|
|
|
|
init();
|
|
|
|
}
|
2018-05-11 11:43:41 +02:00
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
2018-05-12 18:28:28 +02:00
|
|
|
endpoint = loopback_ip4_binder{server}.endpoint();
|
|
|
|
ASSERT_NO_THROW(client.connect(endpoint));
|
2018-05-11 11:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::context_t context;
|
2018-07-05 00:46:47 +02:00
|
|
|
zmq::socket_t server{context, zmq::socket_type::pair};
|
|
|
|
zmq::socket_t client{context, zmq::socket_type::pair};
|
2018-05-11 11:43:41 +02:00
|
|
|
std::string endpoint;
|
|
|
|
};
|
|
|
|
#endif
|