aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/containers/unittests/expiring_set_ut.cpp
blob: e9f2743a807c87b28b4ca8d0e62e513d737e3ef7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <library/cpp/yt/containers/expiring_set.h>

#include <library/cpp/testing/gtest/gtest.h>

namespace NYT {
namespace {

////////////////////////////////////////////////////////////////////////////////

TInstant operator""_ts(unsigned long long seconds)
{
    return TInstant::Zero() + TDuration::Seconds(seconds);
}

////////////////////////////////////////////////////////////////////////////////

TEST(TExpiringSetTest, Empty)
{
    TExpiringSet<int> set;
    EXPECT_EQ(set.GetSize(), 0);
}

TEST(TExpiringSetTest, ExpireSingle)
{
    TExpiringSet<int> set;
    set.SetTTl(TDuration::Seconds(2));

    set.Insert(0_ts, 1);
    EXPECT_EQ(set.GetSize(), 1);

    set.Expire(1_ts);
    EXPECT_EQ(set.GetSize(), 1);

    set.Expire(2_ts);
    EXPECT_EQ(set.GetSize(), 0);
}

TEST(TExpiringSetTest, ExpireBatch)
{
    TExpiringSet<int> set;
    set.SetTTl(TDuration::Seconds(2));

    set.InsertMany(0_ts, std::vector<int>{1, 2, 3});
    EXPECT_EQ(set.GetSize(), 3);

    set.Expire(1_ts);
    EXPECT_EQ(set.GetSize(), 3);

    set.Expire(2_ts);
    EXPECT_EQ(set.GetSize(), 0);
}

TEST(TExpiringSetTest, Reinsert)
{
    TExpiringSet<int> set;
    set.SetTTl(TDuration::Seconds(2));

    set.Insert(0_ts, 1);
    EXPECT_EQ(set.GetSize(), 1);

    set.Insert(1_ts, 1);
    EXPECT_EQ(set.GetSize(), 1);

    set.Expire(2_ts);
    EXPECT_EQ(set.GetSize(), 1);

    set.Expire(3_ts);
    EXPECT_EQ(set.GetSize(), 0);
}

TEST(TExpiringSetTest, Contains)
{
    TExpiringSet<int> set;
    set.SetTTl(TDuration::Seconds(1));

    EXPECT_FALSE(set.Contains(1));

    set.Insert(0_ts, 1);
    EXPECT_TRUE(set.Contains(1));

    set.Expire(1_ts);
    EXPECT_FALSE(set.Contains(1));
}

TEST(TExpiringSetTest, Clear)
{
    TExpiringSet<int> set;
    set.SetTTl(TDuration::Seconds(1));

    set.Insert(0_ts, 1);
    EXPECT_EQ(set.GetSize(), 1);
    EXPECT_TRUE(set.Contains(1));

    set.Clear();
    EXPECT_EQ(set.GetSize(), 0);
    EXPECT_FALSE(set.Contains(1));
}

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT