diff options
author | Amiti Uttarwar <amiti@uttarwar.org> | 2019-11-05 11:06:53 +0100 |
---|---|---|
committer | Amiti Uttarwar <amiti@uttarwar.org> | 2019-11-05 11:06:53 +0100 |
commit | 4de630354fc6808b9b13b9e82da1a82f2f50f26a (patch) | |
tree | 4fc0563ddd0a33a0859ca71c8527ca1ba6a7c399 /src | |
parent | deb2327b435925c6a39ca654a79283b8eb6aeb86 (diff) | |
download | bitcoin-4de630354fc6808b9b13b9e82da1a82f2f50f26a.tar.xz |
[tools] add PoissonNextSend method that returns mockable time
Diffstat (limited to 'src')
-rw-r--r-- | src/net.h | 10 | ||||
-rw-r--r-- | src/test/net_tests.cpp | 14 |
2 files changed, 20 insertions, 4 deletions
@@ -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 diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index fed65afdbf..81ae8e2c3f 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -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() |