diff --git a/llarp/CMakeLists.txt b/llarp/CMakeLists.txt index 9d4c9e30e..483bd2c47 100644 --- a/llarp/CMakeLists.txt +++ b/llarp/CMakeLists.txt @@ -18,6 +18,7 @@ set(LIB_UTIL_SRC util/common.cpp util/encode.cpp util/endian.cpp + util/decaying_hashset.cpp util/fs.cpp util/json.cpp util/logging/android_logger.cpp diff --git a/llarp/util/decaying_hashset.cpp b/llarp/util/decaying_hashset.cpp new file mode 100644 index 000000000..c9b69ab38 --- /dev/null +++ b/llarp/util/decaying_hashset.cpp @@ -0,0 +1 @@ +#include diff --git a/llarp/util/decaying_hashset.hpp b/llarp/util/decaying_hashset.hpp index 2555aa46b..558c94185 100644 --- a/llarp/util/decaying_hashset.hpp +++ b/llarp/util/decaying_hashset.hpp @@ -45,7 +45,7 @@ namespace llarp auto itr = m_Values.begin(); while(itr != m_Values.end()) { - if(itr->second >= now) + if(itr->second <= now) itr = m_Values.erase(itr); else ++itr; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3aea0d14e..7dea76fee 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -38,6 +38,7 @@ list(APPEND TEST_SRC util/test_llarp_util_aligned.cpp util/test_llarp_util_bencode.cpp util/test_llarp_util_bits.cpp + util/test_llarp_util_decaying_hashset.cpp util/test_llarp_util_encode.cpp util/test_llarp_util_printer.cpp util/test_llarp_utils_str.cpp diff --git a/test/util/test_llarp_util_decaying_hashset.cpp b/test/util/test_llarp_util_decaying_hashset.cpp new file mode 100644 index 000000000..e5710077e --- /dev/null +++ b/test/util/test_llarp_util_decaying_hashset.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +struct DecayingHashSetTest : public ::testing::Test +{ +}; + +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); + 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)); + hashset.Decay(now + timeout); + ASSERT_FALSE(hashset.Contains(zero)); + hashset.Decay(now + timeout + 1); + ASSERT_FALSE(hashset.Contains(zero)); +} + +TEST_F(DecayingHashSetTest, TestDecay) +{ + static constexpr llarp_time_t timeout = 5; + const llarp_time_t now = llarp::time_now_ms(); + 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)); + hashset.Decay(now + timeout); + ASSERT_FALSE(hashset.Contains(zero)); + hashset.Decay(now + timeout + 1); + ASSERT_FALSE(hashset.Contains(zero)); +}