Chromium Code Reviews| 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 | 6 |
| 7 #include "base/logging.h" | |
| 7 #include "base/macros.h" | 8 #include "base/macros.h" |
| 8 #include "base/time/tick_clock.h" | 9 #include "base/time/tick_clock.h" |
| 10 #include "base/values.h" | |
| 11 #include "net/base/backoff_entry_serializer.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 13 |
| 11 namespace net { | 14 namespace net { |
| 12 | 15 |
| 13 namespace { | 16 namespace { |
| 14 | 17 |
| 18 using base::Time; | |
| 15 using base::TimeDelta; | 19 using base::TimeDelta; |
| 16 using base::TimeTicks; | 20 using base::TimeTicks; |
| 17 | 21 |
| 18 BackoffEntry::Policy base_policy = { 0, 1000, 2.0, 0.0, 20000, 2000, false }; | 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 }; | |
| 19 | 31 |
| 20 class TestTickClock : public base::TickClock { | 32 class TestTickClock : public base::TickClock { |
| 21 public: | 33 public: |
| 22 TestTickClock() {} | 34 TestTickClock() {} |
| 23 ~TestTickClock() override {} | 35 ~TestTickClock() override {} |
| 24 | 36 |
| 25 TimeTicks NowTicks() override { return now_ticks_; } | 37 TimeTicks NowTicks() override { return now_ticks_; } |
| 26 void set_now(TimeTicks now) { now_ticks_ = now; } | 38 void set_now(TimeTicks now) { now_ticks_ = now; } |
| 27 | 39 |
| 28 private: | 40 private: |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 304 for (int i = 0; i < 129; ++i) { | 316 for (int i = 0; i < 129; ++i) { |
| 305 now_ticks.set_now(now_ticks.NowTicks() + custom.GetTimeUntilRelease()); | 317 now_ticks.set_now(now_ticks.NowTicks() + custom.GetTimeUntilRelease()); |
| 306 custom.InformOfRequest(false); | 318 custom.InformOfRequest(false); |
| 307 ASSERT_TRUE(custom.ShouldRejectRequest()); | 319 ASSERT_TRUE(custom.ShouldRejectRequest()); |
| 308 } | 320 } |
| 309 | 321 |
| 310 // Max delay should still be respected. | 322 // Max delay should still be respected. |
| 311 EXPECT_EQ(20000, custom.GetTimeUntilRelease().InMilliseconds()); | 323 EXPECT_EQ(20000, custom.GetTimeUntilRelease().InMilliseconds()); |
| 312 } | 324 } |
| 313 | 325 |
| 326 TEST(BackoffEntryTest, SerializeTimeOffsets) { | |
|
mmenke
2015/05/05 15:47:52
These should probably go in backoff_entry_serializ
johnme
2015/05/06 12:46:47
Done.
| |
| 327 TestTickClock now_ticks; | |
| 328 BackoffEntry original(&base_policy, &now_ticks); | |
| 329 // 2 errors. | |
| 330 original.InformOfRequest(false); | |
| 331 original.InformOfRequest(false); | |
| 332 scoped_ptr<base::ListValue> serialized = | |
| 333 BackoffEntrySerializer::SerializeToValue(original, base::Time()); | |
| 334 | |
| 335 { | |
| 336 // Test that immediate deserializeation round-trips. | |
| 337 scoped_ptr<BackoffEntry> deserialized = | |
| 338 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 339 &now_ticks, base::Time()); | |
| 340 ASSERT_TRUE(deserialized.get()); | |
| 341 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 342 EXPECT_EQ(original.GetReleaseTime(), deserialized->GetReleaseTime()); | |
| 343 } | |
| 344 | |
| 345 { | |
| 346 // Test deserialization when wall clock has advanced but TimeTicks haven't | |
| 347 // (e.g. device was rebooted). | |
| 348 base::Time later = base::Time() + TimeDelta::FromDays(1); | |
| 349 scoped_ptr<BackoffEntry> deserialized = | |
| 350 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 351 &now_ticks, later); | |
| 352 ASSERT_TRUE(deserialized.get()); | |
| 353 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 354 EXPECT_EQ(original.GetReleaseTime() - TimeDelta::FromDays(1), | |
| 355 deserialized->GetReleaseTime()); | |
| 356 } | |
| 357 | |
| 358 { | |
| 359 // Test deserialization when TimeTicks have advanced but wall clock hasn't | |
| 360 // (e.g. it's an hour later, but a DST change cancelled that out). | |
| 361 TestTickClock later_ticks; | |
| 362 later_ticks.set_now(base::TimeTicks() + TimeDelta::FromDays(1)); | |
| 363 scoped_ptr<BackoffEntry> deserialized = | |
| 364 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 365 &later_ticks, | |
| 366 base::Time()); | |
| 367 ASSERT_TRUE(deserialized.get()); | |
| 368 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 369 EXPECT_EQ(original.GetReleaseTime() + TimeDelta::FromDays(1), | |
| 370 deserialized->GetReleaseTime()); | |
| 371 } | |
| 372 | |
| 373 { | |
| 374 // Test deserialization when both wall clock and TimeTicks have advanced | |
| 375 // (e.g. it's just later than it's used to be). | |
| 376 TestTickClock later_ticks; | |
| 377 later_ticks.set_now(base::TimeTicks() + TimeDelta::FromDays(1)); | |
| 378 base::Time later = base::Time() + TimeDelta::FromDays(1); | |
| 379 scoped_ptr<BackoffEntry> deserialized = | |
| 380 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 381 &later_ticks, later); | |
| 382 ASSERT_TRUE(deserialized.get()); | |
| 383 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 384 EXPECT_EQ(original.GetReleaseTime(), deserialized->GetReleaseTime()); | |
| 385 } | |
| 386 | |
| 387 { | |
| 388 // Test deserialization when wall clock has gone backwards but TimeTicks | |
| 389 // haven't (e.g. the system clock was fast but they fixed it). | |
| 390 EXPECT_LT(TimeDelta::FromSeconds(1), original.GetTimeUntilRelease()); | |
| 391 base::Time earlier = base::Time() - TimeDelta::FromSeconds(1); | |
| 392 scoped_ptr<BackoffEntry> deserialized = | |
| 393 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 394 &now_ticks, earlier); | |
| 395 ASSERT_TRUE(deserialized.get()); | |
| 396 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 397 // Deserialization should not exceed original time_until_release in such a | |
| 398 // situation. | |
| 399 EXPECT_EQ(original.GetTimeUntilRelease(), | |
| 400 deserialized->GetTimeUntilRelease()); | |
| 401 // Since TimeTicks are equal, the release times will be too. | |
| 402 EXPECT_EQ(original.GetReleaseTime(), deserialized->GetReleaseTime()); | |
| 403 } | |
| 404 } | |
| 405 | |
| 406 TEST(BackoffEntryTest, SerializeNoFailures) { | |
| 407 TestTickClock now_ticks; | |
| 408 BackoffEntry original(&base_policy, &now_ticks); | |
| 409 scoped_ptr<base::ListValue> serialized = | |
| 410 BackoffEntrySerializer::SerializeToValue(original, base::Time()); | |
| 411 | |
| 412 scoped_ptr<BackoffEntry> deserialized = | |
| 413 BackoffEntrySerializer::DeserializeFromValue(*serialized, &base_policy, | |
| 414 &now_ticks, base::Time()); | |
| 415 ASSERT_TRUE(deserialized.get()); | |
| 416 EXPECT_EQ(original.failure_count(), deserialized->failure_count()); | |
| 417 EXPECT_EQ(original.GetReleaseTime(), deserialized->GetReleaseTime()); | |
| 418 } | |
| 419 | |
| 314 } // namespace | 420 } // namespace |
| 315 | 421 |
| 316 } // namespace net | 422 } // namespace net |
| OLD | NEW |