logging_timer_unittest.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2017 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. #include "extensions/test/logging_timer.h"
  5. #include "base/test/simple_test_tick_clock.h"
  6. #include "base/time/time.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace extensions {
  9. namespace {
  10. // Shorthand.
  11. const auto& GetMS = base::Milliseconds<int>;
  12. } // namespace
  13. class LoggingTimerTest : public testing::Test {
  14. public:
  15. LoggingTimerTest() {}
  16. LoggingTimerTest(const LoggingTimerTest&) = delete;
  17. LoggingTimerTest& operator=(const LoggingTimerTest&) = delete;
  18. ~LoggingTimerTest() override {}
  19. void SetUp() override { LoggingTimer::set_clock_for_testing(&tick_clock_); }
  20. void TearDown() override { LoggingTimer::set_clock_for_testing(nullptr); }
  21. // Creates a LoggingTimer for the |key|, and advanced the clock by
  22. // |elapsed|.
  23. void KillTime(base::TimeDelta elapsed, const char* key) {
  24. LoggingTimer timer(key);
  25. tick_clock_.Advance(elapsed);
  26. }
  27. private:
  28. base::SimpleTestTickClock tick_clock_;
  29. };
  30. TEST_F(LoggingTimerTest, TestIncrements) {
  31. constexpr char key[] = "test_increments";
  32. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key).is_zero());
  33. KillTime(GetMS(10), key);
  34. base::TimeDelta time1 = LoggingTimer::GetTrackedTime(key);
  35. EXPECT_EQ(GetMS(10), time1);
  36. KillTime(GetMS(10), key);
  37. base::TimeDelta time2 = LoggingTimer::GetTrackedTime(key);
  38. EXPECT_EQ(GetMS(20), time2);
  39. }
  40. TEST_F(LoggingTimerTest, TestMultipleKeys) {
  41. constexpr char key1[] = "key1";
  42. constexpr char key2[] = "key2";
  43. // Timers are grouped by pointer equality, so key3 should be separate
  44. // (despite having the same name as key1).
  45. constexpr char key3[] = "key1";
  46. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key1).is_zero());
  47. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key2).is_zero());
  48. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key3).is_zero());
  49. KillTime(GetMS(10), key1);
  50. EXPECT_EQ(GetMS(10), LoggingTimer::GetTrackedTime(key1));
  51. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key2).is_zero());
  52. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key3).is_zero());
  53. KillTime(GetMS(10), key1);
  54. EXPECT_EQ(GetMS(20), LoggingTimer::GetTrackedTime(key1));
  55. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key2).is_zero());
  56. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key3).is_zero());
  57. KillTime(GetMS(10), key2);
  58. EXPECT_EQ(GetMS(20), LoggingTimer::GetTrackedTime(key1));
  59. EXPECT_EQ(GetMS(10), LoggingTimer::GetTrackedTime(key2));
  60. EXPECT_TRUE(LoggingTimer::GetTrackedTime(key3).is_zero());
  61. KillTime(GetMS(5), key3);
  62. EXPECT_EQ(GetMS(20), LoggingTimer::GetTrackedTime(key1));
  63. EXPECT_EQ(GetMS(10), LoggingTimer::GetTrackedTime(key2));
  64. EXPECT_EQ(GetMS(5), LoggingTimer::GetTrackedTime(key3));
  65. }
  66. } // namespace extensions