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 "net/url_request/url_request_backoff_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/time/time.h"
10 #include "net/http/http_response_headers.h"
11 #include "net/http/http_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 class URLRequestBackoffManagerTest
: public testing::Test
{
20 URLRequestBackoffManagerTest() : manager_(new URLRequestBackoffManager
) {}
21 void RegisterURL(const GURL
& url
,
23 const base::Time
& request_time
) {
24 std::string raw_headers
= base::StringPrintf(
28 scoped_refptr
<HttpResponseHeaders
> headers(new HttpResponseHeaders(
29 HttpUtil::AssembleRawHeaders(raw_headers
.c_str(), raw_headers
.size())));
30 manager_
->UpdateWithResponse(url
, headers
.get(), request_time
);
33 scoped_ptr
<URLRequestBackoffManager
> manager_
;
37 TEST_F(URLRequestBackoffManagerTest
, ShouldRejectRequest
) {
38 base::Time request_time
= base::Time::Now();
39 RegisterURL(GURL("https://example.com"), 3600, request_time
);
40 ASSERT_EQ(1, manager_
->GetNumberOfEntriesForTests());
41 ASSERT_TRUE(manager_
->ShouldRejectRequest(
42 GURL("https://example.com?q=v"),
43 request_time
+ base::TimeDelta::FromSeconds(3500)));
45 // Only can try once in the interval of
46 // [|request_time| + 3600, |request_time| + 3600 * 1.1).
47 ASSERT_FALSE(manager_
->ShouldRejectRequest(
48 GURL("https://example.com?q=v"),
49 request_time
+ base::TimeDelta::FromSeconds(3700)));
50 ASSERT_TRUE(manager_
->ShouldRejectRequest(
51 GURL("https://example.com?q=v"),
52 request_time
+ base::TimeDelta::FromSeconds(3700)));
53 ASSERT_EQ(1, manager_
->GetNumberOfEntriesForTests());
55 // After release time, throttling should not be applied.
56 ASSERT_FALSE(manager_
->ShouldRejectRequest(
57 GURL("https://example.com?q=v"),
58 request_time
+ base::TimeDelta::FromSeconds(3960)));
61 TEST_F(URLRequestBackoffManagerTest
, MisconfiguredHeaders
) {
62 // Backoff time is smaller than the minimum allowed.
63 RegisterURL(GURL("https://example.com"),
64 URLRequestBackoffManager::kMinimumBackoffInSeconds
- 1,
66 ASSERT_EQ(0, manager_
->GetNumberOfEntriesForTests());
68 // Backoff time is bigger than the maximum allowed.
69 RegisterURL(GURL("https://example.com"),
70 URLRequestBackoffManager::kMaximumBackoffInSeconds
+ 1,
72 ASSERT_EQ(0, manager_
->GetNumberOfEntriesForTests());
75 TEST_F(URLRequestBackoffManagerTest
, ShouldGarbageCollect
) {
76 base::Time request_time
=
77 base::Time::Now() - base::TimeDelta::FromSeconds(60);
79 i
< URLRequestBackoffManager::kNewEntriesBetweenCollecting
- 1; i
++) {
80 RegisterURL(GURL(base::StringPrintf("http://example%d.com", i
)), 10,
82 ASSERT_EQ(i
+ 1, manager_
->GetNumberOfEntriesForTests());
84 // Should clear all previous outdated entries.
85 RegisterURL(GURL("http://example.com"), 10, base::Time::Now());
86 ASSERT_EQ(1, manager_
->GetNumberOfEntriesForTests());
89 TEST_F(URLRequestBackoffManagerTest
, ClearOnNetworkChange
) {
90 for (int i
= 0; i
< 3; ++i
) {
91 RegisterURL(GURL("http://www.example.com/"), 60, base::Time::Now());
92 ASSERT_EQ(1, manager_
->GetNumberOfEntriesForTests());
93 EXPECT_TRUE(manager_
->ShouldRejectRequest(GURL("http://www.example.com/"),
97 manager_
->OnIPAddressChanged();
100 manager_
->OnConnectionTypeChanged(
101 NetworkChangeNotifier::CONNECTION_UNKNOWN
);
104 manager_
->OnConnectionTypeChanged(
105 NetworkChangeNotifier::CONNECTION_NONE
);
111 EXPECT_FALSE(manager_
->ShouldRejectRequest(GURL("http://www.example.com/"),
113 ASSERT_EQ(0, manager_
->GetNumberOfEntriesForTests());