OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/backoff_entry.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 namespace { |
| 9 |
| 10 using base::TimeDelta; |
| 11 using base::TimeTicks; |
| 12 using net::BackoffEntry; |
| 13 |
| 14 BackoffEntry::Policy base_policy = { 0, 1000, 2.0, 0.0, 20000, 2000 }; |
| 15 |
| 16 class TestBackoffEntry : public BackoffEntry { |
| 17 public: |
| 18 TestBackoffEntry(const Policy* policy) |
| 19 : BackoffEntry(policy), |
| 20 now_(TimeTicks()) { |
| 21 // Work around initialization in constructor not picking up |
| 22 // fake time. |
| 23 SetCustomReleaseTime(TimeTicks()); |
| 24 } |
| 25 |
| 26 virtual TimeTicks GetTimeNow() const { |
| 27 return now_; |
| 28 } |
| 29 |
| 30 void set_now(TimeTicks now) { |
| 31 now_ = now; |
| 32 } |
| 33 |
| 34 private: |
| 35 TimeTicks now_; |
| 36 }; |
| 37 |
| 38 TEST(BackoffEntryTest, BaseTest) { |
| 39 TestBackoffEntry entry(&base_policy); |
| 40 EXPECT_FALSE(entry.ShouldRejectRequest()); |
| 41 |
| 42 entry.InformOfRequest(false); |
| 43 EXPECT_TRUE(entry.ShouldRejectRequest()); |
| 44 } |
| 45 |
| 46 TEST(BackoffEntryTest, CanDiscardNeverExpires) { |
| 47 BackoffEntry::Policy never_expires_policy = base_policy; |
| 48 never_expires_policy.entry_lifetime_ms_ = -1; |
| 49 TestBackoffEntry never_expires(&never_expires_policy); |
| 50 EXPECT_FALSE(never_expires.CanDiscard()); |
| 51 } |
| 52 |
| 53 TEST(BackoffEntryTest, CanDiscard) { |
| 54 TestBackoffEntry entry(&base_policy); |
| 55 EXPECT_FALSE(entry.CanDiscard()); // because of lifetime |
| 56 |
| 57 // Test the "being used" case. |
| 58 entry.InformOfRequest(false); |
| 59 EXPECT_FALSE(entry.CanDiscard()); |
| 60 |
| 61 // Test the case where there are errors but we can time out. |
| 62 entry.set_now( |
| 63 entry.GetReleaseTime() + TimeDelta::FromMilliseconds(1)); |
| 64 EXPECT_FALSE(entry.CanDiscard()); |
| 65 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( |
| 66 base_policy.maximum_backoff_ms_ + 1)); |
| 67 EXPECT_TRUE(entry.CanDiscard()); |
| 68 |
| 69 // Test the final case (no errors, dependent only on specified lifetime). |
| 70 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( |
| 71 base_policy.entry_lifetime_ms_ - 1)); |
| 72 entry.InformOfRequest(true); |
| 73 EXPECT_FALSE(entry.CanDiscard()); |
| 74 entry.set_now(entry.GetReleaseTime() + TimeDelta::FromMilliseconds( |
| 75 base_policy.entry_lifetime_ms_)); |
| 76 EXPECT_TRUE(entry.CanDiscard()); |
| 77 } |
| 78 |
| 79 TEST(BackoffEntryTest, CanDiscardNotStored) { |
| 80 BackoffEntry::Policy no_store_policy = base_policy; |
| 81 no_store_policy.entry_lifetime_ms_ = 0; |
| 82 TestBackoffEntry not_stored(&no_store_policy); |
| 83 EXPECT_TRUE(not_stored.CanDiscard()); |
| 84 } |
| 85 |
| 86 TEST(BackoffEntryTest, ShouldIgnoreFirstTwo) { |
| 87 BackoffEntry::Policy lenient_policy = base_policy; |
| 88 lenient_policy.num_errors_to_ignore_ = 2; |
| 89 |
| 90 BackoffEntry entry(&lenient_policy); |
| 91 entry.InformOfRequest(false); |
| 92 EXPECT_FALSE(entry.ShouldRejectRequest()); |
| 93 entry.InformOfRequest(false); |
| 94 EXPECT_FALSE(entry.ShouldRejectRequest()); |
| 95 entry.InformOfRequest(false); |
| 96 EXPECT_TRUE(entry.ShouldRejectRequest()); |
| 97 } |
| 98 |
| 99 TEST(BackoffEntryTest, ReleaseTimeCalculation) { |
| 100 TestBackoffEntry entry(&base_policy); |
| 101 |
| 102 // With zero errors, should return "now". |
| 103 TimeTicks result = entry.GetReleaseTime(); |
| 104 EXPECT_EQ(entry.GetTimeNow(), result); |
| 105 |
| 106 // 1 error. |
| 107 entry.InformOfRequest(false); |
| 108 result = entry.GetReleaseTime(); |
| 109 EXPECT_EQ(entry.GetTimeNow() + TimeDelta::FromMilliseconds(1000), result); |
| 110 |
| 111 // 2 errors. |
| 112 entry.InformOfRequest(false); |
| 113 result = entry.GetReleaseTime(); |
| 114 EXPECT_EQ(entry.GetTimeNow() + TimeDelta::FromMilliseconds(2000), result); |
| 115 |
| 116 // 3 errors. |
| 117 entry.InformOfRequest(false); |
| 118 result = entry.GetReleaseTime(); |
| 119 EXPECT_EQ(entry.GetTimeNow() + TimeDelta::FromMilliseconds(4000), result); |
| 120 |
| 121 // 6 errors (to check it doesn't pass maximum). |
| 122 entry.InformOfRequest(false); |
| 123 entry.InformOfRequest(false); |
| 124 entry.InformOfRequest(false); |
| 125 result = entry.GetReleaseTime(); |
| 126 EXPECT_EQ(entry.GetTimeNow() + TimeDelta::FromMilliseconds(20000), result); |
| 127 } |
| 128 |
| 129 TEST(BackoffEntryTest, ReleaseTimeCalculationWithJitter) { |
| 130 for (int i = 0; i < 10; ++i) { |
| 131 BackoffEntry::Policy jittery_policy = base_policy; |
| 132 jittery_policy.jitter_factor_ = 0.2; |
| 133 |
| 134 TestBackoffEntry entry(&jittery_policy); |
| 135 |
| 136 entry.InformOfRequest(false); |
| 137 entry.InformOfRequest(false); |
| 138 entry.InformOfRequest(false); |
| 139 TimeTicks result = entry.GetReleaseTime(); |
| 140 EXPECT_LE(entry.GetTimeNow() + TimeDelta::FromMilliseconds(3200), result); |
| 141 EXPECT_GE(entry.GetTimeNow() + TimeDelta::FromMilliseconds(4000), result); |
| 142 } |
| 143 } |
| 144 |
| 145 TEST(BackoffEntryTest, FailureThenSuccess) { |
| 146 TestBackoffEntry entry(&base_policy); |
| 147 |
| 148 // Failure count 1, establishes horizon. |
| 149 entry.InformOfRequest(false); |
| 150 TimeTicks release_time = entry.GetReleaseTime(); |
| 151 EXPECT_EQ(TimeTicks() + TimeDelta::FromMilliseconds(1000), release_time); |
| 152 |
| 153 // Success, failure count 0, should not advance past |
| 154 // the horizon that was already set. |
| 155 entry.set_now(release_time - TimeDelta::FromMilliseconds(200)); |
| 156 entry.InformOfRequest(true); |
| 157 EXPECT_EQ(release_time, entry.GetReleaseTime()); |
| 158 |
| 159 // Failure, failure count 1. |
| 160 entry.InformOfRequest(false); |
| 161 EXPECT_EQ(release_time + TimeDelta::FromMilliseconds(800), |
| 162 entry.GetReleaseTime()); |
| 163 } |
| 164 |
| 165 TEST(BackoffEntryTest, RetainCustomHorizon) { |
| 166 TestBackoffEntry custom(&base_policy); |
| 167 TimeTicks custom_horizon = TimeTicks() + TimeDelta::FromDays(3); |
| 168 custom.SetCustomReleaseTime(custom_horizon); |
| 169 custom.InformOfRequest(false); |
| 170 custom.InformOfRequest(true); |
| 171 custom.set_now(TimeTicks() + TimeDelta::FromDays(2)); |
| 172 custom.InformOfRequest(false); |
| 173 custom.InformOfRequest(true); |
| 174 EXPECT_EQ(custom_horizon, custom.GetReleaseTime()); |
| 175 |
| 176 // Now check that once we are at or past the custom horizon, |
| 177 // we get normal behavior. |
| 178 custom.set_now(TimeTicks() + TimeDelta::FromDays(3)); |
| 179 custom.InformOfRequest(false); |
| 180 EXPECT_EQ( |
| 181 TimeTicks() + TimeDelta::FromDays(3) + TimeDelta::FromMilliseconds(1000), |
| 182 custom.GetReleaseTime()); |
| 183 } |
| 184 |
| 185 } // namespace |
OLD | NEW |