make decaying hashset use llarp::Time_t and move unit tests to use catch2

pull/1128/head
Jeff Becker 4 years ago
parent 39cdc9e6dd
commit f4520ac920
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -199,6 +199,10 @@ set(LIB_SRC
service/tag_lookup_job.cpp service/tag_lookup_job.cpp
service/tag.cpp service/tag.cpp
) )
if(TRACY_ROOT)
set(LIB_SRC ${LIB_SRC} ${TRACY_ROOT}/TracyClient.cpp)
endif()
if(TESTNET) if(TESTNET)
set(LIB_SRC ${LIB_SRC} testnet.c) set(LIB_SRC ${LIB_SRC} testnet.c)
endif() endif()

@ -371,7 +371,7 @@ namespace llarp
// clear gossip list // clear gossip list
m_GossipList.clear(); m_GossipList.clear();
// decay gossip filter // decay gossip filter
m_GossipReplayFilter.Decay(now); m_GossipReplayFilter.Decay();
if(_services) if(_services)
{ {

@ -378,8 +378,8 @@ namespace llarp
m_RXRate = 0; m_RXRate = 0;
m_TXRate = 0; m_TXRate = 0;
m_UpstreamReplayFilter.Decay(now); m_UpstreamReplayFilter.Decay(Time_t(now));
m_DownstreamReplayFilter.Decay(now); m_DownstreamReplayFilter.Decay(Time_t(now));
if(_status == ePathBuilding) if(_status == ePathBuilding)
{ {

@ -9,7 +9,7 @@ namespace llarp
{ {
namespace path namespace path
{ {
static constexpr llarp_time_t DefaultPathBuildLimit = 500; static constexpr auto DefaultPathBuildLimit = 500ms;
PathContext::PathContext(AbstractRouter* router) PathContext::PathContext(AbstractRouter* router)
: m_Router(router) : m_Router(router)
@ -297,7 +297,7 @@ namespace llarp
PathContext::ExpirePaths(llarp_time_t now) PathContext::ExpirePaths(llarp_time_t now)
{ {
// decay limits // decay limits
m_PathLimits.Decay(now); m_PathLimits.Decay(Time_t(now));
{ {
SyncTransitMap_t::Lock_t lock(m_TransitPaths.first); SyncTransitMap_t::Lock_t lock(m_TransitPaths.first);

@ -41,8 +41,7 @@ namespace llarp
void void
RCGossiper::Decay(Time_t now) RCGossiper::Decay(Time_t now)
{ {
LogDebug("decay filter at ", now.count()); m_Filter.Decay(now);
m_Filter.Decay(now.count());
} }
bool bool

@ -32,9 +32,7 @@ namespace llarp
bool bool
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 30000) const ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 30000) const
{ {
if(dlt) return IsExpired(now + dlt);
return now >= (expiresAt - dlt);
return IsExpired(now);
} }
std::ostream& std::ostream&

@ -11,15 +11,10 @@ namespace llarp
template < typename Val_t, typename Hash_t = typename Val_t::Hash > template < typename Val_t, typename Hash_t = typename Val_t::Hash >
struct DecayingHashSet struct DecayingHashSet
{ {
DecayingHashSet(Time_t cacheInterval) DecayingHashSet(Time_t cacheInterval = 5s)
: DecayingHashSet(cacheInterval.count())
{
}
explicit DecayingHashSet(llarp_time_t cacheInterval = 5000)
: m_CacheInterval(cacheInterval) : m_CacheInterval(cacheInterval)
{ {
} }
/// determine if we have v contained in our decaying hashset /// determine if we have v contained in our decaying hashset
bool bool
Contains(const Val_t& v) const Contains(const Val_t& v) const
@ -30,19 +25,20 @@ namespace llarp
/// return true if inserted /// return true if inserted
/// return false if not inserted /// return false if not inserted
bool bool
Insert(const Val_t& v, llarp_time_t now = 0) Insert(const Val_t& v, Time_t now = 0s)
{ {
if(now == 0) if(now == 0s)
now = llarp::time_now_ms(); now = llarp::time_now();
return m_Values.emplace(v, now).second; return m_Values.emplace(v, now).second;
} }
/// decay hashset entries /// decay hashset entries
void void
Decay(llarp_time_t now = 0) Decay(Time_t now = 0s)
{ {
if(now == 0) if(now == 0s)
now = llarp::time_now_ms(); now = llarp::time_now();
auto itr = m_Values.begin(); auto itr = m_Values.begin();
while(itr != m_Values.end()) while(itr != m_Values.end())
{ {
@ -53,21 +49,21 @@ namespace llarp
} }
} }
llarp_time_t Time_t
DecayInterval() const DecayInterval() const
{ {
return m_CacheInterval; return m_CacheInterval;
} }
void void
DecayInterval(llarp_time_t interval) DecayInterval(Time_t interval)
{ {
m_CacheInterval = interval; m_CacheInterval = interval;
} }
private: private:
llarp_time_t m_CacheInterval; Time_t m_CacheInterval;
std::unordered_map< Val_t, llarp_time_t, Hash_t > m_Values; std::unordered_map< Val_t, Time_t, Hash_t > m_Values;
}; };
} // namespace util } // namespace util
} // namespace llarp } // namespace llarp

@ -35,7 +35,6 @@ list(APPEND GTEST_SRC
util/meta/test_llarp_util_traits.cpp util/meta/test_llarp_util_traits.cpp
util/test_llarp_util_aligned.cpp util/test_llarp_util_aligned.cpp
util/test_llarp_util_bencode.cpp util/test_llarp_util_bencode.cpp
util/test_llarp_util_decaying_hashset.cpp
util/test_llarp_util_encode.cpp util/test_llarp_util_encode.cpp
util/test_llarp_util_log_level.cpp util/test_llarp_util_log_level.cpp
util/thread/test_llarp_util_queue_manager.cpp util/thread/test_llarp_util_queue_manager.cpp
@ -73,6 +72,7 @@ add_executable(${CATCH_EXE}
util/test_llarp_util_bits.cpp util/test_llarp_util_bits.cpp
util/test_llarp_util_printer.cpp util/test_llarp_util_printer.cpp
util/test_llarp_util_str.cpp util/test_llarp_util_str.cpp
util/test_llarp_util_decaying_hashset.cpp
check_main.cpp) check_main.cpp)
target_link_libraries(${CATCH_EXE} PUBLIC ${STATIC_LIB} Catch2::Catch2) target_link_libraries(${CATCH_EXE} PUBLIC ${STATIC_LIB} Catch2::Catch2)

@ -1,43 +1,39 @@
#include <util/decaying_hashset.hpp> #include <util/decaying_hashset.hpp>
#include <router_id.hpp> #include <router_id.hpp>
#include <gtest/gtest.h> #include <catch2/catch.hpp>
struct DecayingHashSetTest : public ::testing::Test TEST_CASE("DecayingHashSet test decay static time", "[decaying-hashset]")
{ {
}; static constexpr auto timeout = 5s;
static constexpr auto now = 1s;
TEST_F(DecayingHashSetTest, TestDecayDeterministc)
{
static constexpr llarp_time_t timeout = 5;
static constexpr llarp_time_t now = 1;
llarp::util::DecayingHashSet< llarp::RouterID > hashset(timeout); llarp::util::DecayingHashSet< llarp::RouterID > hashset(timeout);
const llarp::RouterID zero; const llarp::RouterID zero;
ASSERT_TRUE(zero.IsZero()); REQUIRE(zero.IsZero());
ASSERT_FALSE(hashset.Contains(zero)); REQUIRE(not hashset.Contains(zero));
ASSERT_TRUE(hashset.Insert(zero, now)); REQUIRE(hashset.Insert(zero, now));
ASSERT_TRUE(hashset.Contains(zero)); REQUIRE(hashset.Contains(zero));
hashset.Decay(now + 1); hashset.Decay(now + 1s);
ASSERT_TRUE(hashset.Contains(zero)); REQUIRE(hashset.Contains(zero));
hashset.Decay(now + timeout); hashset.Decay(now + timeout);
ASSERT_FALSE(hashset.Contains(zero)); REQUIRE(not hashset.Contains(zero));
hashset.Decay(now + timeout + 1); hashset.Decay(now + timeout + 1s);
ASSERT_FALSE(hashset.Contains(zero)); REQUIRE(not hashset.Contains(zero));
} }
TEST_F(DecayingHashSetTest, TestDecay) TEST_CASE("DecayingHashSet tset decay dynamic time", "[decaying-hashset]")
{ {
static constexpr llarp_time_t timeout = 5; static constexpr auto timeout = 5s;
const llarp_time_t now = llarp::time_now_ms(); const llarp::Time_t now = llarp::time_now();
llarp::util::DecayingHashSet< llarp::RouterID > hashset(timeout); llarp::util::DecayingHashSet< llarp::RouterID > hashset(timeout);
const llarp::RouterID zero; const llarp::RouterID zero;
ASSERT_TRUE(zero.IsZero()); REQUIRE(zero.IsZero());
ASSERT_FALSE(hashset.Contains(zero)); REQUIRE(not hashset.Contains(zero));
ASSERT_TRUE(hashset.Insert(zero)); REQUIRE(hashset.Insert(zero));
ASSERT_TRUE(hashset.Contains(zero)); REQUIRE(hashset.Contains(zero));
hashset.Decay(now + 1); hashset.Decay(now + 1s);
ASSERT_TRUE(hashset.Contains(zero)); REQUIRE(hashset.Contains(zero));
hashset.Decay(now + timeout); hashset.Decay(now + timeout);
ASSERT_FALSE(hashset.Contains(zero)); REQUIRE(not hashset.Contains(zero));
hashset.Decay(now + timeout + 1); hashset.Decay(now + timeout + 1s);
ASSERT_FALSE(hashset.Contains(zero)); REQUIRE(not hashset.Contains(zero));
} }

Loading…
Cancel
Save