diff options
Diffstat (limited to 'test/storage/http_coalescing.cpp')
-rw-r--r-- | test/storage/http_coalescing.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/test/storage/http_coalescing.cpp b/test/storage/http_coalescing.cpp index 9dfa855501..8558362034 100644 --- a/test/storage/http_coalescing.cpp +++ b/test/storage/http_coalescing.cpp @@ -50,3 +50,108 @@ TEST_F(Storage, HTTPCoalescing) { uv_run(uv_default_loop(), UV_RUN_DEFAULT); } + +TEST_F(Storage, HTTPMultiple) { + SCOPED_TEST(HTTPMultiple) + + using namespace mbgl; + + DefaultFileSource fs(nullptr); + + const Response *reference = nullptr; + + const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test?expires=2147483647" }; + Request* req1 = nullptr; + Request* req2 = nullptr; + req1 = fs.request(resource, uv_default_loop(), [&] (const Response &res) { + EXPECT_EQ(nullptr, reference); + reference = &res; + + // Do not cancel the request right away. + EXPECT_EQ(Response::Successful, res.status); + EXPECT_EQ("Hello World!", res.data); + EXPECT_EQ(2147483647, res.expires); + EXPECT_EQ(0, res.modified); + EXPECT_EQ("", res.etag); + EXPECT_EQ("", res.message); + + // Start a second request for the same resource after the first one has been completed. + req2 = fs.request(resource, uv_default_loop(), [&] (const Response &res2) { + // Make sure we get the same object ID as before. + EXPECT_EQ(reference, &res2); + + // Now cancel both requests after both have been notified. + fs.cancel(req1); + fs.cancel(req2); + + EXPECT_EQ(Response::Successful, res2.status); + EXPECT_EQ("Hello World!", res2.data); + EXPECT_EQ(2147483647, res2.expires); + EXPECT_EQ(0, res2.modified); + EXPECT_EQ("", res2.etag); + EXPECT_EQ("", res2.message); + + HTTPMultiple.finish(); + }); + }); + + uv_run(uv_default_loop(), UV_RUN_DEFAULT); +} + +// Tests that we get stale responses from previous requests when requesting the same thing again. +TEST_F(Storage, HTTPStale) { + SCOPED_TEST(HTTPStale) + + using namespace mbgl; + + DefaultFileSource fs(nullptr); + + int updates = 0; + int stale = 0; + + const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test" }; + Request* req1 = nullptr; + Request* req2 = nullptr; + req1 = fs.request(resource, uv_default_loop(), [&] (const Response &res) { + // Do not cancel the request right away. + EXPECT_EQ(Response::Successful, res.status); + EXPECT_EQ("Hello World!", res.data); + EXPECT_EQ(false, res.stale); + EXPECT_EQ(0, res.expires); + EXPECT_EQ(0, res.modified); + EXPECT_EQ("", res.etag); + EXPECT_EQ("", res.message); + + // Don't start the request twice in case this callback gets fired multiple times. + if (req2) { + return; + } + + updates++; + + // Start a second request for the same resource after the first one has been completed. + req2 = fs.request(resource, uv_default_loop(), [&] (const Response &res2) { + EXPECT_EQ(Response::Successful, res2.status); + EXPECT_EQ("Hello World!", res2.data); + EXPECT_EQ(0, res2.expires); + EXPECT_EQ(0, res2.modified); + EXPECT_EQ("", res2.etag); + EXPECT_EQ("", res2.message); + + if (res2.stale) { + EXPECT_EQ(0, stale); + stale++; + } else { + // Now cancel both requests after both have been notified. + fs.cancel(req1); + fs.cancel(req2); + HTTPStale.finish(); + } + }); + }); + + uv_run(uv_default_loop(), UV_RUN_DEFAULT); + + EXPECT_EQ(1, stale); + EXPECT_EQ(1, updates); +} |