mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-08 21:59:10 +02:00
Merge bitcoin/bitcoin#24357: refactor: make setsockopt() and SetSocketNoDelay() mockable/testable
a2c4a7acd1net: use Sock::SetSockOpt() instead of standalone SetSocketNoDelay() (Vasil Dimov)d65b6c3fb9net: use Sock::SetSockOpt() instead of setsockopt() (Vasil Dimov)184e56d668net: add new method Sock::SetSockOpt() that wraps setsockopt() (Vasil Dimov) Pull request description: _This is a piece of #21878, chopped off to ease review._ Add a `virtual` (thus mockable) method `Sock::SetSockOpt()` that wraps the system `setsockopt()`. Convert the standalone `SetSocketNoDelay()` function to a `virtual` (thus mockable) method `Sock::SetNoDelay()`. This will help avoid syscalls during testing and to mock them to return whatever is suitable for the tests. ACKs for top commit: laanwj: Code review ACKa2c4a7acd1jonatack: ACKa2c4a7acd1change since last review is folding `Sock::SetNoDelay()` into the callers Tree-SHA512: 3e2b016c1e4128317a28c17dc9b30472949e1ac3b071b2697c6d30cbcc830df1ee4392a4e23b2ea1ab4e3fb0f59ef450e2a4f3c1df3d8c803dd081652b6c7387
This commit is contained in:
@@ -105,6 +105,11 @@ int Sock::GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len)
|
||||
return getsockopt(m_socket, level, opt_name, static_cast<char*>(opt_val), opt_len);
|
||||
}
|
||||
|
||||
int Sock::SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt_len) const
|
||||
{
|
||||
return setsockopt(m_socket, level, opt_name, static_cast<const char*>(opt_val), opt_len);
|
||||
}
|
||||
|
||||
bool Sock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
|
||||
{
|
||||
#ifdef USE_POLL
|
||||
|
||||
@@ -115,6 +115,16 @@ public:
|
||||
void* opt_val,
|
||||
socklen_t* opt_len) const;
|
||||
|
||||
/**
|
||||
* setsockopt(2) wrapper. Equivalent to
|
||||
* `setsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this
|
||||
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
|
||||
*/
|
||||
[[nodiscard]] virtual int SetSockOpt(int level,
|
||||
int opt_name,
|
||||
const void* opt_val,
|
||||
socklen_t opt_len) const;
|
||||
|
||||
using Event = uint8_t;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user