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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#include "cache.h"
#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/time_provider/monotonic_provider.h>
#include <util/random/random.h>
#include <util/thread/pool.h>
using namespace NSQLComplete;
class TPausedClock: public NMonotonic::IMonotonicTimeProvider {
public:
NMonotonic::TMonotonic Now() override {
return Now_;
}
void Skip(TDuration duration) {
Now_ += duration;
}
private:
NMonotonic::TMonotonic Now_ = NMonotonic::CreateDefaultMonotonicTimeProvider()->Now();
};
struct TFat {
size_t Id = 0;
friend bool operator==(const TFat& lhs, const TFat& rhs) = default;
};
namespace NSQLComplete {
template <>
struct TByteSize<TFat> {
size_t operator()(const TFat&) const noexcept {
return 10'000;
}
};
} // namespace NSQLComplete
template <>
struct THash<TFat> {
size_t operator()(const TFat& x) const noexcept {
return x.Id;
}
};
struct TAction {
bool IsGet = false;
TString Key = "";
TString Value = "";
};
TVector<TAction> GenerateRandomActions(size_t size) {
constexpr double GetFrequency = 0.75;
constexpr ui32 MaxKey = 100;
constexpr ui32 MinValue = 1;
constexpr ui32 MaxValue = 10;
TVector<TAction> actions(size);
for (auto& action : actions) {
action.IsGet = RandomNumber<double>() < GetFrequency;
action.Key = ToString(RandomNumber(MaxKey));
action.Value = ToString(MinValue + RandomNumber(MaxValue - MinValue));
}
return actions;
}
TIntrusivePtr<TPausedClock> MakePausedClock() {
return new TPausedClock();
}
Y_UNIT_TEST_SUITE(LocalCacheTests) {
Y_UNIT_TEST(OnEmpty_WhenGet_ThenReturnedExpiredDefault) {
auto cache = MakeLocalCache<TString, TString>(
NMonotonic::CreateDefaultMonotonicTimeProvider(), {});
auto entry = cache->Get("1").GetValueSync();
UNIT_ASSERT_VALUES_EQUAL(entry.Value, Nothing());
UNIT_ASSERT_VALUES_EQUAL(entry.IsExpired, true);
}
Y_UNIT_TEST(OnEmpty_WhenUpdate_ThenReturnedNew) {
auto cache = MakeLocalCache<TString, TString>(
NMonotonic::CreateDefaultMonotonicTimeProvider(), {});
cache->Update("1", "1").GetValueSync();
auto entry = cache->Get("1").GetValueSync();
UNIT_ASSERT_VALUES_EQUAL(entry.Value, "1");
UNIT_ASSERT_VALUES_EQUAL(entry.IsExpired, false);
}
Y_UNIT_TEST(OnExistingKey_WhenUpdate_ThenReturnedNew) {
auto cache = MakeLocalCache<TString, TString>(
NMonotonic::CreateDefaultMonotonicTimeProvider(), {});
cache->Update("1", "1");
cache->Update("1", "2").GetValueSync();
auto entry = cache->Get("1").GetValueSync();
UNIT_ASSERT_VALUES_EQUAL(entry.Value, "2");
UNIT_ASSERT_VALUES_EQUAL(entry.IsExpired, false);
}
Y_UNIT_TEST(OnExistingKey_WhenExpires_ThenReturnedOld) {
auto clock = MakePausedClock();
auto cache = MakeLocalCache<TString, TString>(clock, {.TTL = TDuration::Minutes(2)});
cache->Update("1", "1");
clock->Skip(TDuration::Minutes(2) + TDuration::Seconds(1));
auto entry = cache->Get("1").GetValueSync();
UNIT_ASSERT_VALUES_EQUAL(entry.Value, "1");
UNIT_ASSERT_VALUES_EQUAL(entry.IsExpired, true);
}
Y_UNIT_TEST(OnExistingKey_WhenGetResultExtracted_ThenItIsCopied) {
auto cache = MakeLocalCache<TString, TString>(
NMonotonic::CreateDefaultMonotonicTimeProvider(), {});
cache->Update("1", TString(128, '1'));
cache->Get("1").ExtractValueSync();
UNIT_ASSERT_VALUES_EQUAL(cache->Get("1").ExtractValueSync().Value, TString(128, '1'));
UNIT_ASSERT_VALUES_EQUAL(cache->Get("1").ExtractValueSync().Value, TString(128, '1'));
}
Y_UNIT_TEST(OnFull_WhenFatAdded_ThenSomeKeyIsEvicted) {
const size_t Overhead = TByteSize<TFat>()({}) / 10;
auto cache = MakeLocalCache<int, TFat>(
NMonotonic::CreateDefaultMonotonicTimeProvider(),
{.ByteCapacity = 4 * TByteSize<TFat>()({}) + Overhead});
cache->Update(1, {});
cache->Update(2, {});
cache->Update(3, {});
cache->Update(4, {});
cache->Update(5, {});
size_t evicted = 0;
for (auto x : {1, 2, 3, 4, 5}) {
if (cache->Get(x).GetValueSync().IsExpired) {
evicted += 1;
}
}
UNIT_ASSERT_VALUES_EQUAL(evicted, 1);
}
Y_UNIT_TEST(OnFull_WhenFatAdded_ThenKeyAndOverheadAreAccounted) {
const size_t Overhead = TByteSize<TFat>()({}) / 10;
auto cache = MakeLocalCache<TFat, TFat>(
NMonotonic::CreateDefaultMonotonicTimeProvider(),
{.ByteCapacity = 3 * 4 * TByteSize<TFat>()({}) + Overhead});
cache->Update(TFat{1}, {});
cache->Update(TFat{2}, {});
cache->Update(TFat{3}, {});
cache->Update(TFat{4}, {});
cache->Update(TFat{5}, {});
size_t evicted = 0;
for (auto x : {TFat{1}, TFat{2}, TFat{3}, TFat{4}, TFat{5}}) {
if (cache->Get(x).GetValueSync().IsExpired) {
evicted += 1;
}
}
UNIT_ASSERT_VALUES_EQUAL(evicted, 1);
}
Y_UNIT_TEST(WhenRandomlyAccessed_ThenDoesNotDie) {
constexpr size_t Iterations = 1024 * 1024;
SetRandomSeed(1);
auto cache = MakeLocalCache<TString, TString>(
NMonotonic::CreateDefaultMonotonicTimeProvider(), {.ByteCapacity = 64, .TTL = TDuration::MilliSeconds(1)});
for (auto&& a : GenerateRandomActions(Iterations)) {
if (a.IsGet) {
Y_DO_NOT_OPTIMIZE_AWAY(cache->Get(a.Key));
} else {
Y_DO_NOT_OPTIMIZE_AWAY(cache->Update(a.Key, std::move(a.Value)));
}
}
}
Y_UNIT_TEST(WhenConcurrentlyAccessed_ThenDoesNotDie) {
constexpr size_t Threads = 8;
constexpr size_t Iterations = Threads * 16 * 1024;
SetRandomSeed(1);
auto cache = MakeLocalCache<TString, TString>(
NMonotonic::CreateDefaultMonotonicTimeProvider(), {.ByteCapacity = 64, .TTL = TDuration::MilliSeconds(1)});
auto pool = CreateThreadPool(Threads);
for (auto&& a : GenerateRandomActions(Iterations)) {
Y_ENSURE(pool->AddFunc([cache, a = std::move(a)] {
if (a.IsGet) {
Y_DO_NOT_OPTIMIZE_AWAY(cache->Get(a.Key));
} else {
Y_DO_NOT_OPTIMIZE_AWAY(cache->Update(a.Key, std::move(a.Value)));
}
}));
}
pool->Stop();
}
} // Y_UNIT_TEST_SUITE(LocalCacheTests)
|