2024-03-05 11:56:14 +01:00
|
|
|
#include <catch2/catch_all.hpp>
|
2019-11-09 13:40:02 +01:00
|
|
|
#include <zmq_addon.hpp>
|
2019-10-26 22:26:17 +02:00
|
|
|
#ifdef ZMQ_CPP11
|
|
|
|
|
2019-10-26 20:29:22 +02:00
|
|
|
#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");
|
|
|
|
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("send 0 messages") {
|
2019-10-26 20:29:22 +02:00
|
|
|
std::vector<zmq::message_t> imsgs;
|
|
|
|
auto iret = zmq::send_multipart(input, imsgs);
|
|
|
|
REQUIRE(iret);
|
|
|
|
CHECK(*iret == 0);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("send 1 message") {
|
2019-10-26 20:29:22 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("send 2 messages") {
|
2019-10-26 20:29:22 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("send 2 messages, const_buffer") {
|
|
|
|
std::array<zmq::const_buffer, 2> imsgs = {zmq::str_buffer("foo"),
|
|
|
|
zmq::str_buffer("bar!")};
|
2019-10-26 20:29:22 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("send 2 messages, mutable_buffer") {
|
2019-10-26 20:29:22 +02:00
|
|
|
char buf[4] = {};
|
2020-01-26 10:09:20 +01:00
|
|
|
std::array<zmq::mutable_buffer, 2> imsgs = {
|
|
|
|
zmq::buffer(buf, 3), zmq::buffer(buf)};
|
2019-10-26 20:29:22 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("send 2 messages, dontwait") {
|
2019-10-26 20:29:22 +02:00
|
|
|
zmq::socket_t push(context, ZMQ_PUSH);
|
|
|
|
push.bind("inproc://multipart.test.push");
|
2020-01-26 10:09:20 +01:00
|
|
|
|
2019-10-26 20:29:22 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("send, misc. containers") {
|
2019-10-26 20:29:22 +02:00
|
|
|
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
|
2020-01-26 10:09:20 +01:00
|
|
|
iret = zmq::send_multipart(input,
|
|
|
|
std::initializer_list<zmq::const_buffer>{
|
|
|
|
zmq::str_buffer("foo"),
|
|
|
|
zmq::str_buffer("bar!")});
|
2019-10-26 20:29:22 +02:00
|
|
|
REQUIRE(iret);
|
|
|
|
CHECK(*iret == 2);
|
|
|
|
}
|
2020-01-26 10:09:20 +01:00
|
|
|
SECTION("send with invalid socket") {
|
2019-10-26 20:29:22 +02:00
|
|
|
std::vector<zmq::message_t> msgs(1);
|
2020-01-26 10:09:20 +01:00
|
|
|
CHECK_THROWS_AS(zmq::send_multipart(zmq::socket_ref(), msgs),
|
2021-01-02 10:36:02 +01:00
|
|
|
zmq::error_t);
|
2019-10-26 20:29:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|