forked from mapbox/mapbox-gl-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffline_database.test.cpp
More file actions
1964 lines (1536 loc) · 72.5 KB
/
offline_database.test.cpp
File metadata and controls
1964 lines (1536 loc) · 72.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <mbgl/test/util.hpp>
#include <mbgl/test/fixture_log_observer.hpp>
#include <mbgl/test/sqlite3_test_fs.hpp>
#include <mbgl/storage/offline_database.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/storage/sqlite3.hpp>
#include <thread>
#include <random>
using namespace std::literals::string_literals;
using namespace mbgl;
using mapbox::sqlite::ResultCode;
static constexpr const char* filename = "test/fixtures/offline_database/offline.db";
static constexpr const char* filename_sideload = "test/fixtures/offline_database/offline_sideload.db";
#ifndef __QT__ // Qt doesn't expose the ability to register virtual file system handlers.
static constexpr const char* filename_test_fs = "file:test/fixtures/offline_database/offline.db?vfs=test_fs";
#endif
static void deleteDatabaseFiles() {
// Delete leftover journaling files as well.
util::deleteFile(filename);
util::deleteFile(filename + "-wal"s);
util::deleteFile(filename + "-journal"s);
}
static std::shared_ptr<std::string> randomString(size_t size) {
auto result = std::make_shared<std::string>(size, 0);
std::mt19937 random;
for (size_t i = 0; i < size; i++) {
(*result)[i] = static_cast<char>(random());
}
return result;
}
static FixtureLog::Message error(ResultCode code, const char* message) {
return { EventSeverity::Error, Event::Database, static_cast<int64_t>(code), message };
}
static __attribute__((unused)) FixtureLog::Message warning(ResultCode code, const char* message) {
return { EventSeverity::Warning, Event::Database, static_cast<int64_t>(code), message };
}
static int databasePageCount(const std::string& path) {
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly);
mapbox::sqlite::Statement stmt{ db, "pragma page_count" };
mapbox::sqlite::Query query{ stmt };
query.run();
return query.get<int>(0);
}
static int databaseUserVersion(const std::string& path) {
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly);
mapbox::sqlite::Statement stmt{ db, "pragma user_version" };
mapbox::sqlite::Query query{ stmt };
query.run();
return query.get<int>(0);
}
static std::string databaseJournalMode(const std::string& path) {
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly);
mapbox::sqlite::Statement stmt{ db, "pragma journal_mode" };
mapbox::sqlite::Query query{ stmt };
query.run();
return query.get<std::string>(0);
}
static int databaseSyncMode(const std::string& path) {
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly);
mapbox::sqlite::Statement stmt{ db, "pragma synchronous" };
mapbox::sqlite::Query query{ stmt };
query.run();
return query.get<int>(0);
}
static std::vector<std::string> databaseTableColumns(const std::string& path, const std::string& name) {
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly);
const auto sql = std::string("pragma table_info(") + name + ")";
mapbox::sqlite::Statement stmt{ db, sql.c_str() };
mapbox::sqlite::Query query{ stmt };
std::vector<std::string> columns;
while (query.run()) {
columns.push_back(query.get<std::string>(1));
}
return columns;
}
static int databaseAutoVacuum(const std::string& path) {
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(path, mapbox::sqlite::ReadOnly);
mapbox::sqlite::Statement stmt{db, "pragma auto_vacuum"};
mapbox::sqlite::Query query{stmt};
query.run();
return query.get<int>(0);
}
namespace fixture {
const Resource resource{ Resource::Style, "mapbox://test" };
const Resource tile = Resource::tile("mapbox://test", 1, 0, 0, 0, Tileset::Scheme::XYZ);
const Response response = [] {
Response res;
res.data = std::make_shared<std::string>("first");
return res;
}();
} // namespace fixture
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Create)) {
FixtureLog log;
deleteDatabaseFiles();
OfflineDatabase db(filename);
EXPECT_FALSE(bool(db.get({ Resource::Unknown, "mapbox://test" })));
EXPECT_EQ(0u, log.uncheckedCount());
}
#ifndef __QT__ // Qt doesn't expose the ability to register virtual file system handlers.
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(CreateFail)) {
FixtureLog log;
deleteDatabaseFiles();
test::SQLite3TestFS fs;
// Opening the database will fail because our mock VFS returns a SQLITE_CANTOPEN error because
// it is not allowed to create the file. The OfflineDatabase object should handle this gracefully
// and treat it like an empty cache that can't be written to.
fs.allowFileCreate(false);
OfflineDatabase db(filename_test_fs);
EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't open database: unable to open database file")));
EXPECT_EQ(0u, log.uncheckedCount());
// We can try to insert things into the cache, but since the cache database isn't open, it won't be stored.
for (const auto& res : { fixture::resource, fixture::tile }) {
EXPECT_EQ(std::make_pair(false, uint64_t(0)), db.put(res, fixture::response));
EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't write resource: unable to open database file")));
EXPECT_EQ(0u, log.uncheckedCount());
}
// We can also still "query" the database even though it is not open, and we will always get an empty result.
for (const auto& res : { fixture::resource, fixture::tile }) {
EXPECT_FALSE(bool(db.get(res)));
EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't update timestamp: unable to open database file")));
EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't read resource: unable to open database file")));
EXPECT_EQ(0u, log.uncheckedCount());
}
// Now, we're "freeing up" some space on the disk, and try to insert and query again. This time, we should
// be opening the datbase, creating the schema, and writing the data so that we can retrieve it again.
fs.allowFileCreate(true);
for (const auto& res : { fixture::resource, fixture::tile }) {
EXPECT_EQ(std::make_pair(true, uint64_t(5)), db.put(res, fixture::response));
auto result = db.get(res);
EXPECT_EQ(0u, log.uncheckedCount());
ASSERT_TRUE(result && result->data);
EXPECT_EQ("first", *result->data);
}
// Next, set the file system to read only mode and try to read the data again. While we can't
// write anymore, we should still be able to read, and the query that tries to update the last
// accessed timestamp may fail without crashing.
fs.allowFileCreate(false);
fs.setWriteLimit(0);
for (const auto& res : { fixture::resource, fixture::tile }) {
auto result = db.get(res);
EXPECT_EQ(1u, log.count(warning(ResultCode::CantOpen, "Can't update timestamp: unable to open database file")));
EXPECT_EQ(0u, log.uncheckedCount());
ASSERT_TRUE(result && result->data);
EXPECT_EQ("first", *result->data);
}
fs.setDebug(false);
// We're allowing SQLite to create a journal file, but restrict the number of bytes it
// can write so that it can start writing the journal file, but eventually fails during the
// timestamp update.
fs.allowFileCreate(true);
fs.setWriteLimit(8192);
for (const auto& res : { fixture::resource, fixture::tile }) {
auto result = db.get(res);
EXPECT_EQ(1u, log.count(warning(ResultCode::Full, "Can't update timestamp: database or disk is full")));
EXPECT_EQ(0u, log.uncheckedCount());
ASSERT_TRUE(result && result->data);
EXPECT_EQ("first", *result->data);
}
// Lastly, we're disabling all I/O to simulate a backgrounded app that is restricted from doing
// any disk I/O at all.
fs.setWriteLimit(-1);
fs.allowIO(false);
for (const auto& res : { fixture::resource, fixture::tile }) {
// First, try reading.
auto result = db.get(res);
EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't update timestamp: authorization denied")));
EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't read resource: authorization denied")));
EXPECT_EQ(0u, log.uncheckedCount());
EXPECT_FALSE(result);
// Now try inserting.
EXPECT_EQ(std::make_pair(false, uint64_t(0)), db.put(res, fixture::response));
EXPECT_EQ(1u, log.count(warning(ResultCode::Auth, "Can't write resource: authorization denied")));
EXPECT_EQ(0u, log.uncheckedCount());
}
// Allow deleting the database.
fs.reset();
}
#endif // __QT__
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(SchemaVersion)) {
FixtureLog log;
deleteDatabaseFiles();
{
mapbox::sqlite::Database db = mapbox::sqlite::Database::open(filename, mapbox::sqlite::ReadWriteCreate);
db.setBusyTimeout(Milliseconds(1000));
db.exec("PRAGMA user_version = 1");
}
{
OfflineDatabase db(filename);
}
EXPECT_EQ(6, databaseUserVersion(filename));
OfflineDatabase db(filename);
// Now try inserting and reading back to make sure we have a valid database.
for (const auto& res : { fixture::resource, fixture::tile }) {
EXPECT_EQ(std::make_pair(true, uint64_t(5)), db.put(res, fixture::response));
EXPECT_EQ(0u, log.uncheckedCount());
auto result = db.get(res);
EXPECT_EQ(0u, log.uncheckedCount());
ASSERT_TRUE(result && result->data);
EXPECT_EQ("first", *result->data);
}
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Invalid)) {
FixtureLog log;
deleteDatabaseFiles();
util::write_file(filename, "this is an invalid file");
OfflineDatabase db(filename);
// Checking two possibilities for the error string because it apparently changes between SQLite versions.
EXPECT_EQ(1u, log.count(error(ResultCode::NotADB, "Can't open database: file is encrypted or is not a database"), true) +
log.count(error(ResultCode::NotADB, "Can't open database: file is not a database"), true));
EXPECT_EQ(1u, log.count({ EventSeverity::Warning, Event::Database, -1, "Removing existing incompatible offline database" }));
// Now try inserting and reading back to make sure we have a valid database.
for (const auto& res : { fixture::resource, fixture::tile }) {
EXPECT_EQ(std::make_pair(true, uint64_t(5)), db.put(res, fixture::response));
EXPECT_EQ(0u, log.uncheckedCount());
auto result = db.get(res);
EXPECT_EQ(0u, log.uncheckedCount());
ASSERT_TRUE(result && result->data);
EXPECT_EQ("first", *result->data);
}
}
TEST(OfflineDatabase, PutDoesNotStoreConnectionErrors) {
FixtureLog log;
OfflineDatabase db(":memory:");
Resource resource { Resource::Unknown, "http://example.com/" };
Response response;
response.error = std::make_unique<Response::Error>(Response::Error::Reason::Connection);
db.put(resource, response);
EXPECT_FALSE(bool(db.get(resource)));
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, PutDoesNotStoreServerErrors) {
FixtureLog log;
OfflineDatabase db(":memory:");
Resource resource { Resource::Unknown, "http://example.com/" };
Response response;
response.error = std::make_unique<Response::Error>(Response::Error::Reason::Server);
db.put(resource, response);
EXPECT_FALSE(bool(db.get(resource)));
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, PutResource) {
FixtureLog log;
OfflineDatabase db(":memory:");
Resource resource { Resource::Style, "http://example.com/" };
Response response;
response.data = std::make_shared<std::string>("first");
auto insertPutResult = db.put(resource, response);
EXPECT_TRUE(insertPutResult.first);
EXPECT_EQ(5u, insertPutResult.second);
auto insertGetResult = db.get(resource);
EXPECT_EQ(nullptr, insertGetResult->error.get());
EXPECT_EQ("first", *insertGetResult->data);
response.data = std::make_shared<std::string>("second");
auto updatePutResult = db.put(resource, response);
EXPECT_FALSE(updatePutResult.first);
EXPECT_EQ(6u, updatePutResult.second);
auto updateGetResult = db.get(resource);
EXPECT_EQ(nullptr, updateGetResult->error.get());
EXPECT_EQ("second", *updateGetResult->data);
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(GetResourceFromOfflineRegion)) {
FixtureLog log;
deleteDatabaseFiles();
util::copyFile(filename, "test/fixtures/offline_database/satellite_test.db");
OfflineDatabase db(filename);
Resource resource = Resource::style("mapbox://styles/mapbox/satellite-v9");
ASSERT_TRUE(db.get(resource));
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, PutAndGetResource) {
FixtureLog log;
OfflineDatabase db(":memory:");
Response response1;
response1.data = std::make_shared<std::string>("foobar");
Resource resource = Resource::style("mapbox://example.com/style");
db.put(resource, response1);
auto response2 = db.get(resource);
ASSERT_EQ(*response1.data, *(*response2).data);
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, PutTile) {
FixtureLog log;
OfflineDatabase db(":memory:");
Resource resource { Resource::Tile, "http://example.com/" };
resource.tileData = Resource::TileData {
"http://example.com/",
1,
0,
0,
0
};
Response response;
response.data = std::make_shared<std::string>("first");
auto insertPutResult = db.put(resource, response);
EXPECT_TRUE(insertPutResult.first);
EXPECT_EQ(5u, insertPutResult.second);
auto insertGetResult = db.get(resource);
EXPECT_EQ(nullptr, insertGetResult->error.get());
EXPECT_EQ("first", *insertGetResult->data);
response.data = std::make_shared<std::string>("second");
auto updatePutResult = db.put(resource, response);
EXPECT_FALSE(updatePutResult.first);
EXPECT_EQ(6u, updatePutResult.second);
auto updateGetResult = db.get(resource);
EXPECT_EQ(nullptr, updateGetResult->error.get());
EXPECT_EQ("second", *updateGetResult->data);
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, PutResourceNoContent) {
FixtureLog log;
OfflineDatabase db(":memory:");
Resource resource { Resource::Style, "http://example.com/" };
Response response;
response.noContent = true;
db.put(resource, response);
auto res = db.get(resource);
EXPECT_EQ(nullptr, res->error);
EXPECT_TRUE(res->noContent);
EXPECT_FALSE(res->data.get());
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, PutTileNotFound) {
FixtureLog log;
OfflineDatabase db(":memory:");
Resource resource { Resource::Tile, "http://example.com/" };
resource.tileData = Resource::TileData {
"http://example.com/",
1,
0,
0,
0
};
Response response;
response.noContent = true;
db.put(resource, response);
auto res = db.get(resource);
EXPECT_EQ(nullptr, res->error);
EXPECT_TRUE(res->noContent);
EXPECT_FALSE(res->data.get());
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, CreateRegion) {
FixtureLog log;
OfflineDatabase db(":memory:");
OfflineTilePyramidRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, true };
OfflineRegionMetadata metadata {{ 1, 2, 3 }};
auto region = db.createRegion(definition, metadata);
ASSERT_TRUE(region);
EXPECT_EQ(0u, log.uncheckedCount());
region->getDefinition().match(
[&](OfflineTilePyramidRegionDefinition& def) {
EXPECT_EQ(definition.styleURL, def.styleURL);
EXPECT_EQ(definition.bounds, def.bounds);
EXPECT_EQ(definition.minZoom, def.minZoom);
EXPECT_EQ(definition.maxZoom, def.maxZoom);
EXPECT_EQ(definition.pixelRatio, def.pixelRatio);
EXPECT_EQ(definition.includeIdeographs, def.includeIdeographs);
}, [](auto&) {
EXPECT_FALSE(false);
}
);
EXPECT_EQ(metadata, region->getMetadata());
}
TEST(OfflineDatabase, UpdateMetadata) {
FixtureLog log;
OfflineDatabase db(":memory:");
OfflineTilePyramidRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, true };
OfflineRegionMetadata metadata {{ 1, 2, 3 }};
auto region = db.createRegion(definition, metadata);
ASSERT_TRUE(region);
OfflineRegionMetadata newmetadata {{ 4, 5, 6 }};
db.updateMetadata(region->getID(), newmetadata);
auto regions = db.listRegions().value();
EXPECT_EQ(regions.at(0).getMetadata(), newmetadata);
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, ListRegions) {
FixtureLog log;
OfflineDatabase db(":memory:");
OfflineTilePyramidRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, false };
OfflineRegionMetadata metadata {{ 1, 2, 3 }};
auto region = db.createRegion(definition, metadata);
ASSERT_TRUE(region);
auto regions = db.listRegions().value();
ASSERT_EQ(1u, regions.size());
EXPECT_EQ(region->getID(), regions.at(0).getID());
regions.at(0).getDefinition().match(
[&](OfflineTilePyramidRegionDefinition& def) {
EXPECT_EQ(definition.styleURL, def.styleURL);
EXPECT_EQ(definition.bounds, def.bounds);
EXPECT_EQ(definition.minZoom, def.minZoom);
EXPECT_EQ(definition.maxZoom, def.maxZoom);
EXPECT_EQ(definition.pixelRatio, def.pixelRatio);
EXPECT_EQ(definition.includeIdeographs, def.includeIdeographs);
},
[&](auto&) {
EXPECT_FALSE(false);
});
EXPECT_EQ(metadata, regions.at(0).getMetadata());
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, GetRegionDefinition) {
FixtureLog log;
OfflineDatabase db(":memory:");
OfflineTilePyramidRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, false };
OfflineRegionMetadata metadata {{ 1, 2, 3 }};
EXPECT_EQ(0u, log.uncheckedCount());
auto region = db.createRegion(definition, metadata);
db.getRegionDefinition(region->getID())->match(
[&](OfflineTilePyramidRegionDefinition& result) {
EXPECT_EQ(definition.styleURL, result.styleURL);
EXPECT_EQ(definition.bounds, result.bounds);
EXPECT_EQ(definition.minZoom, result.minZoom);
EXPECT_EQ(definition.maxZoom, result.maxZoom);
EXPECT_EQ(definition.pixelRatio, result.pixelRatio);
EXPECT_EQ(definition.includeIdeographs, result.includeIdeographs);
},
[&](auto&) {
EXPECT_FALSE(false);
}
);
}
// Disabled due to flakiness: https://github.com/mapbox/mapbox-gl-native/issues/14966
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(DISABLED_MaximumAmbientCacheSize)) {
FixtureLog log;
deleteDatabaseFiles();
auto databaseSize = [] {
return util::read_file(filename).size();
};
{
OfflineDatabase db(filename);
}
size_t initialSize = util::read_file(filename).size();
size_t maximumSize = 50 * 1024 * 1024;
Response response;
response.data = randomString(100 * 1024);
{
OfflineDatabase db(filename);
db.setMaximumAmbientCacheSize(maximumSize); // 50 MB
OfflineTilePyramidRegionDefinition definition{ "mapbox://style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, true };
OfflineRegionMetadata metadata{{ 1, 2, 3 }};
auto region = db.createRegion(definition, metadata);
// Add 100 MB of resources (50/50 ambient/region)
for (unsigned i = 0; i < 250; ++i) {
const Resource ambientTile = Resource::tile("mapbox://ambient_tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.put(ambientTile, response);
const Resource ambientStyle = Resource::style("mapbox://ambient_style_" + std::to_string(i));
db.put(ambientStyle, response);
const Resource regionTile = Resource::tile("mapbox://region_tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.putRegionResource(region->getID(), regionTile, response);
const Resource regionStyle = Resource::style("mapbox://region_style_" + std::to_string(i));
db.putRegionResource(region->getID(), regionStyle, response);
}
}
// We are adding about 50 MB of "region" data and 50 MB,
// of "ambient" data. The effective size of the ambient
// cache will be zero here because it will try to make
// room for the region data.
EXPECT_GE(databaseSize(), maximumSize);
EXPECT_LE(databaseSize(), 60 * 1024 * 1024);
maximumSize = 30 * 1024 * 1024;
{
OfflineDatabase db(filename);
db.setMaximumAmbientCacheSize(maximumSize); // 30 MB
}
// Setting a new size to the ambient cache should have no
// effect because it is all taken by offline region anyway.
EXPECT_GE(databaseSize(), maximumSize);
EXPECT_LE(databaseSize(), 60 * 1024 * 1024);
{
OfflineDatabase db(filename);
db.setMaximumAmbientCacheSize(maximumSize); // 30 MB
db.deleteRegion(std::move(db.listRegions().value()[0]));
}
// After deleting the offline region, the data will migrate
// to the ambient cache, respecting the size defined.
EXPECT_LE(databaseSize(), maximumSize);
EXPECT_GE(databaseSize(), maximumSize / 2);
{
OfflineDatabase db(filename);
db.setMaximumAmbientCacheSize(maximumSize * 2); // 60 MB
}
// Doubling the size should have no effect if
// we don't and new tiles and if the ambient cache
// is already under the maximum size.
EXPECT_LE(databaseSize(), maximumSize);
EXPECT_GE(databaseSize(), maximumSize / 2);
{
OfflineDatabase db(filename);
db.setMaximumAmbientCacheSize(maximumSize); // 30 MB
// Add ~50 MB in ambient cache data.
for (unsigned i = 0; i < 250; ++i) {
const Resource ambientTile = Resource::tile("mapbox://ambient_tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.put(ambientTile, response);
const Resource ambientStyle = Resource::style("mapbox://ambient_style_" + std::to_string(i));
db.put(ambientStyle, response);
}
}
// Only ambient cache now, so it should respect
// the established size.
EXPECT_LE(databaseSize(), maximumSize);
EXPECT_GE(databaseSize(), maximumSize / 2);
maximumSize = 20 * 1024 * 1024;
{
OfflineDatabase db(filename);
db.setMaximumAmbientCacheSize(maximumSize); // 20 MB
}
// Should shrink again.
EXPECT_LE(databaseSize(), maximumSize);
EXPECT_GE(databaseSize(), initialSize);
{
OfflineDatabase db(filename);
db.setMaximumAmbientCacheSize(0);
for (unsigned i = 0; i < 5; ++i) {
const Resource ambientTile = Resource::tile("mapbox://ambient_tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.put(ambientTile, response);
ASSERT_FALSE(db.get(ambientTile));
const Resource ambientStyle = Resource::style("mapbox://ambient_style_" + std::to_string(i));
db.put(ambientStyle, response);
ASSERT_FALSE(db.get(ambientStyle));
}
}
// Setting the size to zero should effectively
// clear the cache now.
EXPECT_EQ(initialSize, util::read_file(filename).size());
}
namespace {
std::list<std::tuple<Resource, Response>> generateResources(const std::string& tilePrefix,
const std::string& stylePrefix) {
static const auto responseData = randomString(.5 * 1024 * 1024);
Response response;
response.data = responseData;
std::list<std::tuple<Resource, Response>> resources;
for (unsigned i = 0; i < 50; ++i) {
resources.emplace_back(Resource::tile(tilePrefix + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ),
response);
resources.emplace_back(Resource::style(stylePrefix + std::to_string(i)), response);
}
return resources;
}
} // namespace
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(DeleteRegion)) {
FixtureLog log;
deleteDatabaseFiles();
{
OfflineDatabase dbCreate(filename);
}
size_t initialSize = util::read_file(filename).size();
{
Response response;
response.data = randomString(.5 * 1024 * 1024);
OfflineDatabase db(filename);
OfflineTilePyramidRegionDefinition definition{ "mapbox://style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, true };
OfflineRegionMetadata metadata{{ 1, 2, 3 }};
auto region1 = db.createRegion(definition, metadata);
auto region2 = db.createRegion(definition, metadata);
OfflineRegionStatus status;
db.putRegionResources(region1->getID(), generateResources("mapbox://tile_1", "mapbox://style_1"), status);
db.putRegionResources(region2->getID(), generateResources("mapbox://tile_2", "mapbox://style_2"), status);
const size_t sizeWithTwoRegions = util::read_file(filename).size();
db.runPackDatabaseAutomatically(false);
db.deleteRegion(std::move(*region1));
ASSERT_EQ(1u, db.listRegions().value().size());
// Region is removed but the size of the database is the same.
EXPECT_EQ(sizeWithTwoRegions, util::read_file(filename).size());
db.pack();
// The size of the database has shrunk after pack().
const size_t sizeWithOneRegion = util::read_file(filename).size();
EXPECT_LT(sizeWithOneRegion, sizeWithTwoRegions);
db.runPackDatabaseAutomatically(true);
db.deleteRegion(std::move(*region2));
// 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();
// 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);
ASSERT_EQ(0u, db.listRegions().value().size());
EXPECT_LT(sizeWithoutRegions, sizeWithOneRegion);
}
EXPECT_EQ(initialSize, util::read_file(filename).size());
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;
uint64_t limit = 60;
OfflineDatabase db(":memory:");
db.setOfflineMapboxTileCountLimit(limit);
Response response;
response.data = randomString(4096);
auto insertAmbientTile = [&](unsigned i) {
const Resource ambientTile = Resource::tile("mapbox://ambient_tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.put(ambientTile, response);
};
auto insertRegionTile = [&](int64_t regionID, uint64_t i) {
const Resource tile = Resource::tile("mapbox://region_tile_" + std::to_string(i), 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.putRegionResource(regionID, tile, response);
};
OfflineTilePyramidRegionDefinition definition1{ "mapbox://style1", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, true };
OfflineRegionMetadata metadata1{{ 1, 2, 3 }};
OfflineTilePyramidRegionDefinition definition2{ "mapbox://style2", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, true };
OfflineRegionMetadata metadata2{{ 1, 2, 3 }};
auto region1 = db.createRegion(definition1, metadata1);
auto region2 = db.createRegion(definition2, metadata2);
// Fine because tile limit only affects offline region.
for (unsigned i = 0; i < limit * 2; ++i) {
insertAmbientTile(i);
}
ASSERT_EQ(db.getOfflineMapboxTileCount(), 0);
// Fine because this region is under the tile limit.
for (uint64_t i = 0; i < limit - 10; ++i) {
insertRegionTile(region1->getID(), i);
}
ASSERT_EQ(db.getOfflineMapboxTileCount(), limit - 10);
// Fine because this region + the previous is at the limit.
for (uint64_t i = limit; i < limit + 10; ++i) {
insertRegionTile(region2->getID(), i);
}
ASSERT_EQ(db.getOfflineMapboxTileCount(), limit);
// Full.
ASSERT_THROW(insertRegionTile(region1->getID(), 200), MapboxTileLimitExceededException);
ASSERT_THROW(insertRegionTile(region2->getID(), 201), MapboxTileLimitExceededException);
// These tiles are already on respective
// regions.
insertRegionTile(region1->getID(), 0);
insertRegionTile(region2->getID(), 60);
// Should be fine, ambient tile.
insertAmbientTile(333);
// Also fine, not Mapbox.
const Resource notMapboxTile = Resource::tile("foobar://region_tile", 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.putRegionResource(region1->getID(), notMapboxTile, response);
// These tiles are not on the region they are
// being added to, but exist on another region,
// so they do not add to the total size.
insertRegionTile(region2->getID(), 0);
insertRegionTile(region1->getID(), 60);
ASSERT_EQ(db.getOfflineMapboxTileCount(), limit);
// The tile 1 belongs to two regions and will
// still count as resource.
db.deleteRegion(std::move(*region2));
ASSERT_EQ(db.getOfflineMapboxTileCount(), 51);
// Add new tiles to the region 1. We are adding
// 10, which would blow up the limit if it wasn't
// for the fact that tile 60 is already on the
// database and will not count.
for (uint64_t i = limit; i < limit + 10; ++i) {
insertRegionTile(region1->getID(), i);
}
// Full again.
ASSERT_THROW(insertRegionTile(region1->getID(), 202), MapboxTileLimitExceededException);
db.deleteRegion(std::move(*region1));
ASSERT_EQ(0u, db.listRegions().value().size());
ASSERT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, Invalidate) {
using namespace std::chrono_literals;
FixtureLog log;
OfflineDatabase db(":memory:");
Response response;
response.noContent = true;
response.mustRevalidate = false;
response.expires = util::now() + 1h;
const Resource ambientTile = Resource::tile("mapbox://tile_ambient", 1, 0, 0, 0, Tileset::Scheme::XYZ);
db.put(ambientTile, response);
const Resource ambientStyle = Resource::style("mapbox://style_ambient");
db.put(ambientStyle, response);
OfflineTilePyramidRegionDefinition definition { "mapbox://style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0, true };
OfflineRegionMetadata metadata {{ 1, 2, 3 }};
auto region1 = db.createRegion(definition, metadata);
const Resource region1Tile = Resource::tile("mapbox://tile_offline_region1", 1.0, 0, 0, 0, Tileset::Scheme::XYZ);
db.putRegionResource(region1->getID(), region1Tile, response);
const Resource region1Style = Resource::style("mapbox://style_offline_region1");
db.putRegionResource(region1->getID(), region1Style, response);
auto region2 = db.createRegion(definition, metadata);
const Resource region2Tile = Resource::tile("mapbox://tile_offline_region2", 1.0, 0, 0, 0, Tileset::Scheme::XYZ);
db.putRegionResource(region2->getID(), region2Tile, response);
const Resource region2Style = Resource::style("mapbox://style_offline_region2");
db.putRegionResource(region2->getID(), region2Style, response);
// Prior to invalidation, all tiles are usable.
EXPECT_TRUE(db.get(ambientTile)->isUsable());
EXPECT_TRUE(db.get(ambientStyle)->isUsable());
EXPECT_TRUE(db.get(region1Tile)->isUsable());
EXPECT_TRUE(db.get(region1Style)->isUsable());
EXPECT_TRUE(db.get(region2Tile)->isUsable());
EXPECT_TRUE(db.get(region2Style)->isUsable());
// Invalidate a region will not invalidate ambient
// tiles or other regions.
EXPECT_TRUE(db.invalidateRegion(region1->getID()) == nullptr);
EXPECT_TRUE(db.get(ambientTile)->isUsable());
EXPECT_TRUE(db.get(ambientStyle)->isUsable());
EXPECT_FALSE(db.get(region1Tile)->isUsable());
EXPECT_FALSE(db.get(region1Style)->isUsable());
EXPECT_TRUE(db.get(region2Tile)->isUsable());
EXPECT_TRUE(db.get(region2Style)->isUsable());
// Invalidate the ambient cache will not invalidate
// the regions that are still valid.
EXPECT_TRUE(db.invalidateAmbientCache() == nullptr);
EXPECT_FALSE(db.get(ambientTile)->isUsable());
EXPECT_FALSE(db.get(ambientStyle)->isUsable());
EXPECT_FALSE(db.get(region1Tile)->isUsable());
EXPECT_FALSE(db.get(region1Style)->isUsable());
EXPECT_TRUE(db.get(region2Tile)->isUsable());
EXPECT_TRUE(db.get(region2Style)->isUsable());
// Sanity check.
EXPECT_TRUE(db.get(ambientTile)->expires < util::now());
EXPECT_TRUE(db.get(ambientStyle)->expires < util::now());
EXPECT_TRUE(db.get(region1Tile)->expires < util::now());
EXPECT_TRUE(db.get(region1Style)->expires < util::now());
EXPECT_TRUE(db.get(region2Tile)->expires > util::now());
EXPECT_TRUE(db.get(region2Style)->expires > util::now());
EXPECT_TRUE(db.get(ambientTile)->mustRevalidate);
EXPECT_TRUE(db.get(ambientStyle)->mustRevalidate);
EXPECT_TRUE(db.get(region1Tile)->mustRevalidate);
EXPECT_TRUE(db.get(region1Style)->mustRevalidate);
EXPECT_FALSE(db.get(region2Tile)->mustRevalidate);
EXPECT_FALSE(db.get(region2Style)->mustRevalidate);
// Should not throw.
EXPECT_TRUE(db.invalidateRegion(region2->getID()) == nullptr);
EXPECT_TRUE(db.invalidateRegion(region2->getID()) == nullptr);
EXPECT_TRUE(db.invalidateRegion(123) == nullptr);
// Invalidate != delete.
auto regions = db.listRegions().value();
ASSERT_EQ(2u, regions.size());
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(ClearAmbientCache)) {
FixtureLog log;
deleteDatabaseFiles();
{
OfflineDatabase dbCreate(filename);
}
size_t initialSize = util::read_file(filename).size();
{
Response response;
response.data = randomString(.5 * 1024 * 1024);
OfflineDatabase db(filename);
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);
}
db.clearAmbientCache();
}
EXPECT_EQ(initialSize, util::read_file(filename).size());
EXPECT_EQ(0u, log.uncheckedCount());
}
TEST(OfflineDatabase, CreateRegionInfiniteMaxZoom) {
FixtureLog log;
OfflineDatabase db(":memory:");
OfflineTilePyramidRegionDefinition definition { "", LatLngBounds::world(), 0, INFINITY, 1.0, false };
OfflineRegionMetadata metadata;
auto region = db.createRegion(definition, metadata);
ASSERT_TRUE(region);
EXPECT_EQ(0u, log.uncheckedCount());
region->getDefinition().match([&](auto& def) {
EXPECT_EQ(0, def.minZoom);