Reland of Skip the Service Worker CORS fallback for same origin requests. [2/2 chromi...
[chromium-blink-merge.git] / google_apis / gcm / engine / gcm_request_test_base.cc
blob5337f7e9819836f2979652a0926838a6ffb32c13
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <cmath>
7 #include "google_apis/gcm/engine/gcm_request_test_base.h"
8 #include "net/url_request/url_fetcher_delegate.h"
10 namespace {
12 // Backoff policy for testing registration request.
13 const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
14 // Number of initial errors (in sequence) to ignore before applying
15 // exponential back-off rules.
18 // Initial delay for exponential back-off in ms.
19 15000, // 15 seconds.
21 // Factor by which the waiting time will be multiplied.
24 // Fuzzing percentage. ex: 10% will spread requests randomly
25 // between 90%-100% of the calculated time.
26 0.5, // 50%.
28 // Maximum amount of time we are willing to delay our request in ms.
29 1000 * 60 * 5, // 5 minutes.
31 // Time to keep an entry from being discarded even when it
32 // has no significant state, -1 to never discard.
33 -1,
35 // Don't use initial delay unless the last request was an error.
36 false,
39 } // namespace
41 namespace gcm {
43 GCMRequestTestBase::GCMRequestTestBase()
44 : task_runner_(new base::TestMockTimeTaskRunner),
45 task_runner_handle_(task_runner_),
46 url_request_context_getter_(new net::TestURLRequestContextGetter(
47 task_runner_)),
48 retry_count_(0) {
51 GCMRequestTestBase::~GCMRequestTestBase() {
54 const net::BackoffEntry::Policy& GCMRequestTestBase::GetBackoffPolicy() const {
55 return kDefaultBackoffPolicy;
58 net::TestURLFetcher* GCMRequestTestBase::GetFetcher() const {
59 return url_fetcher_factory_.GetFetcherByID(0);
62 void GCMRequestTestBase::SetResponse(net::HttpStatusCode status_code,
63 const std::string& response_body) {
64 if (retry_count_++)
65 FastForwardToTriggerNextRetry();
67 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
68 ASSERT_TRUE(fetcher);
69 fetcher->set_response_code(status_code);
70 fetcher->SetResponseString(response_body);
73 void GCMRequestTestBase::CompleteFetch() {
74 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
75 ASSERT_TRUE(fetcher);
76 fetcher->delegate()->OnURLFetchComplete(fetcher);
79 void GCMRequestTestBase::FastForwardToTriggerNextRetry() {
80 // Here we compute the maximum delay time by skipping the jitter fluctuation
81 // that only affects in the negative way.
82 int next_retry_delay_ms = kDefaultBackoffPolicy.initial_delay_ms;
83 next_retry_delay_ms *=
84 pow(kDefaultBackoffPolicy.multiply_factor, retry_count_);
85 task_runner_->FastForwardBy(
86 base::TimeDelta::FromMilliseconds(next_retry_delay_ms));
89 } // namespace gcm