-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlazy_load_system_test.cpp
More file actions
352 lines (274 loc) · 12 KB
/
lazy_load_system_test.cpp
File metadata and controls
352 lines (274 loc) · 12 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "data_systems/lazy_load/lazy_load_system.hpp"
#include <set>
#include <unordered_map>
#include "data_components/serialization_adapters/json_deserializer.hpp"
#include "spy_logger.hpp"
using namespace launchdarkly;
using namespace launchdarkly::server_side;
using namespace launchdarkly::server_side::config;
using ::testing::InSequence;
using ::testing::NiceMock;
using ::testing::Return;
class MockDataReader : public integrations::ISerializedDataReader {
public:
MOCK_METHOD(GetResult,
Get,
(integrations::ISerializedItemKind const& kind,
std::string const& itemKey),
(override, const));
MOCK_METHOD(AllResult,
All,
(integrations::ISerializedItemKind const& kind),
(override, const));
MOCK_METHOD(std::string const&, Identity, (), (override, const));
MOCK_METHOD(bool, Initialized, (), (override, const));
explicit MockDataReader(std::string name) : name_(std::move(name)) {
ON_CALL(*this, Identity).WillByDefault(::testing::ReturnRef(name_));
}
private:
std::string const name_;
};
class LazyLoadTest : public ::testing::Test {
public:
std::string mock_reader_name = "fake reader";
std::shared_ptr<NiceMock<MockDataReader>> mock_reader;
std::shared_ptr<logging::SpyLoggerBackend> spy_logger_backend;
Logger const logger;
data_components::DataSourceStatusManager status_manager;
LazyLoadTest()
: mock_reader(
std::make_shared<NiceMock<MockDataReader>>(mock_reader_name)),
spy_logger_backend(std::make_shared<logging::SpyLoggerBackend>()),
logger(spy_logger_backend) {}
};
TEST_F(LazyLoadTest, IdentityWrapsReaderIdentity) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::milliseconds(100), mock_reader};
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
ASSERT_EQ(lazy_load.Identity(),
"lazy load via " + mock_reader_name + " (JSON)");
}
TEST_F(LazyLoadTest, ReaderIsNotQueriedRepeatedlyIfFlagIsCached) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(integrations::SerializedItemDescriptor{
1, false, "{\"key\":\"foo\",\"version\":1}"}));
for (std::size_t i = 0; i < 20; i++) {
ASSERT_TRUE(lazy_load.GetFlag("foo"));
}
}
TEST_F(LazyLoadTest, ReaderIsNotQueriedRepeatedlyIfSegmentIsCached) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(integrations::SerializedItemDescriptor{
1, false, "{\"key\":\"foo\",\"version\":1}"}));
for (std::size_t i = 0; i < 20; i++) {
ASSERT_TRUE(lazy_load.GetSegment("foo"));
}
}
TEST_F(LazyLoadTest, ReaderIsNotQueriedRepeatedlyIfFlagCannotBeFetched) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(tl::make_unexpected(
integrations::ISerializedDataReader::Error{"oops"})));
for (std::size_t i = 0; i < 20; i++) {
ASSERT_FALSE(lazy_load.GetFlag("foo"));
};
ASSERT_TRUE(spy_logger_backend->Count(21)); // 20 debug logs + 1 error log
ASSERT_TRUE(spy_logger_backend->Contains(1, LogLevel::kError, "oops"));
}
TEST_F(LazyLoadTest, ReaderIsNotQueriedRepeatedlyIfSegmentCannotBeFetched) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(tl::make_unexpected(
integrations::ISerializedDataReader::Error{"oops"})));
for (std::size_t i = 0; i < 20; i++) {
ASSERT_FALSE(lazy_load.GetSegment("foo"));
};
ASSERT_TRUE(spy_logger_backend->Count(21));
ASSERT_TRUE(spy_logger_backend->Contains(1, LogLevel::kError, "oops"));
}
TEST_F(LazyLoadTest, RefreshesFlagIfStale) {
using TimePoint = data_systems::LazyLoad::ClockType::time_point;
constexpr auto refresh_ttl = std::chrono::seconds(10);
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled, refresh_ttl,
mock_reader};
TimePoint now{std::chrono::seconds(0)};
data_systems::LazyLoad const lazy_load(logger, config, status_manager,
[&]() { return now; });
{
InSequence s;
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(integrations::SerializedItemDescriptor{
1, false, "{\"key\":\"foo\",\"version\":1}"}));
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(integrations::SerializedItemDescriptor{
2, false, "{\"key\":\"foo\",\"version\":2}"}));
}
for (std::size_t i = 0; i < 10; i++) {
auto flag = lazy_load.GetFlag("foo");
ASSERT_TRUE(flag);
ASSERT_EQ(flag->version, 1);
}
now = TimePoint{refresh_ttl + std::chrono::seconds(1)};
for (std::size_t i = 0; i < 10; i++) {
auto flag = lazy_load.GetFlag("foo");
ASSERT_TRUE(flag);
ASSERT_EQ(flag->version, 2);
}
}
TEST_F(LazyLoadTest, RefreshesSegmentIfStale) {
using TimePoint = data_systems::LazyLoad::ClockType::time_point;
constexpr auto refresh_ttl = std::chrono::seconds(10);
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled, refresh_ttl,
mock_reader};
TimePoint now{std::chrono::seconds(0)};
data_systems::LazyLoad const lazy_load(logger, config, status_manager,
[&]() { return now; });
{
InSequence s;
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(integrations::SerializedItemDescriptor{
1, false, "{\"key\":\"foo\",\"version\":1}"}));
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(integrations::SerializedItemDescriptor{
2, false, "{\"key\":\"foo\",\"version\":2}"}));
}
for (std::size_t i = 0; i < 10; i++) {
auto segment = lazy_load.GetSegment("foo");
ASSERT_TRUE(segment);
ASSERT_EQ(segment->version, 1);
}
now = TimePoint{refresh_ttl + std::chrono::seconds(1)};
for (std::size_t i = 0; i < 10; i++) {
auto segment = lazy_load.GetSegment("foo");
ASSERT_TRUE(segment);
ASSERT_EQ(segment->version, 2);
}
}
TEST_F(LazyLoadTest, AllFlagsRefreshesIndividualFlag) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
// We want to demonstrate that an individual flag will be
// refreshed not just when we grab that single flag, but also if
// we call AllFlags.
{
InSequence s;
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(integrations::SerializedItemDescriptor{
1, false, "{\"key\":\"foo\",\"version\":1}"}));
EXPECT_CALL(*mock_reader, All(testing::_))
.WillOnce(Return(
std::unordered_map<std::string,
integrations::SerializedItemDescriptor>{
{"foo", {2, false, "{\"key\":\"foo\",\"version\":2}"}}}));
}
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
auto const flag1 = lazy_load.GetFlag("foo");
ASSERT_TRUE(flag1);
ASSERT_EQ(flag1->version, 1);
auto const all_flags = lazy_load.AllFlags();
ASSERT_EQ(all_flags.size(), 1);
ASSERT_EQ(all_flags.at("foo")->version, 2);
auto const flag2 = lazy_load.GetFlag("foo");
ASSERT_TRUE(flag2);
ASSERT_EQ(flag2->version, 2);
}
TEST_F(LazyLoadTest, AllSegmentsRefreshesIndividualSegment) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
// We want to demonstrate that an individual segment will be
// refreshed not just when we grab that single segment, but also if
// we call AllSegments.
{
InSequence s;
EXPECT_CALL(*mock_reader, Get(testing::_, "foo"))
.WillOnce(Return(integrations::SerializedItemDescriptor{
1, false, "{\"key\":\"foo\",\"version\":1}"}));
EXPECT_CALL(*mock_reader, All(testing::_))
.WillOnce(Return(
std::unordered_map<std::string,
integrations::SerializedItemDescriptor>{
{"foo", {2, false, "{\"key\":\"foo\",\"version\":2}"}}}));
}
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
auto const segment1 = lazy_load.GetSegment("foo");
ASSERT_TRUE(segment1);
ASSERT_EQ(segment1->version, 1);
auto const all_segments = lazy_load.AllSegments();
ASSERT_EQ(all_segments.size(), 1);
ASSERT_EQ(all_segments.at("foo")->version, 2);
auto const segment2 = lazy_load.GetSegment("foo");
ASSERT_TRUE(segment2);
ASSERT_EQ(segment2->version, 2);
}
TEST_F(LazyLoadTest, InitializeNotQueriedRepeatedly) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
EXPECT_CALL(*mock_reader, Initialized).WillOnce(Return(false));
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
for (std::size_t i = 0; i < 10; i++) {
ASSERT_FALSE(lazy_load.Initialized());
}
}
TEST_F(LazyLoadTest, InitializeCalledOnceThenNeverAgainAfterReturningTrue) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
EXPECT_CALL(*mock_reader, Initialized).WillOnce(Return(true));
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
for (std::size_t i = 0; i < 10; i++) {
ASSERT_TRUE(lazy_load.Initialized());
}
}
TEST_F(LazyLoadTest, InitializeCalledAgainAfterTTL) {
using TimePoint = data_systems::LazyLoad::ClockType::time_point;
constexpr auto refresh_ttl = std::chrono::seconds(10);
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled, refresh_ttl,
mock_reader};
{
InSequence s;
EXPECT_CALL(*mock_reader, Initialized).WillOnce(Return(false));
EXPECT_CALL(*mock_reader, Initialized).WillOnce(Return(true));
}
TimePoint now{std::chrono::seconds(0)};
data_systems::LazyLoad const lazy_load(logger, config, status_manager,
[&]() { return now; });
for (std::size_t i = 0; i < 10; i++) {
ASSERT_FALSE(lazy_load.Initialized());
now += std::chrono::seconds(1);
}
for (std::size_t i = 0; i < 10; i++) {
ASSERT_TRUE(lazy_load.Initialized());
}
}
TEST_F(LazyLoadTest, CanEvaluateWhenNotInitialized) {
built::LazyLoadConfig const config{
built::LazyLoadConfig::EvictionPolicy::Disabled,
std::chrono::seconds(10), mock_reader};
data_systems::LazyLoad const lazy_load(logger, config, status_manager);
// LazyLoad can always serve evaluations on demand, even if not
// initialized (i.e. $inited key not found in store).
ASSERT_TRUE(lazy_load.CanEvaluateWhenNotInitialized());
}