OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/backoff_entry.h" | 5 #include "net/base/backoff_entry.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 | 7 |
8 namespace { | 8 namespace { |
9 | 9 |
10 using base::TimeDelta; | 10 using base::TimeDelta; |
11 using base::TimeTicks; | 11 using base::TimeTicks; |
12 using net::BackoffEntry; | 12 using net::BackoffEntry; |
13 | 13 |
14 BackoffEntry::Policy base_policy = { 0, 1000, 2.0, 0.0, 20000, 2000 }; | 14 BackoffEntry::Policy base_policy = { 0, 1000, 2.0, 0.0, 20000, 2000 }; |
15 | 15 |
16 class TestBackoffEntry : public BackoffEntry { | 16 class TestBackoffEntry : public BackoffEntry { |
17 public: | 17 public: |
18 explicit TestBackoffEntry(const Policy* const policy) | 18 explicit TestBackoffEntry(const Policy* const policy) |
19 : BackoffEntry(policy), | 19 : BackoffEntry(policy), |
20 now_(TimeTicks()) { | 20 now_(TimeTicks()) { |
21 // Work around initialization in constructor not picking up | 21 // Work around initialization in constructor not picking up |
22 // fake time. | 22 // fake time. |
23 SetCustomReleaseTime(TimeTicks()); | 23 SetCustomReleaseTime(TimeTicks()); |
24 } | 24 } |
25 | 25 |
26 virtual ~TestBackoffEntry() {} | 26 virtual ~TestBackoffEntry() {} |
27 | 27 |
28 virtual TimeTicks GetTimeNow() const { | 28 virtual TimeTicks ImplGetTimeNow() const OVERRIDE { |
29 return now_; | 29 return now_; |
30 } | 30 } |
31 | 31 |
32 void set_now(const TimeTicks& now) { | 32 void set_now(const TimeTicks& now) { |
33 now_ = now; | 33 now_ = now; |
34 } | 34 } |
35 | 35 |
36 private: | 36 private: |
37 TimeTicks now_; | 37 TimeTicks now_; |
38 | 38 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 EXPECT_FALSE(entry.ShouldRejectRequest()); | 101 EXPECT_FALSE(entry.ShouldRejectRequest()); |
102 entry.InformOfRequest(false); | 102 entry.InformOfRequest(false); |
103 EXPECT_TRUE(entry.ShouldRejectRequest()); | 103 EXPECT_TRUE(entry.ShouldRejectRequest()); |
104 } | 104 } |
105 | 105 |
106 TEST(BackoffEntryTest, ReleaseTimeCalculation) { | 106 TEST(BackoffEntryTest, ReleaseTimeCalculation) { |
107 TestBackoffEntry entry(&base_policy); | 107 TestBackoffEntry entry(&base_policy); |
108 | 108 |
109 // With zero errors, should return "now". | 109 // With zero errors, should return "now". |
110 TimeTicks result = entry.GetReleaseTime(); | 110 TimeTicks result = entry.GetReleaseTime(); |
111 EXPECT_EQ(entry.GetTimeNow(), result); | 111 EXPECT_EQ(entry.ImplGetTimeNow(), result); |
112 | 112 |
113 // 1 error. | 113 // 1 error. |
114 entry.InformOfRequest(false); | 114 entry.InformOfRequest(false); |
115 result = entry.GetReleaseTime(); | 115 result = entry.GetReleaseTime(); |
116 EXPECT_EQ(entry.GetTimeNow() + TimeDelta::FromMilliseconds(1000), result); | 116 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(1000), result); |
117 | 117 |
118 // 2 errors. | 118 // 2 errors. |
119 entry.InformOfRequest(false); | 119 entry.InformOfRequest(false); |
120 result = entry.GetReleaseTime(); | 120 result = entry.GetReleaseTime(); |
121 EXPECT_EQ(entry.GetTimeNow() + TimeDelta::FromMilliseconds(2000), result); | 121 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(2000), result); |
122 | 122 |
123 // 3 errors. | 123 // 3 errors. |
124 entry.InformOfRequest(false); | 124 entry.InformOfRequest(false); |
125 result = entry.GetReleaseTime(); | 125 result = entry.GetReleaseTime(); |
126 EXPECT_EQ(entry.GetTimeNow() + TimeDelta::FromMilliseconds(4000), result); | 126 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); |
127 | 127 |
128 // 6 errors (to check it doesn't pass maximum). | 128 // 6 errors (to check it doesn't pass maximum). |
129 entry.InformOfRequest(false); | 129 entry.InformOfRequest(false); |
130 entry.InformOfRequest(false); | 130 entry.InformOfRequest(false); |
131 entry.InformOfRequest(false); | 131 entry.InformOfRequest(false); |
132 result = entry.GetReleaseTime(); | 132 result = entry.GetReleaseTime(); |
133 EXPECT_EQ(entry.GetTimeNow() + TimeDelta::FromMilliseconds(20000), result); | 133 EXPECT_EQ( |
| 134 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(20000), result); |
134 } | 135 } |
135 | 136 |
136 TEST(BackoffEntryTest, ReleaseTimeCalculationWithJitter) { | 137 TEST(BackoffEntryTest, ReleaseTimeCalculationWithJitter) { |
137 for (int i = 0; i < 10; ++i) { | 138 for (int i = 0; i < 10; ++i) { |
138 BackoffEntry::Policy jittery_policy = base_policy; | 139 BackoffEntry::Policy jittery_policy = base_policy; |
139 jittery_policy.jitter_factor = 0.2; | 140 jittery_policy.jitter_factor = 0.2; |
140 | 141 |
141 TestBackoffEntry entry(&jittery_policy); | 142 TestBackoffEntry entry(&jittery_policy); |
142 | 143 |
143 entry.InformOfRequest(false); | 144 entry.InformOfRequest(false); |
144 entry.InformOfRequest(false); | 145 entry.InformOfRequest(false); |
145 entry.InformOfRequest(false); | 146 entry.InformOfRequest(false); |
146 TimeTicks result = entry.GetReleaseTime(); | 147 TimeTicks result = entry.GetReleaseTime(); |
147 EXPECT_LE(entry.GetTimeNow() + TimeDelta::FromMilliseconds(3200), result); | 148 EXPECT_LE( |
148 EXPECT_GE(entry.GetTimeNow() + TimeDelta::FromMilliseconds(4000), result); | 149 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(3200), result); |
| 150 EXPECT_GE( |
| 151 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); |
149 } | 152 } |
150 } | 153 } |
151 | 154 |
152 TEST(BackoffEntryTest, FailureThenSuccess) { | 155 TEST(BackoffEntryTest, FailureThenSuccess) { |
153 TestBackoffEntry entry(&base_policy); | 156 TestBackoffEntry entry(&base_policy); |
154 | 157 |
155 // Failure count 1, establishes horizon. | 158 // Failure count 1, establishes horizon. |
156 entry.InformOfRequest(false); | 159 entry.InformOfRequest(false); |
157 TimeTicks release_time = entry.GetReleaseTime(); | 160 TimeTicks release_time = entry.GetReleaseTime(); |
158 EXPECT_EQ(TimeTicks() + TimeDelta::FromMilliseconds(1000), release_time); | 161 EXPECT_EQ(TimeTicks() + TimeDelta::FromMilliseconds(1000), release_time); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 BackoffEntry::Policy lenient_policy = base_policy; | 197 BackoffEntry::Policy lenient_policy = base_policy; |
195 lenient_policy.num_errors_to_ignore = 1; | 198 lenient_policy.num_errors_to_ignore = 1; |
196 TestBackoffEntry custom(&lenient_policy); | 199 TestBackoffEntry custom(&lenient_policy); |
197 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); | 200 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); |
198 custom.SetCustomReleaseTime(custom_horizon); | 201 custom.SetCustomReleaseTime(custom_horizon); |
199 custom.InformOfRequest(false); // This must not reset the horizon. | 202 custom.InformOfRequest(false); // This must not reset the horizon. |
200 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); | 203 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); |
201 } | 204 } |
202 | 205 |
203 } // namespace | 206 } // namespace |
OLD | NEW |