Skip to content

Commit

Permalink
Merge pull request pistacheio#1240 from dgreatwood/ErrnoNotEPIPEServe…
Browse files Browse the repository at this point in the history
…rTest

Errno not EPIPE server test
  • Loading branch information
kiplingw authored Sep 10, 2024
2 parents 7f1b84c + d4523f1 commit 3068e11
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ on:
branches:
- master
- macOSRunner
- ErrnoNotEPIPEServerTest
pull_request:
branches:
- master
- macOSRunner
- ErrnoNotEPIPEServerTest

defaults:
run:
Expand Down
4 changes: 2 additions & 2 deletions src/common/reactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace Pistache::Aio
{
std::mutex& poller_reg_unreg_mutex(poller.reg_unreg_mutex_);
GUARD_AND_DBG_LOG(poller_reg_unreg_mutex);

handlers_.forEachHandler([this](
const std::shared_ptr<Handler> handler) {
detachFromReactor(handler);
Expand Down Expand Up @@ -254,7 +254,7 @@ namespace Pistache::Aio
// Note: poller_reg_unreg_mutex is already locked (by
// Listener::run()) before calling here, so it is safe to call
// handlers_.forEachHandler here

handlers_.forEachHandler([](const std::shared_ptr<Handler> handler) {
handler->context_.tid = std::this_thread::get_id();
});
Expand Down
4 changes: 2 additions & 2 deletions src/server/listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ namespace Pistache::Tcp
F_SETFDL_NOTHING // f_setfl_flags - don't change
));
#else
Fd event_fd = actual_fd;
Fd event_fd = actual_fd;
#endif

LOG_DEBUG_ACT_FD_AND_FDL_FLAGS(actual_fd);
Expand Down Expand Up @@ -760,7 +760,7 @@ namespace Pistache::Tcp
F_SETFDL_NOTHING // f_setfl_flags - don't change
));
#else
Fd client_fd = actual_cli_fd;
Fd client_fd = actual_cli_fd;
#endif

std::shared_ptr<Peer> peer;
Expand Down
19 changes: 12 additions & 7 deletions tests/http_server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,23 +986,28 @@ TEST(http_server_test, client_request_timeout_on_delay_in_request_line_send_rais
const std::string reqStr { "GET /ping HTTP/1.1\r\n" };
TcpClient client;
EXPECT_TRUE(client.connect(Pistache::Address("localhost", port))) << client.lastError();
bool send_failed = false;
for (size_t i = 0; i < reqStr.size(); ++i)
{
if (!client.send(reqStr.substr(i, 1)))
{
send_failed = true;
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}

EXPECT_EQ(client.lastErrno(), EPIPE) << "Errno: " << client.lastErrno();
if (send_failed)
{ // Usually, send does fail; but on macOS occasionally it does not fail
EXPECT_EQ(client.lastErrno(), EPIPE) << "Errno: " << client.lastErrno();

char recvBuf[1024] = {
0,
};
size_t bytes;
EXPECT_TRUE(client.receive(recvBuf, sizeof(recvBuf), &bytes, std::chrono::seconds(5))) << client.lastError();
EXPECT_EQ(0, strncmp(recvBuf, ExpectedResponseLine, strlen(ExpectedResponseLine)));
char recvBuf[1024] = {
0,
};
size_t bytes;
EXPECT_TRUE(client.receive(recvBuf, sizeof(recvBuf), &bytes, std::chrono::seconds(5))) << client.lastError();
EXPECT_EQ(0, strncmp(recvBuf, ExpectedResponseLine, strlen(ExpectedResponseLine)));
}

server.shutdown();

Expand Down
46 changes: 36 additions & 10 deletions tests/tcp_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,45 @@
#include <sys/socket.h>
#include <sys/types.h>

// In CLIENT_TRY, note that strerror is allowed to change errno in certain
// circumstances, so we must save errno in lastErrno_ BEFORE we call strerror
//
// Secondly, if errno has not been set at all then we set lastErrno_ =
// ECANCELED; the ECANCELED errno is not used in Pistache code as of Aug/2024.
static const char * strerror_errstr = "<no strerror>";
namespace Pistache
{

#define CLIENT_TRY(...) \
do \
{ \
auto ret = __VA_ARGS__; \
if (ret == -1) \
{ \
lastError_ = strerror(errno); \
lastErrno_ = errno; \
return false; \
} \
#define CLIENT_TRY(...) \
do \
{ \
auto ret = __VA_ARGS__; \
if (ret == -1) \
{ \
if (errno) \
{ \
lastErrno_ = errno; \
lastError_ = strerror(errno); \
if (errno != lastErrno_) \
std::cout << "strerror changed errno (was " << \
lastErrno_ << ", now " << errno << " )" << std::endl; \
\
if (lastError_.empty()) \
lastError_ = strerror_errstr; \
} \
else \
{ \
std::cout << "ret is -1, but errno not set" << std::endl; \
if (!lastErrno_) \
{ \
std::cout << "Setting lastErrno_ to ECANCELED" << \
std::endl; \
lastError_ = strerror_errstr; \
lastErrno_ = ECANCELED; \
} \
} \
return false; \
} \
} while (0)

class TcpClient
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.3.20240906
0.4.4.20240906

0 comments on commit 3068e11

Please sign in to comment.