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.cpp
)
if(TRACY_ROOT)
set(LIB_SRC ${LIB_SRC} ${TRACY_ROOT}/TracyClient.cpp)
endif()
if(TESTNET)
set(LIB_SRC ${LIB_SRC} testnet.c)
endif()

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

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

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

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

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

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

@ -35,7 +35,6 @@ list(APPEND GTEST_SRC
util/meta/test_llarp_util_traits.cpp
util/test_llarp_util_aligned.cpp
util/test_llarp_util_bencode.cpp
util/test_llarp_util_decaying_hashset.cpp
util/test_llarp_util_encode.cpp
util/test_llarp_util_log_level.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_printer.cpp
util/test_llarp_util_str.cpp
util/test_llarp_util_decaying_hashset.cpp
check_main.cpp)
target_link_libraries(${CATCH_EXE} PUBLIC ${STATIC_LIB} Catch2::Catch2)

@ -1,43 +1,39 @@
#include <util/decaying_hashset.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]")
{
};
TEST_F(DecayingHashSetTest, TestDecayDeterministc)
{
static constexpr llarp_time_t timeout = 5;
static constexpr llarp_time_t now = 1;
static constexpr auto timeout = 5s;
static constexpr auto now = 1s;
llarp::util::DecayingHashSet< llarp::RouterID > hashset(timeout);
const llarp::RouterID zero;
ASSERT_TRUE(zero.IsZero());
ASSERT_FALSE(hashset.Contains(zero));
ASSERT_TRUE(hashset.Insert(zero, now));
ASSERT_TRUE(hashset.Contains(zero));
hashset.Decay(now + 1);
ASSERT_TRUE(hashset.Contains(zero));
REQUIRE(zero.IsZero());
REQUIRE(not hashset.Contains(zero));
REQUIRE(hashset.Insert(zero, now));
REQUIRE(hashset.Contains(zero));
hashset.Decay(now + 1s);
REQUIRE(hashset.Contains(zero));
hashset.Decay(now + timeout);
ASSERT_FALSE(hashset.Contains(zero));
hashset.Decay(now + timeout + 1);
ASSERT_FALSE(hashset.Contains(zero));
REQUIRE(not hashset.Contains(zero));
hashset.Decay(now + timeout + 1s);
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;
const llarp_time_t now = llarp::time_now_ms();
static constexpr auto timeout = 5s;
const llarp::Time_t now = llarp::time_now();
llarp::util::DecayingHashSet< llarp::RouterID > hashset(timeout);
const llarp::RouterID zero;
ASSERT_TRUE(zero.IsZero());
ASSERT_FALSE(hashset.Contains(zero));
ASSERT_TRUE(hashset.Insert(zero));
ASSERT_TRUE(hashset.Contains(zero));
hashset.Decay(now + 1);
ASSERT_TRUE(hashset.Contains(zero));
REQUIRE(zero.IsZero());
REQUIRE(not hashset.Contains(zero));
REQUIRE(hashset.Insert(zero));
REQUIRE(hashset.Contains(zero));
hashset.Decay(now + 1s);
REQUIRE(hashset.Contains(zero));
hashset.Decay(now + timeout);
ASSERT_FALSE(hashset.Contains(zero));
hashset.Decay(now + timeout + 1);
ASSERT_FALSE(hashset.Contains(zero));
REQUIRE(not hashset.Contains(zero));
hashset.Decay(now + timeout + 1s);
REQUIRE(not hashset.Contains(zero));
}

Loading…
Cancel
Save