本文整理汇总了C++中OnlineFileSource::request方法的典型用法代码示例。如果您正苦于以下问题:C++ OnlineFileSource::request方法的具体用法?C++ OnlineFileSource::request怎么用?C++ OnlineFileSource::request使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OnlineFileSource
的用法示例。
在下文中一共展示了OnlineFileSource::request方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Seconds
TEST(OnlineFileSource, TEST_REQUIRES_SERVER(RespectPriorExpires)) {
util::RunLoop loop;
OnlineFileSource fs;
// Very long expiration time, should never arrive.
Resource resource1{ Resource::Unknown, "http://127.0.0.1:3000/test" };
resource1.priorExpires = util::now() + Seconds(100000);
std::unique_ptr<AsyncRequest> req1 = fs.request(resource1, [&](Response) {
FAIL() << "Should never be called";
});
// No expiration time, should be requested immediately.
Resource resource2{ Resource::Unknown, "http://127.0.0.1:3000/test" };
std::unique_ptr<AsyncRequest> req2 = fs.request(resource2, [&](Response) {
loop.stop();
});
// Very long expiration time, should never arrive.
Resource resource3{ Resource::Unknown, "http://127.0.0.1:3000/test" };
resource3.priorExpires = util::now() + Seconds(100000);
std::unique_ptr<AsyncRequest> req3 = fs.request(resource3, [&](Response) {
FAIL() << "Should never be called";
});
loop.run();
}
示例2:
TEST(OnlineFileSource, TEST_REQUIRES_SERVER(ConnectionError)) {
util::RunLoop loop;
OnlineFileSource fs;
const auto start = Clock::now();
std::unique_ptr<AsyncRequest> req = fs.request({ Resource::Unknown, "http://127.0.0.1:3001/" }, [&](Response res) {
static int counter = 0;
static int wait = 0;
const auto duration = std::chrono::duration<const double>(Clock::now() - start).count();
EXPECT_LT(wait - 0.01, duration) << "Backoff timer didn't wait 1 second";
EXPECT_GT(wait + 0.2, duration) << "Backoff timer fired too late";
ASSERT_NE(nullptr, res.error);
EXPECT_EQ(Response::Error::Reason::Connection, res.error->reason);
ASSERT_FALSE(res.data.get());
EXPECT_FALSE(bool(res.expires));
EXPECT_FALSE(bool(res.modified));
EXPECT_FALSE(bool(res.etag));
if (counter == 2) {
req.reset();
loop.stop();
}
wait += (1 << counter);
counter++;
});
loop.run();
}
示例3: switch
TEST(OnlineFileSource, TEST_REQUIRES_SERVER(RetryOnClockSkew)) {
util::RunLoop loop;
OnlineFileSource fs;
int counter = 0;
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/clockskew" };
std::unique_ptr<AsyncRequest> req1 = fs.request(resource, [&](Response res) {
switch (counter++) {
case 0: {
EXPECT_EQ(nullptr, res.error);
EXPECT_GT(util::now(), res.expires);
} break;
case 1: {
EXPECT_EQ(nullptr, res.error);
auto now = util::now();
EXPECT_LT(now + Seconds(40), res.expires) << "Expiration not interpolated to 60s";
EXPECT_GT(now + Seconds(80), res.expires) << "Expiration not interpolated to 60s";
loop.stop();
} break;
}
});
loop.run();
}
示例4: bool
TEST(OnlineFileSource, TEST_REQUIRES_SERVER(RateLimitStandard)) {
util::RunLoop loop;
OnlineFileSource fs;
auto req = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/rate-limit?std=true" }, [&](Response res) {
ASSERT_NE(nullptr, res.error);
EXPECT_EQ(Response::Error::Reason::RateLimit, res.error->reason);
ASSERT_EQ(true, bool(res.error->retryAfter));
ASSERT_LT(util::now(), res.error->retryAfter);
loop.stop();
});
loop.run();
}
示例5: if
// Tests that a change in network status preempts requests that failed due to connection or
// reachability issues.
TEST(OnlineFileSource, TEST_REQUIRES_SERVER(NetworkStatusChangePreempt)) {
util::RunLoop loop;
OnlineFileSource fs;
const auto start = Clock::now();
const Resource resource{ Resource::Unknown, "http://127.0.0.1:3001/test" };
std::unique_ptr<AsyncRequest> req = fs.request(resource, [&](Response res) {
static int counter = 0;
const auto duration = std::chrono::duration<const double>(Clock::now() - start).count();
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);
ASSERT_FALSE(res.data.get());
EXPECT_FALSE(bool(res.expires));
EXPECT_FALSE(bool(res.modified));
EXPECT_FALSE(bool(res.etag));
if (counter++ == 1) {
req.reset();
loop.stop();
}
});
// After 400 milliseconds, we're going to trigger a NetworkStatus change.
util::Timer reachableTimer;
reachableTimer.start(Milliseconds(400), Duration::zero(), [] () {
mbgl::NetworkStatus::Reachable();
});
loop.run();
}
示例6: req
TEST(OnlineFileSource, TEST_REQUIRES_SERVER(Load)) {
util::RunLoop loop;
OnlineFileSource fs;
const int concurrency = 50;
const int max = 10000;
int number = 1;
std::unique_ptr<AsyncRequest> reqs[concurrency];
std::function<void(int)> req = [&](int i) {
const auto current = number++;
reqs[i] = fs.request({ Resource::Unknown,
std::string("http://127.0.0.1:3000/load/") + std::to_string(current) },
[&, i, current](Response res) {
reqs[i].reset();
EXPECT_EQ(nullptr, res.error);
ASSERT_TRUE(res.data.get());
EXPECT_EQ(std::string("Request ") + std::to_string(current), *res.data);
EXPECT_FALSE(bool(res.expires));
EXPECT_FALSE(bool(res.modified));
EXPECT_FALSE(bool(res.etag));
if (number <= max) {
req(i);
} else if (current == max) {
loop.stop();
}
});
};
for (int i = 0; i < concurrency; i++) {
req(i);
}
loop.run();
}
示例7: switch
std::unique_ptr<AsyncRequest> req1 = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/temporary-error" }, [&](Response res) {
static int counter = 0;
switch (counter++) {
case 0: {
const auto duration = std::chrono::duration<const double>(Clock::now() - start).count();
EXPECT_GT(0.2, duration) << "Initial error request took too long";
ASSERT_NE(nullptr, res.error);
EXPECT_EQ(Response::Error::Reason::Server, res.error->reason);
EXPECT_EQ("HTTP status code 500", res.error->message);
ASSERT_FALSE(bool(res.data));
EXPECT_FALSE(bool(res.expires));
EXPECT_FALSE(bool(res.modified));
EXPECT_FALSE(bool(res.etag));
} break;
case 1: {
req1.reset();
const auto duration = std::chrono::duration<const double>(Clock::now() - start).count();
EXPECT_LT(0.99, duration) << "Backoff timer didn't wait 1 second";
EXPECT_GT(1.2, duration) << "Backoff timer fired too late";
EXPECT_EQ(nullptr, res.error);
ASSERT_TRUE(res.data.get());
EXPECT_EQ("Hello World!", *res.data);
EXPECT_FALSE(bool(res.expires));
EXPECT_FALSE(bool(res.modified));
EXPECT_FALSE(bool(res.etag));
loop.stop();
HTTPTemporaryError.finish();
} break;
}
});
示例8: fs
TEST_F(Storage, HTTPExpiresParsing) {
SCOPED_TEST(HTTPExpiresTest)
using namespace mbgl;
util::RunLoop loop;
OnlineFileSource fs(nullptr);
std::unique_ptr<FileRequest> req1 = fs.request({ Resource::Unknown,
"http://127.0.0.1:3000/test?modified=1420794326&expires=1420797926&etag=foo" },
[&](Response res) {
req1.reset();
EXPECT_EQ(nullptr, res.error);
EXPECT_EQ(false, res.stale);
ASSERT_TRUE(res.data.get());
EXPECT_EQ("Hello World!", *res.data);
EXPECT_EQ(1420797926, res.expires.count());
EXPECT_EQ(1420794326, res.modified.count());
EXPECT_EQ("foo", res.etag);
loop.stop();
HTTPExpiresTest.finish();
});
loop.run();
}
TEST_F(Storage, HTTPCacheControlParsing) {
SCOPED_TEST(HTTPCacheControlTest)
using namespace mbgl;
示例9: req
int number = 1;
std::unique_ptr<FileRequest> reqs[concurrency];
std::function<void(int)> req = [&](int i) {
const auto current = number++;
reqs[i] = fs.request({ Resource::Unknown,
std::string("http://127.0.0.1:3000/load/") + std::to_string(current) },
[&, i, current](Response res) {
reqs[i].reset();
EXPECT_EQ(nullptr, res.error);
EXPECT_EQ(false, res.stale);
ASSERT_TRUE(res.data.get());
EXPECT_EQ(std::string("Request ") + std::to_string(current), *res.data);
EXPECT_EQ(Seconds::zero(), res.expires);
EXPECT_EQ(Seconds::zero(), res.modified);
EXPECT_EQ("", res.etag);
if (number <= max) {
req(i);
} else if (current == max) {
loop.stop();
HTTPLoad.finish();
}
});
};
for (int i = 0; i < concurrency; i++) {
req(i);
}
示例10: fs
#include <mbgl/storage/network_status.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
#include <cmath>
TEST_F(Storage, HTTPCancel) {
SCOPED_TEST(HTTPCancel)
using namespace mbgl;
util::RunLoop loop;
OnlineFileSource fs(nullptr);
auto req =
fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" },
[&](Response) { ADD_FAILURE() << "Callback should not be called"; });
req.reset();
HTTPCancel.finish();
loop.runOnce();
}
TEST_F(Storage, HTTPCancelMultiple) {
SCOPED_TEST(HTTPCancelMultiple)
using namespace mbgl;
util::RunLoop loop;
OnlineFileSource fs(nullptr);
示例11: fs
TEST_F(Storage, HTTPTest) {
SCOPED_TEST(HTTPTest)
using namespace mbgl;
util::RunLoop loop;
OnlineFileSource fs(nullptr);
std::unique_ptr<FileRequest> req1 = fs.request({ Resource::Unknown, "http://127.0.0.1:3000/test" },
[&](Response res) {
req1.reset();
EXPECT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Main));
EXPECT_EQ(nullptr, res.error);
ASSERT_TRUE(res.data.get());
EXPECT_EQ("Hello World!", *res.data);
EXPECT_FALSE(bool(res.expires));
EXPECT_FALSE(bool(res.modified));
EXPECT_FALSE(bool(res.etag));
loop.stop();
HTTPTest.finish();
});
loop.run();
}
TEST_F(Storage, HTTP404) {
SCOPED_TEST(HTTP404)
using namespace mbgl;
示例12: fs
SCOPED_TEST(HTTPNetworkStatusChange)
using namespace mbgl;
util::RunLoop loop;
OnlineFileSource fs(nullptr);
const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/delayed" };
// This request takes 200 milliseconds to answer.
std::unique_ptr<FileRequest> req = fs.request(resource, [&](Response res) {
req.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_FALSE(bool(res.etag));
loop.stop();
HTTPNetworkStatusChange.finish();
});
// After 50 milliseconds, we're going to trigger a NetworkStatus change.
util::Timer reachableTimer;
reachableTimer.start(Milliseconds(50), Duration::zero(), [] () {
mbgl::NetworkStatus::Reachable();
});
// 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).
util::Timer delayTimer;