Skip to content

Commit

Permalink
tests(tcp): add test for blocking recv sockets
Browse files Browse the repository at this point in the history
Add test for blocking RECV TCP sockets and add a catch for when we are
trying to receive from a client that has already disconnected.
  • Loading branch information
csegarragonz committed Apr 15, 2024
1 parent 440189f commit b936854
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/transport/tcp/RecvSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ void RecvSocket::recvOne(int conn, uint8_t* buffer, size_t bufferSize)
std::strerror(errno));
throw std::runtime_error("TCP error receiving!");
}
#ifndef FAABRIC_USE_SPINLOCK
if (got == 0 && bufferSize != 0) {
SPDLOG_ERROR(
"TCP socket trying to receive from disconnected client");
throw std::runtime_error("TCP socket client disconnected");
}
#endif

buffer += got;
numRecvd += got;
Expand Down
4 changes: 4 additions & 0 deletions tasks/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def cmake(
coverage=False,
prof=False,
cpu=None,
disable_spinlock=False,
):
"""
Configures the build
Expand Down Expand Up @@ -52,6 +53,9 @@ def cmake(
"-DFAABRIC_USE_SANITISER={}".format(sanitiser),
"-DFAABRIC_SELF_TRACING=ON" if prof else "",
"-DFAABRIC_CODE_COVERAGE=ON" if coverage else "",
"-DFAABRIC_USE_SPINLOCK={}".format(
"OFF" if disable_spinlock else "ON"
),
"-DFAABRIC_TARGET_CPU={}".format(cpu) if cpu else "",
PROJ_ROOT,
]
Expand Down
28 changes: 25 additions & 3 deletions tests/test/transport/test_tcp_sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ TEST_CASE("Test setting socket options", "[transport]")
TEST_CASE("Test send/recv one message using raw TCP sockets", "[transport]")
{
RecvSocket dst(TEST_PORT);
std::latch endLatch(2);

std::vector<int> msg;
bool timeout = false;

SECTION("Small message")
{
Expand All @@ -109,23 +111,43 @@ TEST_CASE("Test send/recv one message using raw TCP sockets", "[transport]")
msg = std::vector<int>(300, 200);
}

SECTION("Do not send a message")
{
msg = std::vector<int>(3, 2);
timeout = true;
}

// Start sending socket thread
std::jthread sendThread([&] {
SendSocket src(LOCALHOST, TEST_PORT);
// Connect to receiving socket
src.dial();

src.sendOne(BYTES(msg.data()), sizeof(int) * msg.size());
if (!timeout) {
src.sendOne(BYTES(msg.data()), sizeof(int) * msg.size());
}

endLatch.arrive_and_wait();
});

// Prepare receiving socket
dst.listen();
int conn = dst.accept();

std::vector<int> actual(msg.size());
dst.recvOne(conn, BYTES(actual.data()), sizeof(int) * actual.size());

REQUIRE(actual == msg);
// To test the timeout we must set the socket as blocking
if (timeout) {
setRecvTimeoutMs(conn, 200);
REQUIRE_THROWS(
dst.recvOne(conn, BYTES(actual.data()), sizeof(int) * actual.size()));
} else {
dst.recvOne(conn, BYTES(actual.data()), sizeof(int) * actual.size());

REQUIRE(actual == msg);
}

endLatch.arrive_and_wait();
}

TEST_CASE("Test using scatter send functionality (sendMany)", "[transport]")
Expand Down

0 comments on commit b936854

Please sign in to comment.