| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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, false }; | 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), now_(TimeTicks()) { |
| 20 now_(TimeTicks()) { | |
| 21 // Work around initialization in constructor not picking up | 20 // Work around initialization in constructor not picking up |
| 22 // fake time. | 21 // fake time. |
| 23 SetCustomReleaseTime(TimeTicks()); | 22 SetCustomReleaseTime(TimeTicks()); |
| 24 } | 23 } |
| 25 | 24 |
| 26 virtual ~TestBackoffEntry() {} | 25 virtual ~TestBackoffEntry() {} |
| 27 | 26 |
| 28 virtual TimeTicks ImplGetTimeNow() const override { | 27 virtual TimeTicks ImplGetTimeNow() const override { return now_; } |
| 29 return now_; | |
| 30 } | |
| 31 | 28 |
| 32 void set_now(const TimeTicks& now) { | 29 void set_now(const TimeTicks& now) { now_ = now; } |
| 33 now_ = now; | |
| 34 } | |
| 35 | 30 |
| 36 private: | 31 private: |
| 37 TimeTicks now_; | 32 TimeTicks now_; |
| 38 | 33 |
| 39 DISALLOW_COPY_AND_ASSIGN(TestBackoffEntry); | 34 DISALLOW_COPY_AND_ASSIGN(TestBackoffEntry); |
| 40 }; | 35 }; |
| 41 | 36 |
| 42 TEST(BackoffEntryTest, BaseTest) { | 37 TEST(BackoffEntryTest, BaseTest) { |
| 43 TestBackoffEntry entry(&base_policy); | 38 TestBackoffEntry entry(&base_policy); |
| 44 EXPECT_FALSE(entry.ShouldRejectRequest()); | 39 EXPECT_FALSE(entry.ShouldRejectRequest()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 61 TEST(BackoffEntryTest, CanDiscard) { | 56 TEST(BackoffEntryTest, CanDiscard) { |
| 62 TestBackoffEntry entry(&base_policy); | 57 TestBackoffEntry entry(&base_policy); |
| 63 // Because lifetime is non-zero, we shouldn't be able to discard yet. | 58 // Because lifetime is non-zero, we shouldn't be able to discard yet. |
| 64 EXPECT_FALSE(entry.CanDiscard()); | 59 EXPECT_FALSE(entry.CanDiscard()); |
| 65 | 60 |
| 66 // Test the "being used" case. | 61 // Test the "being used" case. |
| 67 entry.InformOfRequest(false); | 62 entry.InformOfRequest(false); |
| 68 EXPECT_FALSE(entry.CanDiscard()); | 63 EXPECT_FALSE(entry.CanDiscard()); |
| 69 | 64 |
| 70 // Test the case where there are errors but we can time out. | 65 // Test the case where there are errors but we can time out. |
| 66 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds(1)); |
| 67 EXPECT_FALSE(entry.CanDiscard()); |
| 71 entry.set_now( | 68 entry.set_now( |
| 72 entry.GetReleaseTime() + TimeDelta::FromMilliseconds(1)); | 69 entry.GetReleaseTime() + |
| 73 EXPECT_FALSE(entry.CanDiscard()); | 70 TimeDelta::FromMilliseconds(base_policy.maximum_backoff_ms + 1)); |
| 74 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( | |
| 75 base_policy.maximum_backoff_ms + 1)); | |
| 76 EXPECT_TRUE(entry.CanDiscard()); | 71 EXPECT_TRUE(entry.CanDiscard()); |
| 77 | 72 |
| 78 // Test the final case (no errors, dependent only on specified lifetime). | 73 // Test the final case (no errors, dependent only on specified lifetime). |
| 79 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( | 74 entry.set_now(entry.GetReleaseTime() + |
| 80 base_policy.entry_lifetime_ms - 1)); | 75 TimeDelta::FromMilliseconds(base_policy.entry_lifetime_ms - 1)); |
| 81 entry.InformOfRequest(true); | 76 entry.InformOfRequest(true); |
| 82 EXPECT_FALSE(entry.CanDiscard()); | 77 EXPECT_FALSE(entry.CanDiscard()); |
| 83 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( | 78 entry.set_now(entry.GetReleaseTime() + |
| 84 base_policy.entry_lifetime_ms)); | 79 TimeDelta::FromMilliseconds(base_policy.entry_lifetime_ms)); |
| 85 EXPECT_TRUE(entry.CanDiscard()); | 80 EXPECT_TRUE(entry.CanDiscard()); |
| 86 } | 81 } |
| 87 | 82 |
| 88 TEST(BackoffEntryTest, CanDiscardAlwaysDelay) { | 83 TEST(BackoffEntryTest, CanDiscardAlwaysDelay) { |
| 89 BackoffEntry::Policy always_delay_policy = base_policy; | 84 BackoffEntry::Policy always_delay_policy = base_policy; |
| 90 always_delay_policy.always_use_initial_delay = true; | 85 always_delay_policy.always_use_initial_delay = true; |
| 91 always_delay_policy.entry_lifetime_ms = 0; | 86 always_delay_policy.entry_lifetime_ms = 0; |
| 92 | 87 |
| 93 TestBackoffEntry entry(&always_delay_policy); | 88 TestBackoffEntry entry(&always_delay_policy); |
| 94 | 89 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 entry.InformOfRequest(false); | 146 entry.InformOfRequest(false); |
| 152 result = entry.GetReleaseTime(); | 147 result = entry.GetReleaseTime(); |
| 153 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); | 148 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); |
| 154 EXPECT_EQ(TimeDelta::FromMilliseconds(4000), entry.GetTimeUntilRelease()); | 149 EXPECT_EQ(TimeDelta::FromMilliseconds(4000), entry.GetTimeUntilRelease()); |
| 155 | 150 |
| 156 // 6 errors (to check it doesn't pass maximum). | 151 // 6 errors (to check it doesn't pass maximum). |
| 157 entry.InformOfRequest(false); | 152 entry.InformOfRequest(false); |
| 158 entry.InformOfRequest(false); | 153 entry.InformOfRequest(false); |
| 159 entry.InformOfRequest(false); | 154 entry.InformOfRequest(false); |
| 160 result = entry.GetReleaseTime(); | 155 result = entry.GetReleaseTime(); |
| 161 EXPECT_EQ( | 156 EXPECT_EQ(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(20000), |
| 162 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(20000), result); | 157 result); |
| 163 } | 158 } |
| 164 | 159 |
| 165 TEST(BackoffEntryTest, ReleaseTimeCalculationAlwaysDelay) { | 160 TEST(BackoffEntryTest, ReleaseTimeCalculationAlwaysDelay) { |
| 166 BackoffEntry::Policy always_delay_policy = base_policy; | 161 BackoffEntry::Policy always_delay_policy = base_policy; |
| 167 always_delay_policy.always_use_initial_delay = true; | 162 always_delay_policy.always_use_initial_delay = true; |
| 168 always_delay_policy.num_errors_to_ignore = 2; | 163 always_delay_policy.num_errors_to_ignore = 2; |
| 169 | 164 |
| 170 TestBackoffEntry entry(&always_delay_policy); | 165 TestBackoffEntry entry(&always_delay_policy); |
| 171 | 166 |
| 172 // With previous requests, should return "now". | 167 // With previous requests, should return "now". |
| (...skipping 29 matching lines...) Expand all Loading... |
| 202 for (int i = 0; i < 10; ++i) { | 197 for (int i = 0; i < 10; ++i) { |
| 203 BackoffEntry::Policy jittery_policy = base_policy; | 198 BackoffEntry::Policy jittery_policy = base_policy; |
| 204 jittery_policy.jitter_factor = 0.2; | 199 jittery_policy.jitter_factor = 0.2; |
| 205 | 200 |
| 206 TestBackoffEntry entry(&jittery_policy); | 201 TestBackoffEntry entry(&jittery_policy); |
| 207 | 202 |
| 208 entry.InformOfRequest(false); | 203 entry.InformOfRequest(false); |
| 209 entry.InformOfRequest(false); | 204 entry.InformOfRequest(false); |
| 210 entry.InformOfRequest(false); | 205 entry.InformOfRequest(false); |
| 211 TimeTicks result = entry.GetReleaseTime(); | 206 TimeTicks result = entry.GetReleaseTime(); |
| 212 EXPECT_LE( | 207 EXPECT_LE(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(3200), |
| 213 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(3200), result); | 208 result); |
| 214 EXPECT_GE( | 209 EXPECT_GE(entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), |
| 215 entry.ImplGetTimeNow() + TimeDelta::FromMilliseconds(4000), result); | 210 result); |
| 216 } | 211 } |
| 217 } | 212 } |
| 218 | 213 |
| 219 TEST(BackoffEntryTest, FailureThenSuccess) { | 214 TEST(BackoffEntryTest, FailureThenSuccess) { |
| 220 TestBackoffEntry entry(&base_policy); | 215 TestBackoffEntry entry(&base_policy); |
| 221 | 216 |
| 222 // Failure count 1, establishes horizon. | 217 // Failure count 1, establishes horizon. |
| 223 entry.InformOfRequest(false); | 218 entry.InformOfRequest(false); |
| 224 TimeTicks release_time = entry.GetReleaseTime(); | 219 TimeTicks release_time = entry.GetReleaseTime(); |
| 225 EXPECT_EQ(TimeTicks() + TimeDelta::FromMilliseconds(1000), release_time); | 220 EXPECT_EQ(TimeTicks() + TimeDelta::FromMilliseconds(1000), release_time); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 | 290 |
| 296 TEST(BackoffEntryTest, OverflowProtection) { | 291 TEST(BackoffEntryTest, OverflowProtection) { |
| 297 BackoffEntry::Policy large_multiply_policy = base_policy; | 292 BackoffEntry::Policy large_multiply_policy = base_policy; |
| 298 large_multiply_policy.multiply_factor = 256; | 293 large_multiply_policy.multiply_factor = 256; |
| 299 TestBackoffEntry custom(&large_multiply_policy); | 294 TestBackoffEntry custom(&large_multiply_policy); |
| 300 | 295 |
| 301 // Trigger enough failures such that more than 11 bits of exponent are used | 296 // Trigger enough failures such that more than 11 bits of exponent are used |
| 302 // to represent the exponential backoff intermediate values. Given a multiply | 297 // to represent the exponential backoff intermediate values. Given a multiply |
| 303 // factor of 256 (2^8), 129 iterations is enough: 2^(8*(129-1)) = 2^1024. | 298 // factor of 256 (2^8), 129 iterations is enough: 2^(8*(129-1)) = 2^1024. |
| 304 for (int i = 0; i < 129; ++i) { | 299 for (int i = 0; i < 129; ++i) { |
| 305 custom.set_now(custom.ImplGetTimeNow() + custom.GetTimeUntilRelease()); | 300 custom.set_now(custom.ImplGetTimeNow() + custom.GetTimeUntilRelease()); |
| 306 custom.InformOfRequest(false); | 301 custom.InformOfRequest(false); |
| 307 ASSERT_TRUE(custom.ShouldRejectRequest()); | 302 ASSERT_TRUE(custom.ShouldRejectRequest()); |
| 308 } | 303 } |
| 309 | 304 |
| 310 // Max delay should still be respected. | 305 // Max delay should still be respected. |
| 311 EXPECT_EQ(20000, custom.GetTimeUntilRelease().InMilliseconds()); | 306 EXPECT_EQ(20000, custom.GetTimeUntilRelease().InMilliseconds()); |
| 312 } | 307 } |
| 313 | 308 |
| 314 } // namespace | 309 } // namespace |
| OLD | NEW |