From fc7139e4b2e436fa12035e11cc693d3563b05ef7 Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Thu, 12 Sep 2019 22:36:14 +0300 Subject: [PATCH] [core] Add unit tests for ambient cache size --- test/storage/offline_database.test.cpp | 39 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp index 24234b06246..7845613cbbf 100644 --- a/test/storage/offline_database.test.cpp +++ b/test/storage/offline_database.test.cpp @@ -994,7 +994,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; @@ -1005,6 +1006,39 @@ TEST(OfflineDatabase, PutEvictsLeastRecentlyUsedResources) { EXPECT_EQ(0u, log.uncheckedCount()); } +TEST(OfflineDatabase, OfflineRegionDoesNotAffectAmbientCacheSize) { + FixtureLog log; + OfflineDatabase db(":memory:"); + // 200KB ambient cache limit. + db.setMaximumAmbientCacheSize(1024 * 100 * 2); + + Response response; + response.data = randomString(1024 * 100); + + // 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:"); @@ -1033,7 +1067,8 @@ TEST(OfflineDatabase, PutFailsWhenEvictionInsuffices) { db.setMaximumAmbientCacheSize(1024 * 100); Response big; - big.data = randomString(1024 * 100); + // One byte over the cache size. + big.data = randomString(1024 * 100 + 1); EXPECT_FALSE(db.put(Resource::style("http://example.com/big"), big).first);