diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-11-02 17:00:46 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2015-11-02 17:25:08 +0100 |
commit | 04273fbb1c1dc2c44b804cd209580014bc75ddd7 (patch) | |
tree | 3d562f67c0c0f2f3b28362ae25b1eede1d1739ee /test | |
parent | 4d5c6333be52aae4a9c72f4b01941e16ead503f4 (diff) | |
download | qtlocation-mapboxgl-04273fbb1c1dc2c44b804cd209580014bc75ddd7.tar.gz |
[core] Make DefaultFileSource react to all NetworkStatus changes
Diffstat (limited to 'test')
-rw-r--r-- | test/storage/http_error.cpp | 3 | ||||
-rw-r--r-- | test/storage/http_retry_network_status.cpp | 57 |
2 files changed, 57 insertions, 3 deletions
diff --git a/test/storage/http_error.cpp b/test/storage/http_error.cpp index 00deeb063a..94a3f29a4e 100644 --- a/test/storage/http_error.cpp +++ b/test/storage/http_error.cpp @@ -92,7 +92,4 @@ TEST_F(Storage, HTTPError) { }); uv_run(uv_default_loop(), UV_RUN_DEFAULT); - - // Run again so that the timer handle can be properly closed. - uv_run(uv_default_loop(), UV_RUN_DEFAULT); } diff --git a/test/storage/http_retry_network_status.cpp b/test/storage/http_retry_network_status.cpp index d0209e0bf6..3f8c469cf9 100644 --- a/test/storage/http_retry_network_status.cpp +++ b/test/storage/http_retry_network_status.cpp @@ -47,3 +47,60 @@ TEST_F(Storage, HTTPNetworkStatusChange) { uv_run(uv_default_loop(), UV_RUN_DEFAULT); } + +// Tests that a change in network status preempts requests that failed due to connection or +// reachability issues. +TEST_F(Storage, HTTPNetworkStatusChangePreempt) { + SCOPED_TEST(HTTPNetworkStatusChangePreempt) + + using namespace mbgl; + + DefaultFileSource fs(nullptr); + + const auto start = uv_hrtime(); + + const Resource resource{ Resource::Unknown, "http://127.0.0.1:3001/test" }; + Request* req = fs.request(resource, uv_default_loop(), [&](const Response& res) { + static int counter = 0; + const auto duration = double(uv_hrtime() - start) / 1e9; + if (counter == 0) { + EXPECT_GT(0.2, duration) << "Response came in too late"; + } else if (counter == 1) { + EXPECT_LT(0.39, duration) << "Preempted retry triggered too early"; + EXPECT_GT(0.6, duration) << "Preempted retry triggered too late"; + } else if (counter > 1) { + FAIL() << "Retried too often"; + } + ASSERT_NE(nullptr, res.error); + EXPECT_EQ(Response::Error::Reason::Connection, res.error->reason); +#ifdef MBGL_HTTP_NSURL + EXPECT_TRUE(res.error->message == + "The operation couldn’t be completed. (NSURLErrorDomain error -1004.)" || + res.error->message == "Could not connect to the server.") + << "Full message is: \"" << res.error->message << "\""; +#elif MBGL_HTTP_CURL + const std::string prefix { "Couldn't connect to server: " }; + EXPECT_STREQ(prefix.c_str(), res.error->message.substr(0, prefix.size()).c_str()) << "Full message is: \"" << res.error->message << "\""; +#else + FAIL(); +#endif + EXPECT_EQ(false, res.stale); + ASSERT_FALSE(res.data.get()); + EXPECT_EQ(0, res.expires); + EXPECT_EQ(0, res.modified); + EXPECT_EQ("", res.etag); + + if (counter++ == 1) { + fs.cancel(req); + HTTPNetworkStatusChangePreempt.finish(); + } + }); + + // After 400 milliseconds, we're going to trigger a NetworkStatus change. + uv::timer reachableTimer(uv_default_loop()); + reachableTimer.start(400, 0, [] () { + mbgl::NetworkStatus::Reachable(); + }); + + uv_run(uv_default_loop(), UV_RUN_DEFAULT); +} |