From 6d4e5f57ec68751ca5857d72f444331f3674733e Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Mon, 25 Nov 2019 14:44:46 +0200 Subject: [core] Add OfflineDatabase.Pack unit test --- include/mbgl/storage/default_file_source.hpp | 6 +++--- test/storage/offline_database.test.cpp | 30 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index 4ec72180d5..2942a25a85 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -200,7 +200,7 @@ public: * Sets whether packing the database file occurs automatically after an offline * region is deleted (deleteOfflineRegion()) or the ambient cache is cleared * (clearAmbientCache()). - * + * * By default, packing is enabled. If disabled, disk space will not be freed * after resources are removed unless packDatabase() is explicitly called. */ @@ -223,8 +223,8 @@ public: /* * Erase resources from the ambient cache, freeing storage space. * - * Erases the ambient cache, freeing resources. - * + * Erases the ambient cache, freeing resources. + * * Note that this operation can be potentially slow if packing the database * occurs automatically (see runPackDatabaseAutomatically() and packDatabase()). * diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp index e9164e7c66..ef7ad257d7 100644 --- a/test/storage/offline_database.test.cpp +++ b/test/storage/offline_database.test.cpp @@ -733,6 +733,36 @@ TEST(OfflineDatabase, TEST_REQUIRES_WRITE(DeleteRegion)) { EXPECT_EQ(0u, log.uncheckedCount()); } +TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Pack)) { + FixtureLog log; + deleteDatabaseFiles(); + + OfflineDatabase db(filename); + size_t initialSize = util::read_file(filename).size(); + db.runPackDatabaseAutomatically(false); + + Response response; + response.data = randomString(.5 * 1024 * 1024); + + for (unsigned i = 0; i < 50; ++i) { + const Resource tile = Resource::tile("mapbox://tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ); + db.put(tile, response); + + const Resource style = Resource::style("mapbox://style_" + std::to_string(i)); + db.put(style, response); + } + size_t populatedSize = util::read_file(filename).size(); + ASSERT_GT(populatedSize, initialSize); + + db.clearAmbientCache(); + EXPECT_EQ(populatedSize, util::read_file(filename).size()); + EXPECT_EQ(0u, log.uncheckedCount()); + + db.pack(); + EXPECT_EQ(initialSize, util::read_file(filename).size()); + EXPECT_EQ(0u, log.uncheckedCount()); +} + TEST(OfflineDatabase, MapboxTileLimitExceeded) { FixtureLog log; -- cgit v1.2.1