Debian Codegraph @

tests/testutil.hpp

raw
1 #pragma once
2
3 #include <catch2/catch.hpp>
4 #include <zmq.hpp>
5
6 #if defined(ZMQ_CPP11)
7
8 inline std::string bind_ip4_loopback(zmq::socket_t &socket)
9 {
10 socket.bind("tcp://127.0.0.1:*");
11 std::string endpoint(100, ' ');
12 endpoint.resize(socket.get(zmq::sockopt::last_endpoint, zmq::buffer(endpoint)));
13 return endpoint;
14 }
15
16 struct common_server_client_setup
17 {
18 common_server_client_setup(bool initialize = true)
19 {
20 if (initialize)
21 init();
22 }
23
24 void init()
25 {
26 endpoint = bind_ip4_loopback(server);
27 REQUIRE_NOTHROW(client.connect(endpoint));
28 }
29
30 zmq::context_t context;
31 zmq::socket_t server{context, zmq::socket_type::pair};
32 zmq::socket_t client{context, zmq::socket_type::pair};
33 std::string endpoint;
34 };
35 #endif
36
37 #define CHECK_THROWS_ZMQ_ERROR(ecode, expr) \
38 do { \
39 try { \
40 expr; \
41 CHECK(false); \
42 } \
43 catch (const zmq::error_t &ze) { \
44 INFO(std::string("Unexpected error code: ") + ze.what()); \
45 CHECK(ze.num() == ecode); \
46 } \
47 catch (const std::exception &ex) { \
48 INFO(std::string("Unexpected exception: ") + ex.what()); \
49 CHECK(false); \
50 } \
51 catch (...) { \
52 CHECK(false); \
53 } \
54 } while (false)