diff options
author | Juha Alanen <juha.alanen@mapbox.com> | 2019-11-20 13:54:56 +0200 |
---|---|---|
committer | Juha Alanen <juha.alanen@mapbox.com> | 2019-11-21 14:39:47 +0200 |
commit | d777ba54bdbd9fce305808ea23801905ae8806cd (patch) | |
tree | 7f518246f877e537a93abe4417da32003d8cfad2 /test | |
parent | bdd7e530788b56095657f911dfeff20794991796 (diff) | |
download | qtlocation-mapboxgl-d777ba54bdbd9fce305808ea23801905ae8806cd.tar.gz |
[test] Add test for priority of online requests for expired resources
Diffstat (limited to 'test')
-rw-r--r-- | test/storage/default_file_source.test.cpp | 77 |
1 files changed, 76 insertions, 1 deletions
diff --git a/test/storage/default_file_source.test.cpp b/test/storage/default_file_source.test.cpp index 05912e241e..52051ac839 100644 --- a/test/storage/default_file_source.test.cpp +++ b/test/storage/default_file_source.test.cpp @@ -1,7 +1,8 @@ #include <mbgl/actor/actor.hpp> -#include <mbgl/test/util.hpp> #include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/network_status.hpp> #include <mbgl/storage/resource_transform.hpp> +#include <mbgl/test/util.hpp> #include <mbgl/util/run_loop.hpp> using namespace mbgl; @@ -661,3 +662,77 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(RespondToStaleMustRevalidate)) { loop.run(); } + +// Test that requests for expired resources have lower priority than requests for new resources +TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CachedResourceLowPriority)) { + util::RunLoop loop; + DefaultFileSource fs(":memory:", "."); + + Response response; + std::size_t online_response_counter = 0; + + using namespace std::chrono_literals; + response.expires = util::now() - 1h; + + // Put existing values into the cache. + Resource resource1{Resource::Unknown, "http://127.0.0.1:3000/load/3", {}, Resource::LoadingMethod::All}; + response.data = std::make_shared<std::string>("Cached Request 3"); + fs.put(resource1, response); + + Resource resource2{Resource::Unknown, "http://127.0.0.1:3000/load/4", {}, Resource::LoadingMethod::All}; + response.data = std::make_shared<std::string>("Cached Request 4"); + fs.put(resource2, response); + + fs.setMaximumConcurrentRequests(1); + NetworkStatus::Set(NetworkStatus::Status::Offline); + + // Ensure that the online requests for new resources are processed first. + Resource nonCached1{Resource::Unknown, "http://127.0.0.1:3000/load/1", {}, Resource::LoadingMethod::All}; + std::unique_ptr<AsyncRequest> req1 = fs.request(nonCached1, [&](Response res) { + online_response_counter++; + req1.reset(); + EXPECT_EQ(online_response_counter, 1); // make sure this is responded first + EXPECT_EQ("Request 1", *res.data); + }); + + Resource nonCached2{Resource::Unknown, "http://127.0.0.1:3000/load/2", {}, Resource::LoadingMethod::All}; + std::unique_ptr<AsyncRequest> req2 = fs.request(nonCached2, [&](Response res) { + online_response_counter++; + req2.reset(); + EXPECT_EQ(online_response_counter, 2); // make sure this is responded second + EXPECT_EQ("Request 2", *res.data); + }); + + bool req3CachedResponseReceived = false; + std::unique_ptr<AsyncRequest> req3 = fs.request(resource1, [&](Response res) { + // Offline callback is received first + if (!req3CachedResponseReceived) { + EXPECT_EQ("Cached Request 3", *res.data); + req3CachedResponseReceived = true; + } else { + online_response_counter++; + req3.reset(); + EXPECT_EQ(online_response_counter, 3); + EXPECT_EQ("Request 3", *res.data); + } + }); + + bool req4CachedResponseReceived = false; + std::unique_ptr<AsyncRequest> req4 = fs.request(resource2, [&](Response res) { + // Offline callback is received first + if (!req4CachedResponseReceived) { + EXPECT_EQ("Cached Request 4", *res.data); + req4CachedResponseReceived = true; + } else { + online_response_counter++; + req4.reset(); + EXPECT_EQ(online_response_counter, 4); + EXPECT_EQ("Request 4", *res.data); + loop.stop(); + } + }); + + NetworkStatus::Set(NetworkStatus::Status::Online); + + loop.run(); +} |