mirror of
https://github.com/zeromq/cppzmq.git
synced 2024-12-13 02:42:58 +01:00
57454dff4b
Solution: instead of waiting for fixed amount of time for events, react as soon as events are triggered. - Total running time of unittest reduced 10x (from ~300ms to 30ms). - Reduced code duplication by reusing testutil's constructs.
52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <gtest/gtest.h>
|
|
#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)
|
|
{
|
|
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(bool initialize = true)
|
|
{
|
|
if (initialize)
|
|
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::pair};
|
|
zmq::socket_t client{context, zmq::socket_type::pair};
|
|
std::string endpoint;
|
|
};
|
|
#endif
|