summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2019-09-12 22:36:14 +0300
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2020-03-07 10:14:18 +0200
commit30aaa76910df6c84157f5eec26d39d15dd5860aa (patch)
tree991107c81826418ec83165debf1b436178c55049 /test
parent202b5a8ecc915da301f9550bcfc145a39c7f2c8d (diff)
downloadqtlocation-mapboxgl-30aaa76910df6c84157f5eec26d39d15dd5860aa.tar.gz
[core] Add unit tests for ambient cache size
Diffstat (limited to 'test')
-rw-r--r--test/fixtures/offline_database/no_auto_vacuum.dbbin61440 -> 102400 bytes
-rw-r--r--test/storage/offline_database.test.cpp60
2 files changed, 52 insertions, 8 deletions
diff --git a/test/fixtures/offline_database/no_auto_vacuum.db b/test/fixtures/offline_database/no_auto_vacuum.db
index f19e639933..d4222b23ca 100644
--- a/test/fixtures/offline_database/no_auto_vacuum.db
+++ b/test/fixtures/offline_database/no_auto_vacuum.db
Binary files differ
diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp
index f344151297..146cb33530 100644
--- a/test/storage/offline_database.test.cpp
+++ b/test/storage/offline_database.test.cpp
@@ -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);