lokinet/test/util/test_llarp_util_decaying_ha...

40 lines
1.3 KiB
C++
Raw Normal View History

2023-01-09 18:47:41 +01:00
#include <llarp/util/decaying_hashset.hpp>
#include <llarp/router_id.hpp>
#include <catch2/catch.hpp>
2019-12-30 21:52:10 +01:00
TEST_CASE("DecayingHashSet test decay static time", "[decaying-hashset]")
2019-12-30 21:52:10 +01:00
{
static constexpr auto timeout = 5s;
static constexpr auto now = 1s;
2021-03-01 22:07:32 +01:00
llarp::util::DecayingHashSet<llarp::RouterID> hashset{timeout};
const llarp::RouterID 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));
2019-12-30 21:52:10 +01:00
hashset.Decay(now + timeout);
REQUIRE(not hashset.Contains(zero));
hashset.Decay(now + timeout + 1s);
REQUIRE(not hashset.Contains(zero));
2019-12-30 21:52:10 +01:00
}
2021-03-01 22:07:32 +01:00
TEST_CASE("DecayingHashSet test decay dynamic time", "[decaying-hashset]")
2019-12-30 21:52:10 +01:00
{
static constexpr llarp_time_t timeout = 5s;
2021-03-04 21:47:09 +01:00
const auto now = llarp::time_now_ms();
2021-03-01 22:07:32 +01:00
llarp::util::DecayingHashSet<llarp::RouterID> hashset{timeout};
const llarp::RouterID zero{};
REQUIRE(zero.IsZero());
REQUIRE(not hashset.Contains(zero));
2021-03-04 21:47:09 +01:00
REQUIRE(hashset.Insert(zero, now));
REQUIRE(hashset.Contains(zero));
2021-03-04 21:47:09 +01:00
hashset.Decay(now + 1s);
REQUIRE(hashset.Contains(zero));
2021-03-04 21:47:09 +01:00
hashset.Decay(now + timeout);
REQUIRE(not hashset.Contains(zero));
2021-03-04 21:47:09 +01:00
hashset.Decay(now + timeout + 1s);
REQUIRE(not hashset.Contains(zero));
2019-12-30 21:52:10 +01:00
}