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.
7 #include "google_apis/gcm/engine/gcm_request_test_base.h"
8 #include "net/url_request/url_fetcher_delegate.h"
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.
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.
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.
35 // Don't use initial delay unless the last request was an error.
43 GCMRequestTestBase::GCMRequestTestBase()
44 : task_runner_(new base::TestMockTimeTaskRunner
),
45 task_runner_handle_(task_runner_
),
46 url_request_context_getter_(new net::TestURLRequestContextGetter(
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
) {
65 FastForwardToTriggerNextRetry();
67 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
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);
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
));