| 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, false }; |
| 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 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 TimeTicks now_; | 37 TimeTicks now_; |
| 38 | 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(TestBackoffEntry); | 39 DISALLOW_COPY_AND_ASSIGN(TestBackoffEntry); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 TEST(BackoffEntryTest, BaseTest) { | 42 TEST(BackoffEntryTest, BaseTest) { |
| 43 TestBackoffEntry entry(&base_policy); | 43 TestBackoffEntry entry(&base_policy); |
| 44 EXPECT_FALSE(entry.ShouldRejectRequest()); | 44 EXPECT_FALSE(entry.ShouldRejectRequest()); |
| 45 EXPECT_EQ(TimeDelta(), entry.GetTimeUntilRelease()); |
| 45 | 46 |
| 46 entry.InformOfRequest(false); | 47 entry.InformOfRequest(false); |
| 47 EXPECT_TRUE(entry.ShouldRejectRequest()); | 48 EXPECT_TRUE(entry.ShouldRejectRequest()); |
| 49 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
| 48 } | 50 } |
| 49 | 51 |
| 50 TEST(BackoffEntryTest, CanDiscardNeverExpires) { | 52 TEST(BackoffEntryTest, CanDiscardNeverExpires) { |
| 51 BackoffEntry::Policy never_expires_policy = base_policy; | 53 BackoffEntry::Policy never_expires_policy = base_policy; |
| 52 never_expires_policy.entry_lifetime_ms = -1; | 54 never_expires_policy.entry_lifetime_ms = -1; |
| 53 TestBackoffEntry never_expires(&never_expires_policy); | 55 TestBackoffEntry never_expires(&never_expires_policy); |
| 54 EXPECT_FALSE(never_expires.CanDiscard()); | 56 EXPECT_FALSE(never_expires.CanDiscard()); |
| 55 never_expires.set_now(TimeTicks() + TimeDelta::FromDays(100)); | 57 never_expires.set_now(TimeTicks() + TimeDelta::FromDays(100)); |
| 56 EXPECT_FALSE(never_expires.CanDiscard()); | 58 EXPECT_FALSE(never_expires.CanDiscard()); |
| 57 } | 59 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 76 // Test the final case (no errors, dependent only on specified lifetime). | 78 // Test the final case (no errors, dependent only on specified lifetime). |
| 77 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( | 79 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( |
| 78 base_policy.entry_lifetime_ms - 1)); | 80 base_policy.entry_lifetime_ms - 1)); |
| 79 entry.InformOfRequest(true); | 81 entry.InformOfRequest(true); |
| 80 EXPECT_FALSE(entry.CanDiscard()); | 82 EXPECT_FALSE(entry.CanDiscard()); |
| 81 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( | 83 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( |
| 82 base_policy.entry_lifetime_ms)); | 84 base_policy.entry_lifetime_ms)); |
| 83 EXPECT_TRUE(entry.CanDiscard()); | 85 EXPECT_TRUE(entry.CanDiscard()); |
| 84 } | 86 } |
| 85 | 87 |
| 88 TEST(BackoffEntryTest, CanDiscardAlwaysDelay) { |
| 89 BackoffEntry::Policy always_delay_policy = base_policy; |
| 90 always_delay_policy.always_use_initial_delay = true; |
| 91 always_delay_policy.entry_lifetime_ms = 0; |
| 92 |
| 93 TestBackoffEntry entry(&always_delay_policy); |
| 94 |
| 95 // Because lifetime is non-zero, we shouldn't be able to discard yet. |
| 96 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); |
| 97 EXPECT_TRUE(entry.CanDiscard()); |
| 98 |
| 99 // Even with no failures, we wait until the delay before we allow discard. |
| 100 entry.InformOfRequest(true); |
| 101 EXPECT_FALSE(entry.CanDiscard()); |
| 102 |
| 103 // Wait until the delay expires, and we can discard the entry again. |
| 104 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(1000)); |
| 105 EXPECT_TRUE(entry.CanDiscard()); |
| 106 } |
| 107 |
| 86 TEST(BackoffEntryTest, CanDiscardNotStored) { | 108 TEST(BackoffEntryTest, CanDiscardNotStored) { |
| 87 BackoffEntry::Policy no_store_policy = base_policy; | 109 BackoffEntry::Policy no_store_policy = base_policy; |
| 88 no_store_policy.entry_lifetime_ms = 0; | 110 no_store_policy.entry_lifetime_ms = 0; |
| 89 TestBackoffEntry not_stored(&no_store_policy); | 111 TestBackoffEntry not_stored(&no_store_policy); |
| 90 EXPECT_TRUE(not_stored.CanDiscard()); | 112 EXPECT_TRUE(not_stored.CanDiscard()); |
| 91 } | 113 } |
| 92 | 114 |
| 93 TEST(BackoffEntryTest, ShouldIgnoreFirstTwo) { | 115 TEST(BackoffEntryTest, ShouldIgnoreFirstTwo) { |
| 94 BackoffEntry::Policy lenient_policy = base_policy; | 116 BackoffEntry::Policy lenient_policy = base_policy; |
| 95 lenient_policy.num_errors_to_ignore = 2; | 117 lenient_policy.num_errors_to_ignore = 2; |
| 96 | 118 |
| 97 BackoffEntry entry(&lenient_policy); | 119 BackoffEntry entry(&lenient_policy); |
| 120 |
| 98 entry.InformOfRequest(false); | 121 entry.InformOfRequest(false); |
| 99 EXPECT_FALSE(entry.ShouldRejectRequest()); | 122 EXPECT_FALSE(entry.ShouldRejectRequest()); |
| 123 |
| 100 entry.InformOfRequest(false); | 124 entry.InformOfRequest(false); |
| 101 EXPECT_FALSE(entry.ShouldRejectRequest()); | 125 EXPECT_FALSE(entry.ShouldRejectRequest()); |
| 126 |
| 102 entry.InformOfRequest(false); | 127 entry.InformOfRequest(false); |
| 103 EXPECT_TRUE(entry.ShouldRejectRequest()); | 128 EXPECT_TRUE(entry.ShouldRejectRequest()); |
| 104 } | 129 } |
| 105 | 130 |
| 106 TEST(BackoffEntryTest, ReleaseTimeCalculation) { | 131 TEST(BackoffEntryTest, ReleaseTimeCalculation) { |
| 107 TestBackoffEntry entry(&base_policy); | 132 TestBackoffEntry entry(&base_policy); |
| 108 | 133 |
| 109 // With zero errors, should return "now". | 134 // With zero errors, should return "now". |
| 110 TimeTicks result = entry.GetReleaseTime(); | 135 TimeTicks result = entry.GetReleaseTime(); |
| 111 EXPECT_EQ(entry.ImplGetTimeNow(), result); | 136 EXPECT_EQ(entry.ImplGetTimeNow(), result); |
| 112 | 137 |
| 113 // 1 error. | 138 // 1 error. |
| 114 entry.InformOfRequest(false); | 139 entry.InformOfRequest(false); |
| 115 result = entry.GetReleaseTime(); | 140 result = entry.GetReleaseTime(); |
| 116 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(1000), result); | 141 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(1000), result); |
| 142 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
| 117 | 143 |
| 118 // 2 errors. | 144 // 2 errors. |
| 119 entry.InformOfRequest(false); | 145 entry.InformOfRequest(false); |
| 120 result = entry.GetReleaseTime(); | 146 result = entry.GetReleaseTime(); |
| 121 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(2000), result); | 147 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(2000), result); |
| 148 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); |
| 122 | 149 |
| 123 // 3 errors. | 150 // 3 errors. |
| 124 entry.InformOfRequest(false); | 151 entry.InformOfRequest(false); |
| 125 result = entry.GetReleaseTime(); | 152 result = entry.GetReleaseTime(); |
| 126 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); | 153 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); |
| 154 EXPECT_EQ(TimeDelta::FromMilliseconds(4000), entry.GetTimeUntilRelease()); |
| 127 | 155 |
| 128 // 6 errors (to check it doesn't pass maximum). | 156 // 6 errors (to check it doesn't pass maximum). |
| 129 entry.InformOfRequest(false); | 157 entry.InformOfRequest(false); |
| 130 entry.InformOfRequest(false); | 158 entry.InformOfRequest(false); |
| 131 entry.InformOfRequest(false); | 159 entry.InformOfRequest(false); |
| 132 result = entry.GetReleaseTime(); | 160 result = entry.GetReleaseTime(); |
| 133 EXPECT_EQ( | 161 EXPECT_EQ( |
| 134 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(20000), result); | 162 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(20000), result); |
| 135 } | 163 } |
| 136 | 164 |
| 165 TEST(BackoffEntryTest, ReleaseTimeCalculationAlwaysDelay) { |
| 166 BackoffEntry::Policy always_delay_policy = base_policy; |
| 167 always_delay_policy.always_use_initial_delay = true; |
| 168 always_delay_policy.num_errors_to_ignore = 2; |
| 169 |
| 170 TestBackoffEntry entry(&always_delay_policy); |
| 171 |
| 172 // With previous requests, should return "now". |
| 173 TimeTicks result = entry.GetReleaseTime(); |
| 174 EXPECT_EQ(TimeDelta(), entry.GetTimeUntilRelease()); |
| 175 |
| 176 // 1 error. |
| 177 entry.InformOfRequest(false); |
| 178 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
| 179 |
| 180 // 2 errors. |
| 181 entry.InformOfRequest(false); |
| 182 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
| 183 |
| 184 // 3 errors, exponential backoff starts. |
| 185 entry.InformOfRequest(false); |
| 186 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); |
| 187 |
| 188 // 4 errors. |
| 189 entry.InformOfRequest(false); |
| 190 EXPECT_EQ(TimeDelta::FromMilliseconds(4000), entry.GetTimeUntilRelease()); |
| 191 |
| 192 // 8 errors (to check it doesn't pass maximum). |
| 193 entry.InformOfRequest(false); |
| 194 entry.InformOfRequest(false); |
| 195 entry.InformOfRequest(false); |
| 196 entry.InformOfRequest(false); |
| 197 result = entry.GetReleaseTime(); |
| 198 EXPECT_EQ(TimeDelta::FromMilliseconds(20000), entry.GetTimeUntilRelease()); |
| 199 } |
| 200 |
| 137 TEST(BackoffEntryTest, ReleaseTimeCalculationWithJitter) { | 201 TEST(BackoffEntryTest, ReleaseTimeCalculationWithJitter) { |
| 138 for (int i = 0; i < 10; ++i) { | 202 for (int i = 0; i < 10; ++i) { |
| 139 BackoffEntry::Policy jittery_policy = base_policy; | 203 BackoffEntry::Policy jittery_policy = base_policy; |
| 140 jittery_policy.jitter_factor = 0.2; | 204 jittery_policy.jitter_factor = 0.2; |
| 141 | 205 |
| 142 TestBackoffEntry entry(&jittery_policy); | 206 TestBackoffEntry entry(&jittery_policy); |
| 143 | 207 |
| 144 entry.InformOfRequest(false); | 208 entry.InformOfRequest(false); |
| 145 entry.InformOfRequest(false); | 209 entry.InformOfRequest(false); |
| 146 entry.InformOfRequest(false); | 210 entry.InformOfRequest(false); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 165 entry.set_now(release_time - TimeDelta::FromMilliseconds(200)); | 229 entry.set_now(release_time - TimeDelta::FromMilliseconds(200)); |
| 166 entry.InformOfRequest(true); | 230 entry.InformOfRequest(true); |
| 167 EXPECT_EQ(release_time, entry.GetReleaseTime()); | 231 EXPECT_EQ(release_time, entry.GetReleaseTime()); |
| 168 | 232 |
| 169 // Failure, failure count 1. | 233 // Failure, failure count 1. |
| 170 entry.InformOfRequest(false); | 234 entry.InformOfRequest(false); |
| 171 EXPECT_EQ(release_time + TimeDelta::FromMilliseconds(800), | 235 EXPECT_EQ(release_time + TimeDelta::FromMilliseconds(800), |
| 172 entry.GetReleaseTime()); | 236 entry.GetReleaseTime()); |
| 173 } | 237 } |
| 174 | 238 |
| 239 TEST(BackoffEntryTest, FailureThenSuccessAlwaysDelay) { |
| 240 BackoffEntry::Policy always_delay_policy = base_policy; |
| 241 always_delay_policy.always_use_initial_delay = true; |
| 242 always_delay_policy.num_errors_to_ignore = 1; |
| 243 |
| 244 TestBackoffEntry entry(&always_delay_policy); |
| 245 |
| 246 // Failure count 1. |
| 247 entry.InformOfRequest(false); |
| 248 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
| 249 |
| 250 // Failure count 2. |
| 251 entry.InformOfRequest(false); |
| 252 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); |
| 253 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); |
| 254 |
| 255 // Success. We should go back to the original delay. |
| 256 entry.InformOfRequest(true); |
| 257 EXPECT_EQ(TimeDelta::FromMilliseconds(1000), entry.GetTimeUntilRelease()); |
| 258 |
| 259 // Failure count reaches 2 again. We should increase the delay once more. |
| 260 entry.InformOfRequest(false); |
| 261 EXPECT_EQ(TimeDelta::FromMilliseconds(2000), entry.GetTimeUntilRelease()); |
| 262 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(2000)); |
| 263 } |
| 264 |
| 175 TEST(BackoffEntryTest, RetainCustomHorizon) { | 265 TEST(BackoffEntryTest, RetainCustomHorizon) { |
| 176 TestBackoffEntry custom(&base_policy); | 266 TestBackoffEntry custom(&base_policy); |
| 177 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); | 267 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); |
| 178 custom.SetCustomReleaseTime(custom_horizon); | 268 custom.SetCustomReleaseTime(custom_horizon); |
| 179 custom.InformOfRequest(false); | 269 custom.InformOfRequest(false); |
| 180 custom.InformOfRequest(true); | 270 custom.InformOfRequest(true); |
| 181 custom.set_now(TimeTicks() + TimeDelta::FromDays(2)); | 271 custom.set_now(TimeTicks() + TimeDelta::FromDays(2)); |
| 182 custom.InformOfRequest(false); | 272 custom.InformOfRequest(false); |
| 183 custom.InformOfRequest(true); | 273 custom.InformOfRequest(true); |
| 184 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); | 274 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 197 BackoffEntry::Policy lenient_policy = base_policy; | 287 BackoffEntry::Policy lenient_policy = base_policy; |
| 198 lenient_policy.num_errors_to_ignore = 1; | 288 lenient_policy.num_errors_to_ignore = 1; |
| 199 TestBackoffEntry custom(&lenient_policy); | 289 TestBackoffEntry custom(&lenient_policy); |
| 200 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); | 290 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); |
| 201 custom.SetCustomReleaseTime(custom_horizon); | 291 custom.SetCustomReleaseTime(custom_horizon); |
| 202 custom.InformOfRequest(false); // This must not reset the horizon. | 292 custom.InformOfRequest(false); // This must not reset the horizon. |
| 203 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); | 293 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); |
| 204 } | 294 } |
| 205 | 295 |
| 206 } // namespace | 296 } // namespace |
| OLD | NEW |