[core] Add unit tests for ambient cache size

This commit is contained in:
Alexander Shalamov
2019-09-12 22:36:14 +03:00
parent 202b5a8ecc
commit 30aaa76910
2 changed files with 52 additions and 8 deletions

Binary file not shown.

View File

@@ -714,19 +714,22 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(DeleteRegion)) {
EXPECT_LT(sizeWithOneRegion, sizeWithTwoRegions);
db.runPackDatabaseAutomatically(true);
db.deleteRegion(std::move(*region2));
// The size of the database has shrunk right away.
// After clearing the cache, the size of the database
// should get back to the original size.
db.clearAmbientCache();
// The size of the database has shrunk right away after deleted region
// is evicted from an ambient cache.
const size_t sizeWithoutRegions = util::read_file(filename).size();
ASSERT_EQ(0u, db.listRegions().value().size());
EXPECT_LT(sizeWithoutRegions, sizeWithOneRegion);
// The tiles from the offline region will migrate to the
// ambient cache and shrink the database to the maximum
// size defined by default.
EXPECT_LE(sizeWithoutRegions, util::DEFAULT_MAX_CACHE_SIZE);
// After clearing the cache, the size of the database
// should get back to the original size.
db.clearAmbientCache();
ASSERT_EQ(0u, db.listRegions().value().size());
EXPECT_LT(sizeWithoutRegions, sizeWithOneRegion);
}
EXPECT_EQ(initialSize, util::read_file(filename).size());
@@ -1059,7 +1062,8 @@ TEST(OfflineDatabase, PutEvictsLeastRecentlyUsedResources) {
Response response;
response.data = randomString(1024);
for (uint32_t i = 1; i <= 100; i++) {
// Add 101 resource to ambient cache, 1 over defined limit.
for (uint32_t i = 1; i <= 101; ++i) {
Resource resource = Resource::style("http://example.com/"s + util::toString(i));
db.put(resource, response);
EXPECT_TRUE(bool(db.get(resource))) << i;
@@ -1070,6 +1074,45 @@ TEST(OfflineDatabase, PutEvictsLeastRecentlyUsedResources) {
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, OfflineRegionDoesNotAffectAmbientCacheSize) {
FixtureLog log;
OfflineDatabase db(":memory:");
unsigned dataSize = 1024u * 100u;
unsigned numberOfCachedResources = 2u;
unsigned databasePage = 4096u;
unsigned pageOverhead = numberOfCachedResources * databasePage;
// 200KB ambient cache limit + database page overhead.
db.setMaximumAmbientCacheSize(dataSize * numberOfCachedResources + pageOverhead);
Response response;
response.data = randomString(dataSize);
// First 100KB ambient cache resource.
db.put(Resource::style("http://example.com/ambient1.json"s), response);
OfflineTilePyramidRegionDefinition definition(
"http://example.com/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0, false);
auto region = db.createRegion(definition, OfflineRegionMetadata());
// 1MB of offline region data.
for (std::size_t i = 0; i < 5; ++i) {
const Resource tile = Resource::tile("mapbox://tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.putRegionResource(region->getID(), tile, response);
const Resource style = Resource::style("mapbox://style_" + std::to_string(i));
db.putRegionResource(region->getID(), style, response);
}
// Second 100KB ambient cache resource.
db.put(Resource::style("http://example.com/ambient2.json"s), response);
// Offline region resources should not affect ambient cache size.
EXPECT_TRUE(bool(db.get(Resource::style("http://example.com/ambient1.json"s))));
EXPECT_TRUE(bool(db.get(Resource::style("http://example.com/ambient2.json"s))));
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, PutRegionResourceDoesNotEvict) {
FixtureLog log;
OfflineDatabase db(":memory:");
@@ -1098,7 +1141,8 @@ TEST(OfflineDatabase, PutFailsWhenEvictionInsuffices) {
db.setMaximumAmbientCacheSize(1024 * 100);
Response big;
big.data = randomString(1024 * 100);
// One page over the cache size.
big.data = randomString(1024 * 100 + 4096);
EXPECT_FALSE(db.put(Resource::style("http://example.com/big"), big).first);