Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Add unit tests for ambient cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov committed Sep 12, 2019
1 parent 11783aa commit 6dd778d
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions test/storage/offline_database.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:");
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 6dd778d

Please sign in to comment.