summaryrefslogtreecommitdiff
path: root/test/storage/http_error.cpp
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-01-21 19:30:58 +0100
committerKonstantin Käfer <mail@kkaefer.com>2015-02-04 10:49:05 +0100
commit8a1fce547e9ad0bf750418c844c9b23a3ee6d8dd (patch)
treee3f0e8f4b16071667c6a4fdf706740335500dbc6 /test/storage/http_error.cpp
parent5503aef6907b1fea74d6bdbe696f02b9f016f752 (diff)
downloadqtlocation-mapboxgl-8a1fce547e9ad0bf750418c844c9b23a3ee6d8dd.tar.gz
rearrange tests and add storage tests
Diffstat (limited to 'test/storage/http_error.cpp')
-rw-r--r--test/storage/http_error.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/test/storage/http_error.cpp b/test/storage/http_error.cpp
new file mode 100644
index 0000000000..3f4b95a102
--- /dev/null
+++ b/test/storage/http_error.cpp
@@ -0,0 +1,63 @@
+#include "../util.hpp"
+
+#include <uv.h>
+
+#include <mbgl/storage/default/default_file_source.hpp>
+#include <mbgl/storage/network_status.hpp>
+
+#include <cmath>
+
+TEST(Storage, HTTPError) {
+ SCOPED_TEST(HTTPTemporaryError)
+ SCOPED_TEST(HTTPConnectionError)
+
+ using namespace mbgl;
+
+ uv_timer_t statusChange;
+ uv_timer_init(uv_default_loop(), &statusChange);
+ uv_timer_start(&statusChange, [](uv_timer_t *, int) {
+ NetworkStatus::Reachable();
+ }, 500, 500);
+ uv_unref((uv_handle_t *)&statusChange);
+
+ DefaultFileSource fs(nullptr, uv_default_loop());
+
+ auto start = uv_hrtime();
+
+ fs.request({ Resource::Unknown, "http://127.0.0.1:3000/temporary-error" }, uv_default_loop(), [&](const Response &res) {
+ const auto duration = double(uv_hrtime() - start) / 1e9;
+ EXPECT_GT(duration, 1) << "Backoff timer didn't wait 1 second";
+ EXPECT_LT(duration, 1.2) << "Backoff timer fired too late";
+ EXPECT_EQ(res.status, Response::Successful);
+ EXPECT_EQ(res.data, "Hello World!");
+ EXPECT_EQ(res.expires, 0);
+ EXPECT_EQ(res.modified, 0);
+ EXPECT_EQ(res.etag, "");
+ EXPECT_EQ(res.message, "");
+
+ HTTPTemporaryError.finish();
+ });
+
+ fs.request({ Resource::Unknown, "http://127.0.0.1:3001/" }, uv_default_loop(), [&](const Response &res) {
+ const auto duration = double(uv_hrtime() - start) / 1e9;
+ // 1.5 seconds == 4 retries, with a 500ms timeout (see above).
+ EXPECT_GT(duration, 1.5) << "Resource wasn't retried the correct number of times";
+ EXPECT_LT(duration, 1.7) << "Resource wasn't retried the correct number of times";
+ EXPECT_EQ(res.status, Response::Error);
+ EXPECT_TRUE(res.message == "Couldn't connect to server" || res.message == "The operation couldn’t be completed. (NSURLErrorDomain error -1004.)");
+ EXPECT_EQ(res.data, "");
+ EXPECT_EQ(res.expires, 0);
+ EXPECT_EQ(res.modified, 0);
+ EXPECT_EQ(res.etag, "");
+ HTTPConnectionError.finish();
+ });
+
+
+ uv_run(uv_default_loop(), UV_RUN_DEFAULT);
+
+ uv_timer_stop(&statusChange);
+ uv_close(reinterpret_cast<uv_handle_t *>(&statusChange), nullptr);
+
+ // Run again so that the timer handle can be properly closed.
+ uv_run(uv_default_loop(), UV_RUN_DEFAULT);
+}