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));
69 *sock2 = std::move(*sock1);
88 BOOST_REQUIRE_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0,
s), 0);
93 const char*
msg =
"abcd";
94 constexpr ssize_t msg_len = 4;
112 Sock* sock0moved =
new Sock(std::move(*sock0));
114 *sock1moved = std::move(*sock1);
136 std::thread waiter([&sock0]() { (void)sock0.
Wait(24h,
Sock::RECV); });
138 BOOST_REQUIRE_EQUAL(sock1.
Send(
"a", 1, 0), 1);
145 constexpr auto timeout = 1min;
150 Sock sock_send(
s[0]);
151 Sock sock_recv(
s[1]);
153 std::thread receiver([&sock_recv, &timeout, &interrupt]() {
154 constexpr size_t max_data{10};
155 bool threw_as_expected{
false};
160 }
catch (
const std::runtime_error& e) {
161 threw_as_expected =
HasReason(
"too many bytes without a terminator")(e);
163 assert(threw_as_expected);
166 BOOST_REQUIRE_NO_THROW(sock_send.
SendComplete(
"1234567", timeout, interrupt));
167 BOOST_REQUIRE_NO_THROW(sock_send.
SendComplete(
"89a\n", timeout, interrupt));
A helper class for interruptible sleeps.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
virtual ssize_t Send(const void *data, size_t len, int flags) const
send(2) wrapper.
virtual void SendComplete(Span< const unsigned char > 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).
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
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_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
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()