forked from zeromq/cppzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_multipart.cpp
121 lines (111 loc) · 4.15 KB
/
send_multipart.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <catch2/catch.hpp>
#include <zmq_addon.hpp>
#ifdef ZMQ_CPP11
#include <forward_list>
TEST_CASE("send_multipart test", "[send_multipart]")
{
zmq::context_t context(1);
zmq::socket_t output(context, ZMQ_PAIR);
zmq::socket_t input(context, ZMQ_PAIR);
output.bind("inproc://multipart.test");
input.connect("inproc://multipart.test");
SECTION("send 0 messages") {
std::vector<zmq::message_t> imsgs;
auto iret = zmq::send_multipart(input, imsgs);
REQUIRE(iret);
CHECK(*iret == 0);
}
SECTION("send 1 message") {
std::array<zmq::message_t, 1> imsgs = {zmq::message_t(3)};
auto iret = zmq::send_multipart(input, imsgs);
REQUIRE(iret);
CHECK(*iret == 1);
std::vector<zmq::message_t> omsgs;
auto oret = zmq::recv_multipart(output, std::back_inserter(omsgs));
REQUIRE(oret);
CHECK(*oret == 1);
REQUIRE(omsgs.size() == 1);
CHECK(omsgs[0].size() == 3);
}
SECTION("send 2 messages") {
std::array<zmq::message_t, 2> imsgs = {zmq::message_t(3), zmq::message_t(4)};
auto iret = zmq::send_multipart(input, imsgs);
REQUIRE(iret);
CHECK(*iret == 2);
std::vector<zmq::message_t> omsgs;
auto oret = zmq::recv_multipart(output, std::back_inserter(omsgs));
REQUIRE(oret);
CHECK(*oret == 2);
REQUIRE(omsgs.size() == 2);
CHECK(omsgs[0].size() == 3);
CHECK(omsgs[1].size() == 4);
}
SECTION("send 2 messages, const_buffer") {
std::array<zmq::const_buffer, 2> imsgs = {zmq::str_buffer("foo"),
zmq::str_buffer("bar!")};
auto iret = zmq::send_multipart(input, imsgs);
REQUIRE(iret);
CHECK(*iret == 2);
std::vector<zmq::message_t> omsgs;
auto oret = zmq::recv_multipart(output, std::back_inserter(omsgs));
REQUIRE(oret);
CHECK(*oret == 2);
REQUIRE(omsgs.size() == 2);
CHECK(omsgs[0].size() == 3);
CHECK(omsgs[1].size() == 4);
}
SECTION("send 2 messages, mutable_buffer") {
char buf[4] = {};
std::array<zmq::mutable_buffer, 2> imsgs = {
zmq::buffer(buf, 3), zmq::buffer(buf)};
auto iret = zmq::send_multipart(input, imsgs);
REQUIRE(iret);
CHECK(*iret == 2);
std::vector<zmq::message_t> omsgs;
auto oret = zmq::recv_multipart(output, std::back_inserter(omsgs));
REQUIRE(oret);
CHECK(*oret == 2);
REQUIRE(omsgs.size() == 2);
CHECK(omsgs[0].size() == 3);
CHECK(omsgs[1].size() == 4);
}
SECTION("send 2 messages, dontwait") {
zmq::socket_t push(context, ZMQ_PUSH);
push.bind("inproc://multipart.test.push");
std::array<zmq::message_t, 2> imsgs = {zmq::message_t(3), zmq::message_t(4)};
auto iret = zmq::send_multipart(push, imsgs, zmq::send_flags::dontwait);
REQUIRE_FALSE(iret);
}
SECTION("send, misc. containers") {
std::vector<zmq::message_t> msgs_vec;
msgs_vec.emplace_back(3);
msgs_vec.emplace_back(4);
auto iret = zmq::send_multipart(input, msgs_vec);
REQUIRE(iret);
CHECK(*iret == 2);
std::forward_list<zmq::message_t> msgs_list;
msgs_list.emplace_front(4);
msgs_list.emplace_front(3);
iret = zmq::send_multipart(input, msgs_list);
REQUIRE(iret);
CHECK(*iret == 2);
// init. list
const auto msgs_il = {zmq::str_buffer("foo"), zmq::str_buffer("bar!")};
iret = zmq::send_multipart(input, msgs_il);
REQUIRE(iret);
CHECK(*iret == 2);
// rvalue
iret = zmq::send_multipart(input,
std::initializer_list<zmq::const_buffer>{
zmq::str_buffer("foo"),
zmq::str_buffer("bar!")});
REQUIRE(iret);
CHECK(*iret == 2);
}
SECTION("send with invalid socket") {
std::vector<zmq::message_t> msgs(1);
CHECK_THROWS_AS(zmq::send_multipart(zmq::socket_ref(), msgs),
zmq::error_t);
}
}
#endif