11#include <boost/test/unit_test.hpp>
16using namespace std::chrono_literals;
26 socklen_t len =
sizeof(type);
32 const SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
51 Sock* sock2 =
new Sock(std::move(*sock1));
64 *sock2 = std::move(*sock1);
94 BOOST_REQUIRE_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, s), 0);
99 const char* msg =
"abcd";
100 constexpr ssize_t msg_len = 4;
118 Sock* sock0moved =
new Sock(std::move(*sock0));
120 *sock1moved = std::move(*sock1);
142 std::thread waiter([&sock0]() { (void)sock0.
Wait(24h,
Sock::RECV); });
144 BOOST_REQUIRE_EQUAL(sock1.
Send(
"a", 1, 0), 1);
151 constexpr auto timeout = 1min;
156 Sock sock_send(s[0]);
157 Sock sock_recv(s[1]);
159 std::thread receiver([&sock_recv, &timeout, &interrupt]() {
160 constexpr size_t max_data{10};
161 bool threw_as_expected{
false};
166 }
catch (
const std::runtime_error& e) {
167 threw_as_expected =
HasReason(
"too many bytes without a terminator")(e);
169 assert(threw_as_expected);
172 BOOST_REQUIRE_NO_THROW(sock_send.
SendComplete(
"1234567", timeout, interrupt));
173 BOOST_REQUIRE_NO_THROW(sock_send.
SendComplete(
"89a\n", timeout, interrupt));
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
RAII helper class that manages a socket.
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
virtual void SendComplete(const std::string &data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
Send the given data, retrying on transient errors.
virtual bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
Wait for readiness for input (recv) or output (send).
virtual SOCKET Release()
Get the value of the contained socket and drop ownership.
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
virtual SOCKET Get() const
Get the value of the contained socket.
virtual void Reset()
Close if non-empty.
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
virtual std::string RecvUntilTerminator(uint8_t terminator, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt, size_t max_data) const
Read from socket until a terminator character is encountered.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
bool CloseSocket(SOCKET &hSocket)
Close socket and set hSocket to INVALID_SOCKET.
static void CreateSocketPair(int s[2])
static bool SocketIsClosed(const SOCKET &s)
BOOST_AUTO_TEST_CASE(constructor_and_destructor)
static void SendAndRecvMessage(const Sock &sender, const Sock &receiver)
static SOCKET CreateSocket()