diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-01-26 12:19:04 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-02-10 15:40:20 -0800 |
commit | cdedb66387065680efd318dacb61572c920b81b1 (patch) | |
tree | 3cc2676e15fac4c7b024cd17603c46e341f5a589 /test/storage/default_file_source.cpp | |
parent | 025375ad0b365a06e0742b92fecc9bc538b5a6e0 (diff) | |
download | qtlocation-mapboxgl-cdedb66387065680efd318dacb61572c920b81b1.tar.gz |
[core] Reimplement existing caching within an offline-capable database schema
Diffstat (limited to 'test/storage/default_file_source.cpp')
-rw-r--r-- | test/storage/default_file_source.cpp | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/test/storage/default_file_source.cpp b/test/storage/default_file_source.cpp new file mode 100644 index 0000000000..26fb164d3f --- /dev/null +++ b/test/storage/default_file_source.cpp @@ -0,0 +1,196 @@ +#include "storage.hpp" + +#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/util/run_loop.hpp> + +class DefaultFileSourceTest : public Storage {}; + +TEST_F(DefaultFileSourceTest, CacheResponse) { + SCOPED_TEST(CacheResponse); + + using namespace mbgl; + + util::RunLoop loop; + DefaultFileSource fs(":memory:", "."); + + const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/cache" }; + Response response; + + std::unique_ptr<FileRequest> req1; + std::unique_ptr<FileRequest> req2; + + req1 = fs.request(resource, [&](Response res) { + req1.reset(); + EXPECT_EQ(nullptr, res.error); + ASSERT_TRUE(res.data.get()); + EXPECT_EQ("Response 1", *res.data); + EXPECT_TRUE(bool(res.expires)); + EXPECT_FALSE(bool(res.modified)); + EXPECT_FALSE(bool(res.etag)); + response = res; + + // Now test that we get the same values as in the previous request. If we'd go to the server + // again, we'd get different values. + req2 = fs.request(resource, [&](Response res2) { + req2.reset(); + EXPECT_EQ(response.error, res2.error); + ASSERT_TRUE(res2.data.get()); + EXPECT_EQ(*response.data, *res2.data); + EXPECT_EQ(response.expires, res2.expires); + EXPECT_EQ(response.modified, res2.modified); + EXPECT_EQ(response.etag, res2.etag); + + loop.stop(); + CacheResponse.finish(); + }); + }); + + loop.run(); +} + +TEST_F(DefaultFileSourceTest, CacheRevalidateSame) { + SCOPED_TEST(CacheRevalidateSame) + + using namespace mbgl; + + util::RunLoop loop; + DefaultFileSource fs(":memory:", "."); + + const Resource revalidateSame { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" }; + std::unique_ptr<FileRequest> req1; + std::unique_ptr<FileRequest> req2; + uint16_t counter = 0; + + // First request causes the response to get cached. + req1 = fs.request(revalidateSame, [&](Response res) { + req1.reset(); + + EXPECT_EQ(nullptr, res.error); + ASSERT_TRUE(res.data.get()); + EXPECT_EQ("Response", *res.data); + EXPECT_FALSE(bool(res.expires)); + EXPECT_FALSE(bool(res.modified)); + EXPECT_EQ("snowfall", *res.etag); + + // Second request returns the cached response, then immediately revalidates. + req2 = fs.request(revalidateSame, [&, res](Response res2) { + if (counter == 0) { + ++counter; + EXPECT_FALSE(res2.notModified); + } else { + req2.reset(); + + EXPECT_EQ(nullptr, res2.error); + EXPECT_TRUE(res2.notModified); + ASSERT_FALSE(res2.data.get()); + EXPECT_TRUE(bool(res2.expires)); + EXPECT_FALSE(bool(res2.modified)); + // We're not sending the ETag in the 304 reply, but it should still be there. + EXPECT_EQ("snowfall", *res2.etag); + + loop.stop(); + CacheRevalidateSame.finish(); + } + }); + }); + + loop.run(); +} + +TEST_F(DefaultFileSourceTest, CacheRevalidateModified) { + SCOPED_TEST(CacheRevalidateModified) + + using namespace mbgl; + + util::RunLoop loop; + DefaultFileSource fs(":memory:", "."); + + const Resource revalidateModified{ Resource::Unknown, + "http://127.0.0.1:3000/revalidate-modified" }; + std::unique_ptr<FileRequest> req1; + std::unique_ptr<FileRequest> req2; + uint16_t counter = 0; + + // First request causes the response to get cached. + req1 = fs.request(revalidateModified, [&](Response res) { + req1.reset(); + + EXPECT_EQ(nullptr, res.error); + ASSERT_TRUE(res.data.get()); + EXPECT_EQ("Response", *res.data); + EXPECT_FALSE(bool(res.expires)); + EXPECT_EQ(SystemClock::from_time_t(1420070400), *res.modified); + EXPECT_FALSE(res.etag); + + // Second request returns the cached response, then immediately revalidates. + req2 = fs.request(revalidateModified, [&, res](Response res2) { + if (counter == 0) { + ++counter; + EXPECT_FALSE(res2.notModified); + } else { + req2.reset(); + + EXPECT_EQ(nullptr, res2.error); + EXPECT_TRUE(res2.notModified); + ASSERT_FALSE(res2.data.get()); + EXPECT_TRUE(bool(res2.expires)); + EXPECT_EQ(SystemClock::from_time_t(1420070400), *res2.modified); + EXPECT_FALSE(res2.etag); + + loop.stop(); + CacheRevalidateModified.finish(); + } + }); + }); + + loop.run(); +} + +TEST_F(DefaultFileSourceTest, CacheRevalidateEtag) { + SCOPED_TEST(CacheRevalidateEtag) + + using namespace mbgl; + + util::RunLoop loop; + DefaultFileSource fs(":memory:", "."); + + const Resource revalidateEtag { Resource::Unknown, "http://127.0.0.1:3000/revalidate-etag" }; + std::unique_ptr<FileRequest> req1; + std::unique_ptr<FileRequest> req2; + uint16_t counter = 0; + + // First request causes the response to get cached. + req1 = fs.request(revalidateEtag, [&](Response res) { + req1.reset(); + + EXPECT_EQ(nullptr, res.error); + ASSERT_TRUE(res.data.get()); + EXPECT_EQ("Response 1", *res.data); + EXPECT_FALSE(bool(res.expires)); + EXPECT_FALSE(bool(res.modified)); + EXPECT_EQ("response-1", *res.etag); + + // Second request returns the cached response, then immediately revalidates. + req2 = fs.request(revalidateEtag, [&, res](Response res2) { + if (counter == 0) { + ++counter; + EXPECT_FALSE(res2.notModified); + } else { + req2.reset(); + + EXPECT_EQ(nullptr, res2.error); + ASSERT_TRUE(res2.data.get()); + EXPECT_NE(res.data, res2.data); + EXPECT_EQ("Response 2", *res2.data); + EXPECT_FALSE(bool(res2.expires)); + EXPECT_FALSE(bool(res2.modified)); + EXPECT_EQ("response-2", *res2.etag); + + loop.stop(); + CacheRevalidateEtag.finish(); + } + }); + }); + + loop.run(); +} |