Merge bitcoin/bitcoin#24357: refactor: make setsockopt() and SetSocketNoDelay() mockable/testable

a2c4a7acd1 net: use Sock::SetSockOpt() instead of standalone SetSocketNoDelay() (Vasil Dimov)
d65b6c3fb9 net: use Sock::SetSockOpt() instead of setsockopt() (Vasil Dimov)
184e56d668 net: 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 ACK a2c4a7acd1
  jonatack:
    ACK a2c4a7acd1 change since last review is folding `Sock::SetNoDelay()` into the callers

Tree-SHA512: 3e2b016c1e4128317a28c17dc9b30472949e1ac3b071b2697c6d30cbcc830df1ee4392a4e23b2ea1ab4e3fb0f59ef450e2a4f3c1df3d8c803dd081652b6c7387
This commit is contained in:
laanwj
2022-04-19 16:40:18 +02:00
8 changed files with 62 additions and 20 deletions

View File

@@ -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

View File

@@ -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;
/**