mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
[tools] add PoissonNextSend method that returns mockable time
This commit is contained in:
parent
deb2327b43
commit
4de630354f
2 changed files with 20 additions and 4 deletions
10
src/net.h
10
src/net.h
|
@ -943,11 +943,13 @@ public:
|
|||
void MaybeSetAddrName(const std::string& addrNameIn);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
|
||||
int64_t PoissonNextSend(int64_t now, int average_interval_seconds);
|
||||
|
||||
/** Wrapper to return mockable type */
|
||||
inline std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval)
|
||||
{
|
||||
return std::chrono::microseconds{PoissonNextSend(now.count(), average_interval.count())};
|
||||
}
|
||||
|
||||
#endif // BITCOIN_NET_H
|
||||
|
|
|
@ -303,5 +303,19 @@ BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
|
|||
BOOST_CHECK_EQUAL(IsLocal(addr), false);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(PoissonNextSend)
|
||||
{
|
||||
g_mock_deterministic_tests = true;
|
||||
|
||||
int64_t now = 5000;
|
||||
int average_interval_seconds = 600;
|
||||
|
||||
auto poisson = ::PoissonNextSend(now, average_interval_seconds);
|
||||
std::chrono::microseconds poisson_chrono = ::PoissonNextSend(std::chrono::microseconds{now}, std::chrono::seconds{average_interval_seconds});
|
||||
|
||||
BOOST_CHECK_EQUAL(poisson, poisson_chrono.count());
|
||||
|
||||
g_mock_deterministic_tests = false;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
|
Loading…
Add table
Reference in a new issue