Merge #18638: net: Use mockable time for ping/pong, add tests

fa3365430c5fb57d7c0b5f2bce9fbbe290be93c3 net: Use mockable time for ping/pong, add tests (MarcoFalke)
faab4aaf2fa1153c6d76efc8113fa01b06943ece util: Add count_microseconds helper (MarcoFalke)

Pull request description:

  Switch `CNode::m_ping_start` and `CNetMessage::m_time` to mockable time, so that tests can be added.

  Mockable time is also type-safe, since it uses `std::chrono`

ACKs for top commit:
  jonatack:
    Code review re-ACK fa33654 re-read code, verified rebase per `git range-diff 4b5c919 fa94d6f fa33654`, previous tested ACKs still valid
  troygiorshev:
    ACK fa3365430c5fb57d7c0b5f2bce9fbbe290be93c3

Tree-SHA512: 7d632bd6019ce7c882029e71b667a61517e783af82755a85dd979ef09380934e172dec8b8f91d57b200a30a6e096aeaf01f19fee7f3aed0e0e871c72eb44d70e
This commit is contained in:
MarcoFalke 2020-07-10 16:06:11 +02:00 committed by Konstantin Akimov
parent 6e5d3f1d1f
commit 264e7f9e62
No known key found for this signature in database
GPG Key ID: 2176C4A5D01EA524
2 changed files with 3 additions and 8 deletions

View File

@ -785,8 +785,7 @@ void CNode::CopyStats(CNodeStats& stats)
bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
{
complete = false;
// TODO: use mocktime here after bitcoin#19499 is backported
const auto time = std::chrono::microseconds(GetTimeMicros());
const auto time = GetTime<std::chrono::microseconds>();
LOCK(cs_vRecv);
m_last_recv = std::chrono::duration_cast<std::chrono::seconds>(time);
nRecvBytes += msg_bytes.size();

View File

@ -92,9 +92,7 @@ class PingPongTest(BitcoinTestFramework):
self.mock_forward(ping_delay)
no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)
no_pong_node.send_and_ping(msg_pong(no_pong_node.last_message.pop('ping').nonce))
# TODO this check doesn't work due to partial 18638
# re-enable it after #19499 is done
# self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)
self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)
self.log.info('Check that minping is decreased after a fast roundtrip')
# mock time PING_INTERVAL ahead to trigger node into sending a ping
@ -104,9 +102,7 @@ class PingPongTest(BitcoinTestFramework):
self.mock_forward(ping_delay)
no_pong_node.wait_until(lambda: 'ping' in no_pong_node.last_message)
no_pong_node.send_and_ping(msg_pong(no_pong_node.last_message.pop('ping').nonce))
# TODO this check doesn't work due to partial 18638
# re-enable it after #19499 is done
# self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)
self.check_peer_info(pingtime=ping_delay, minping=ping_delay, pingwait=None)
self.log.info('Check that peer is disconnected after ping timeout')
assert 'ping' not in no_pong_node.last_message