Repository navigation
Expand file tree
/
Copy pathtest_package_query_cache.cpp
More file actions
321 lines (262 loc) · 13.5 KB
/
Copy pathtest_package_query_cache.cpp
File metadata and controls
321 lines (262 loc) · 13.5 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
// -----------------------------------------------------------------------------
// Package query cache tests
// Covers cache keys, generation checks, and cache invalidation.
// -----------------------------------------------------------------------------
#include <catch2/catch_test_macros.hpp>
#include "dnf_backend/base_manager.hpp"
#include "test_utils.hpp"
#include "ui/package_query/package_query_cache.hpp"
#include <string>
#include <vector>
namespace {
// -----------------------------------------------------------------------------
// Build one small package row for cache tests.
// -----------------------------------------------------------------------------
PackageRow
make_cache_row(const std::string &nevra, const std::string &name)
{
PackageRow row;
row.nevra = nevra;
row.name = name;
row.arch = "x86_64";
return row;
}
} // namespace
// -----------------------------------------------------------------------------
// Verify that cache keys include the search mode flags that affect results.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache key includes search options")
{
reset_backend_globals();
REQUIRE(package_query_cache_key_for("bash", backend_search_options(false, false)) == "name:contains:latest:bash");
REQUIRE(package_query_cache_key_for("bash", backend_search_options(false, true)) == "name:exact:latest:bash");
REQUIRE(package_query_cache_key_for("bash", backend_search_options(true, false)) == "desc:contains:latest:bash");
REQUIRE(package_query_cache_key_for("bash", backend_search_options(true, true)) == "desc:exact:latest:bash");
REQUIRE(package_query_cache_key_for("bash", backend_search_options(false, false, false)) == "name:contains:all:bash");
auto provider_options = backend_search_options(false, false);
provider_options.find_provider = true;
REQUIRE(package_query_cache_key_for("bash", provider_options) == "provides:contains:latest:bash");
}
// -----------------------------------------------------------------------------
// Verify that only compact latest-only searches use the in-memory search cache.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache is used only for latest-only searches")
{
REQUIRE(package_query_cache_should_use(backend_search_options(false, false)));
REQUIRE_FALSE(package_query_cache_should_use(backend_search_options(false, false, false)));
}
// -----------------------------------------------------------------------------
// Verify that cached rows are returned only for the same key and generation.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache returns rows for matching key and generation")
{
package_query_cache_clear();
const uint64_t cache_epoch = package_query_cache_current_epoch();
const std::string key = "name:contains:demo";
std::vector<PackageRow> stored = {
make_cache_row("demo-1-1.x86_64", "demo"),
make_cache_row("demo-libs-1-1.x86_64", "demo-libs"),
};
std::vector<PackageRow> loaded;
package_query_cache_store(key, 7, cache_epoch, stored);
REQUIRE(package_query_cache_lookup(key, 7, cache_epoch, loaded));
REQUIRE(loaded.size() == 2);
REQUIRE(loaded[0].nevra == "demo-1-1.x86_64");
REQUIRE(loaded[1].nevra == "demo-libs-1-1.x86_64");
}
// -----------------------------------------------------------------------------
// Verify that stale generation lookups are rejected and removed from cache.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache rejects and removes stale generations")
{
package_query_cache_clear();
const uint64_t cache_epoch = package_query_cache_current_epoch();
const std::string key = "name:contains:demo";
std::vector<PackageRow> stored = {
make_cache_row("demo-1-1.x86_64", "demo"),
};
std::vector<PackageRow> loaded;
package_query_cache_store(key, 7, cache_epoch, stored);
REQUIRE_FALSE(package_query_cache_lookup(key, 8, cache_epoch, loaded));
REQUIRE_FALSE(package_query_cache_lookup(key, 7, cache_epoch, loaded));
}
// -----------------------------------------------------------------------------
// Verify that clearing the query cache removes stored package rows.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache clear removes stored rows")
{
package_query_cache_clear();
const uint64_t cache_epoch = package_query_cache_current_epoch();
const std::string key = "name:contains:demo";
std::vector<PackageRow> stored = {
make_cache_row("demo-1-1.x86_64", "demo"),
};
std::vector<PackageRow> loaded;
package_query_cache_store(key, 7, cache_epoch, stored);
package_query_cache_store("provides:contains:latest:demo", 7, cache_epoch, stored);
package_query_cache_clear();
REQUIRE_FALSE(package_query_cache_lookup(key, 7, cache_epoch, loaded));
REQUIRE_FALSE(package_query_cache_lookup("provides:contains:latest:demo", 7, cache_epoch, loaded));
}
// -----------------------------------------------------------------------------
// Verify that clearing the cache advances the cache epoch as well.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache clear advances cache epoch")
{
package_query_cache_clear();
const uint64_t before = package_query_cache_current_epoch();
package_query_cache_clear();
REQUIRE(package_query_cache_current_epoch() > before);
}
// -----------------------------------------------------------------------------
// Verify that stale cache-epoch rows are rejected and removed from cache.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache rejects rows from an older cache epoch")
{
package_query_cache_clear();
const uint64_t cache_epoch = package_query_cache_current_epoch();
const std::string key = "name:contains:demo";
std::vector<PackageRow> stored = {
make_cache_row("demo-1-1.x86_64", "demo"),
};
std::vector<PackageRow> loaded;
package_query_cache_store(key, 7, cache_epoch, stored);
package_query_cache_clear();
REQUIRE_FALSE(package_query_cache_lookup(key, 7, cache_epoch, loaded));
}
// -----------------------------------------------------------------------------
// Verify that cached search snapshots survive shared Base memory release.
// -----------------------------------------------------------------------------
TEST_CASE("Search cache survives shared Base memory release")
{
package_query_cache_clear();
auto &manager = BaseManager::instance();
manager.reset_for_tests();
manager.initialize_system_only_base_for_tests();
REQUIRE(manager.has_cached_base_for_tests());
const uint64_t cache_epoch = package_query_cache_current_epoch();
const uint64_t generation = manager.current_generation();
const std::string key = "name:contains:demo";
std::vector<PackageRow> stored = {
make_cache_row("demo-1-1.x86_64", "demo"),
};
std::vector<PackageRow> loaded;
package_query_cache_store(key, generation, cache_epoch, stored);
manager.drop_cached_base();
REQUIRE_FALSE(manager.has_cached_base_for_tests());
REQUIRE(package_query_cache_lookup(key, generation, cache_epoch, loaded));
REQUIRE(loaded.size() == 1);
REQUIRE(loaded[0].nevra == "demo-1-1.x86_64");
}
// -----------------------------------------------------------------------------
// Verify that a stale worker does not store rows back into a newer cache epoch.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache ignores store requests from an older cache epoch")
{
package_query_cache_clear();
const uint64_t stale_epoch = package_query_cache_current_epoch();
const std::string key = "name:contains:demo";
std::vector<PackageRow> stored = {
make_cache_row("demo-1-1.x86_64", "demo"),
};
std::vector<PackageRow> loaded;
package_query_cache_clear();
const uint64_t current_epoch = package_query_cache_current_epoch();
REQUIRE(current_epoch > stale_epoch);
package_query_cache_store(key, 7, stale_epoch, stored);
REQUIRE_FALSE(package_query_cache_lookup(key, 7, current_epoch, loaded));
}
// -----------------------------------------------------------------------------
// Verify that cached rows cannot grow without bound across many search terms.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache evicts oldest entries")
{
package_query_cache_clear();
const uint64_t cache_epoch = package_query_cache_current_epoch();
std::vector<PackageRow> stored = {
make_cache_row("demo-1-1.x86_64", "demo"),
};
std::vector<PackageRow> loaded;
package_query_cache_store("name:contains:one", 7, cache_epoch, stored);
package_query_cache_store("name:contains:two", 7, cache_epoch, stored);
package_query_cache_store("name:contains:three", 7, cache_epoch, stored);
package_query_cache_store("name:contains:four", 7, cache_epoch, stored);
REQUIRE_FALSE(package_query_cache_lookup("name:contains:one", 7, cache_epoch, loaded));
REQUIRE(package_query_cache_lookup("name:contains:two", 7, cache_epoch, loaded));
REQUIRE(package_query_cache_lookup("name:contains:three", 7, cache_epoch, loaded));
REQUIRE(package_query_cache_lookup("name:contains:four", 7, cache_epoch, loaded));
}
// -----------------------------------------------------------------------------
// Verify that reading a cached entry keeps it from being the next entry evicted.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache lookup updates entry recency")
{
package_query_cache_clear();
const uint64_t cache_epoch = package_query_cache_current_epoch();
std::vector<PackageRow> stored = {
make_cache_row("demo-1-1.x86_64", "demo"),
};
std::vector<PackageRow> loaded;
package_query_cache_store("name:contains:one", 7, cache_epoch, stored);
package_query_cache_store("name:contains:two", 7, cache_epoch, stored);
package_query_cache_store("name:contains:three", 7, cache_epoch, stored);
REQUIRE(package_query_cache_lookup("name:contains:one", 7, cache_epoch, loaded));
package_query_cache_store("name:contains:four", 7, cache_epoch, stored);
REQUIRE(package_query_cache_lookup("name:contains:one", 7, cache_epoch, loaded));
REQUIRE_FALSE(package_query_cache_lookup("name:contains:two", 7, cache_epoch, loaded));
REQUIRE(package_query_cache_lookup("name:contains:three", 7, cache_epoch, loaded));
REQUIRE(package_query_cache_lookup("name:contains:four", 7, cache_epoch, loaded));
}
// -----------------------------------------------------------------------------
// Each search mode must retain its own three entries when the other mode fills its cache.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache modes have independent capacity")
{
package_query_cache_clear();
const uint64_t epoch = package_query_cache_current_epoch();
const auto packages = backend_search_options(false, false);
auto providers = packages;
providers.find_provider = true;
const std::vector<PackageRow> rows = { make_cache_row("demo-1-1.x86_64", "demo") };
std::vector<PackageRow> loaded;
for (const std::string term : { "one", "two", "three" }) {
package_query_cache_store(package_query_cache_key_for(term, packages), 7, epoch, rows);
}
for (const std::string term : { "one", "two", "three", "four" }) {
package_query_cache_store(package_query_cache_key_for(term, providers), 7, epoch, rows);
}
for (const std::string term : { "one", "two", "three" }) {
REQUIRE(package_query_cache_lookup(package_query_cache_key_for(term, packages), 7, epoch, loaded));
}
REQUIRE_FALSE(package_query_cache_lookup(package_query_cache_key_for("one", providers), 7, epoch, loaded));
for (const std::string term : { "five", "six", "seven", "eight" }) {
package_query_cache_store(package_query_cache_key_for(term, packages), 7, epoch, rows);
}
for (const std::string term : { "two", "three", "four" }) {
REQUIRE(package_query_cache_lookup(package_query_cache_key_for(term, providers), 7, epoch, loaded));
}
REQUIRE_FALSE(package_query_cache_lookup(package_query_cache_key_for("five", packages), 7, epoch, loaded));
}
// -----------------------------------------------------------------------------
// Repository changes must invalidate provider rows and reject results from earlier searches.
// -----------------------------------------------------------------------------
TEST_CASE("Package query cache validates provider generation and epoch")
{
package_query_cache_clear();
const uint64_t epoch = package_query_cache_current_epoch();
const std::string key = "provides:contains:latest:demo";
const std::vector<PackageRow> rows = { make_cache_row("demo-1-1.x86_64", "demo") };
std::vector<PackageRow> loaded;
package_query_cache_store(key, 7, epoch, rows);
REQUIRE_FALSE(package_query_cache_lookup(key, 8, epoch, loaded));
REQUIRE_FALSE(package_query_cache_lookup(key, 7, epoch, loaded));
package_query_cache_clear();
const uint64_t new_epoch = package_query_cache_current_epoch();
package_query_cache_store(key, 7, epoch, rows);
REQUIRE_FALSE(package_query_cache_lookup(key, 7, new_epoch, loaded));
package_query_cache_store(key, 7, new_epoch, rows);
REQUIRE(package_query_cache_lookup(key, 7, new_epoch, loaded));
}
// -----------------------------------------------------------------------------
// EOF
// -----------------------------------------------------------------------------