Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "net/base/backoff_entry.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/time/tick_clock.h" | |
| 10 #include "base/values.h" | |
| 11 #include "net/base/backoff_entry_serializer.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 using base::Time; | |
| 19 using base::TimeDelta; | |
| 20 using base::TimeTicks; | |
| 21 | |
| 22 BackoffEntry::Policy base_policy = { | |
| 23 0 /* num_errors_to_ignore */, | |
| 24 1000 /* initial_delay_ms */, | |
| 25 2.0 /* multiply_factor */, | |
| 26 0.0 /* jitter_factor */, | |
| 27 20000 /* maximum_backoff_ms */, | |
| 28 2000 /* entry_lifetime_ms */, | |
| 29 false /* always_use_initial_delay */ | |
| 30 }; | |
| 31 | |
| 32 class TestTickClock : public base::TickClock { | |
| 33 public: | |
| 34 TestTickClock() {} | |
| 35 ~TestTickClock() override {} | |
| 36 | |
| 37 TimeTicks NowTicks() override { return now_ticks_; } | |
| 38 void set_now(TimeTicks now) { now_ticks_ = now; } | |
| 39 | |
| 40 private: | |
| 41 TimeTicks now_ticks_; | |
| 42 DISALLOW_COPY_AND_ASSIGN(TestTickClock); | |
|
mmenke
2015/05/06 17:34:48
nit: blank line before DISALLOW_COPY_AND_ASSIGN
johnme
2015/05/06 20:38:09
Done.
| |
| 43 }; | |
| 44 | |
| 45 TEST(BackoffEntrySerializerTest, SerializeTimeOffsets) { | |
| 46 TestTickClock now_ticks; | |
| 47 Time now_time = Time::FromJsTime(1430907555111.0); // May 2015 | |
| 48 BackoffEntry original(&base_policy, &now_ticks); | |
| 49 // 2 errors. | |
| 50 original.InformOfRequest(false); | |
| 51 original.InformOfRequest(false); | |
| 52 scoped_ptr<base::ListValue> serialized = | |
| 53 BackoffEntrySerializer::SerializeToValue(original, now_time); | |
| 54 | |
| 55 { | |
| 56 // Test that immediate deserialization round-trips. | |
| 57 scoped_ptr<BackoffEntry> deserialized = | |
| 58 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 59 &now_ticks, now_time); | |
| 60 ASSERT_TRUE(deserialized.get()); | |
| 61 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 62 EXPECT_EQ(original.GetReleaseTime(), deserialized->GetReleaseTime()); | |
| 63 } | |
| 64 | |
| 65 { | |
| 66 // Test deserialization when wall clock has advanced but TimeTicks haven't | |
| 67 // (e.g. device was rebooted). | |
| 68 Time later = now_time + TimeDelta::FromDays(1); | |
|
mmenke
2015/05/06 17:34:48
I find these time names pretty confusing - we're d
johnme
2015/05/06 20:38:09
Ok, I went with original_time, original_ticks, lat
mmenke
2015/05/06 20:48:43
SGTM
| |
| 69 scoped_ptr<BackoffEntry> deserialized = | |
| 70 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 71 &now_ticks, later); | |
| 72 ASSERT_TRUE(deserialized.get()); | |
| 73 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 74 EXPECT_EQ(original.GetReleaseTime() - TimeDelta::FromDays(1), | |
| 75 deserialized->GetReleaseTime()); | |
|
mmenke
2015/05/06 17:34:48
This is a negative TimeTicks value, which seems we
mmenke
2015/05/06 17:34:48
I think this is weird enough to be worth a comment
johnme
2015/05/06 20:38:09
Why is negative TimeTicks weird? If we assume that
johnme
2015/05/06 20:38:09
Done, I added details comments to all these cases.
| |
| 76 } | |
| 77 | |
| 78 { | |
| 79 // Test deserialization when TimeTicks have advanced but wall clock hasn't | |
| 80 // (e.g. it's an hour later, but a DST change cancelled that out). | |
| 81 TestTickClock later_ticks; | |
| 82 later_ticks.set_now(TimeTicks() + TimeDelta::FromDays(1)); | |
| 83 scoped_ptr<BackoffEntry> deserialized = | |
| 84 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 85 &later_ticks, now_time); | |
| 86 ASSERT_TRUE(deserialized.get()); | |
| 87 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 88 EXPECT_EQ(original.GetReleaseTime() + TimeDelta::FromDays(1), | |
| 89 deserialized->GetReleaseTime()); | |
| 90 } | |
| 91 | |
| 92 { | |
| 93 // Test deserialization when both wall clock and TimeTicks have advanced | |
| 94 // (e.g. it's just later than it's used to be). | |
| 95 TestTickClock later_ticks; | |
| 96 later_ticks.set_now(TimeTicks() + TimeDelta::FromDays(1)); | |
| 97 Time later = now_time + TimeDelta::FromDays(1); | |
| 98 scoped_ptr<BackoffEntry> deserialized = | |
| 99 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 100 &later_ticks, later); | |
| 101 ASSERT_TRUE(deserialized.get()); | |
| 102 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 103 EXPECT_EQ(original.GetReleaseTime(), deserialized->GetReleaseTime()); | |
|
mmenke
2015/05/06 17:34:48
Again, think this requires an explanation.
johnme
2015/05/06 20:38:09
Done.
| |
| 104 } | |
| 105 | |
| 106 { | |
| 107 // Test deserialization when wall clock has gone backwards but TimeTicks | |
| 108 // haven't (e.g. the system clock was fast but they fixed it). | |
| 109 EXPECT_LT(TimeDelta::FromSeconds(1), original.GetTimeUntilRelease()); | |
| 110 Time earlier = now_time - TimeDelta::FromSeconds(1); | |
| 111 scoped_ptr<BackoffEntry> deserialized = | |
| 112 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 113 &now_ticks, earlier); | |
| 114 ASSERT_TRUE(deserialized.get()); | |
| 115 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 116 // Deserialization should not exceed original time_until_release in such a | |
| 117 // situation. | |
| 118 EXPECT_EQ(original.GetTimeUntilRelease(), | |
| 119 deserialized->GetTimeUntilRelease()); | |
| 120 // Since TimeTicks are equal, the release times will be too. | |
| 121 EXPECT_EQ(original.GetReleaseTime(), deserialized->GetReleaseTime()); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 TEST(BackoffEntrySerializerTest, SerializeNoFailures) { | |
|
mmenke
2015/05/06 17:34:48
Suggest this test first. Think it's nice to have
mmenke
2015/05/06 17:34:48
This is pretty much identical to the first block i
johnme
2015/05/06 20:38:09
Done. It now covers the case where a) there are no
johnme
2015/05/06 20:38:09
Done.
| |
| 126 TestTickClock now_ticks; | |
| 127 BackoffEntry original(&base_policy, &now_ticks); | |
| 128 scoped_ptr<base::ListValue> serialized = | |
| 129 BackoffEntrySerializer::SerializeToValue(original, Time()); | |
| 130 | |
| 131 scoped_ptr<BackoffEntry> deserialized = | |
| 132 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 133 &now_ticks, Time()); | |
| 134 ASSERT_TRUE(deserialized.get()); | |
| 135 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 136 EXPECT_EQ(original.GetReleaseTime(), deserialized->GetReleaseTime()); | |
| 137 } | |
| 138 | |
| 139 } // namespace | |
| 140 | |
| 141 } // namespace net | |
| OLD | NEW |