diff --git a/platform/darwin/http_request_nsurl.mm b/platform/darwin/http_request_nsurl.mm index 59f3c6b71..10f1d0450 100644 --- a/platform/darwin/http_request_nsurl.mm +++ b/platform/darwin/http_request_nsurl.mm @@ -125,7 +125,6 @@ void HTTPNSURLRequest::start() { attempts++; @autoreleasepool { - NSURL *url = [NSURL URLWithString:@(resource.url.c_str())]; if (context->accountType == 0 && ([url.host isEqualToString:@"mapbox.com"] || [url.host hasSuffix:@".mapbox.com"])) { @@ -317,7 +316,7 @@ void HTTPNSURLRequest::retry(uint64_t timeout) { void HTTPNSURLRequest::retry() { // All batons get notified when the network status changed, but some of them // might not actually wait for the network to become available again. - if (strategy == PreemptImmediately) { + if (strategy == PreemptImmediately && !task) { // Triggers the timer upon the next event loop iteration. timer.stop(); timer.start(0, 0, [this] { start(); }); diff --git a/test/storage/http_retry_network_status.cpp b/test/storage/http_retry_network_status.cpp new file mode 100644 index 000000000..238108153 --- /dev/null +++ b/test/storage/http_retry_network_status.cpp @@ -0,0 +1,52 @@ +#include "storage.hpp" + +#include + +#include +#include + + +// Test for https://github.com/mapbox/mapbox-gl-native/issues/2123 +// +// A request is made. While the request is in progress, the network status changes. This should +// trigger an immediate retry of all requests that are not in progress. This test makes sure that +// we don't accidentally double-trigger the request. + +TEST_F(Storage, HTTPNetworkStatusChange) { + SCOPED_TEST(HTTPNetworkStatusChange) + + using namespace mbgl; + + DefaultFileSource fs(nullptr); + + const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/delayed" }; + + // This request takes 200 milliseconds to answer. + Request* req = fs.request(resource, uv_default_loop(), [&](const Response& res) { + fs.cancel(req); + EXPECT_EQ(Response::Successful, res.status); + EXPECT_EQ(false, res.stale); + ASSERT_TRUE(res.data.get()); + EXPECT_EQ("Response", *res.data); + EXPECT_EQ(0, res.expires); + EXPECT_EQ(0, res.modified); + EXPECT_EQ("", res.etag); + EXPECT_EQ("", res.message); + HTTPNetworkStatusChange.finish(); + }); + + // After 50 milliseconds, we're going to trigger a NetworkStatus change. + uv_timer_t reachableTimer; + uv_timer_init(uv_default_loop(), &reachableTimer); + uv_timer_start(&reachableTimer, [] (uv_timer_t*) { + mbgl::NetworkStatus::Reachable(); + }, 50, 0); + + // This timer will keep the loop alive to make sure we would be getting a response in caes the + // network status change triggered another change (which it shouldn't). + uv_timer_t delayTimer; + uv_timer_init(uv_default_loop(), &delayTimer); + uv_timer_start(&delayTimer, [] (uv_timer_t*) {}, 300, 0); + + uv_run(uv_default_loop(), UV_RUN_DEFAULT); +} diff --git a/test/test.gypi b/test/test.gypi index 3631c14ef..e8d9a9fcc 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -84,6 +84,7 @@ 'storage/http_issue_1369.cpp', 'storage/http_load.cpp', 'storage/http_other_loop.cpp', + 'storage/http_retry_network_status.cpp', 'storage/http_reading.cpp', 'storage/http_timeout.cpp',