| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 // Tests for Watchdog class. | |
| 6 | |
| 7 #include "base/watchdog.h" | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/platform_thread.h" | |
| 11 #include "base/spin_wait.h" | |
| 12 #include "base/time.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 using base::TimeDelta; | |
| 16 using base::TimeTicks; | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 //------------------------------------------------------------------------------ | |
| 21 // Provide a derived class to facilitate testing. | |
| 22 | |
| 23 class WatchdogCounter : public Watchdog { | |
| 24 public: | |
| 25 WatchdogCounter(const TimeDelta& duration, | |
| 26 const std::string& thread_watched_name, | |
| 27 bool enabled) | |
| 28 : Watchdog(duration, thread_watched_name, enabled), alarm_counter_(0) { | |
| 29 } | |
| 30 | |
| 31 virtual ~WatchdogCounter() {} | |
| 32 | |
| 33 virtual void Alarm() { | |
| 34 alarm_counter_++; | |
| 35 Watchdog::Alarm(); | |
| 36 } | |
| 37 | |
| 38 int alarm_counter() { return alarm_counter_; } | |
| 39 | |
| 40 private: | |
| 41 int alarm_counter_; | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(WatchdogCounter); | |
| 44 }; | |
| 45 | |
| 46 class WatchdogTest : public testing::Test { | |
| 47 public: | |
| 48 void SetUp() { | |
| 49 Watchdog::ResetStaticData(); | |
| 50 } | |
| 51 }; | |
| 52 | |
| 53 | |
| 54 //------------------------------------------------------------------------------ | |
| 55 // Actual tests | |
| 56 | |
| 57 // Minimal constructor/destructor test. | |
| 58 TEST_F(WatchdogTest, StartupShutdownTest) { | |
| 59 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), "Disabled", false); | |
| 60 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), "Enabled", true); | |
| 61 } | |
| 62 | |
| 63 // Test ability to call Arm and Disarm repeatedly. | |
| 64 TEST_F(WatchdogTest, ArmDisarmTest) { | |
| 65 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), "Disabled", false); | |
| 66 watchdog1.Arm(); | |
| 67 watchdog1.Disarm(); | |
| 68 watchdog1.Arm(); | |
| 69 watchdog1.Disarm(); | |
| 70 | |
| 71 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), "Enabled", true); | |
| 72 watchdog2.Arm(); | |
| 73 watchdog2.Disarm(); | |
| 74 watchdog2.Arm(); | |
| 75 watchdog2.Disarm(); | |
| 76 } | |
| 77 | |
| 78 // Make sure a basic alarm fires when the time has expired. | |
| 79 TEST_F(WatchdogTest, AlarmTest) { | |
| 80 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Enabled", true); | |
| 81 watchdog.Arm(); | |
| 82 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5), | |
| 83 watchdog.alarm_counter() > 0); | |
| 84 EXPECT_EQ(1, watchdog.alarm_counter()); | |
| 85 } | |
| 86 | |
| 87 // Make sure a basic alarm fires when the time has expired. | |
| 88 TEST_F(WatchdogTest, AlarmPriorTimeTest) { | |
| 89 WatchdogCounter watchdog(TimeDelta(), "Enabled2", true); | |
| 90 // Set a time in the past. | |
| 91 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2)); | |
| 92 // It should instantly go off, but certainly in less than 5 minutes. | |
| 93 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5), | |
| 94 watchdog.alarm_counter() > 0); | |
| 95 | |
| 96 EXPECT_EQ(1, watchdog.alarm_counter()); | |
| 97 } | |
| 98 | |
| 99 // Make sure a disable alarm does nothing, even if we arm it. | |
| 100 TEST_F(WatchdogTest, ConstructorDisabledTest) { | |
| 101 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Disabled", false); | |
| 102 watchdog.Arm(); | |
| 103 // Alarm should not fire, as it was disabled. | |
| 104 PlatformThread::Sleep(500); | |
| 105 EXPECT_EQ(0, watchdog.alarm_counter()); | |
| 106 } | |
| 107 | |
| 108 // Make sure Disarming will prevent firing, even after Arming. | |
| 109 TEST_F(WatchdogTest, DisarmTest) { | |
| 110 WatchdogCounter watchdog(TimeDelta::FromSeconds(1), "Enabled3", true); | |
| 111 | |
| 112 TimeTicks start = TimeTicks::Now(); | |
| 113 watchdog.Arm(); | |
| 114 PlatformThread::Sleep(100); // Sleep a bit, but not past the alarm point. | |
| 115 watchdog.Disarm(); | |
| 116 TimeTicks end = TimeTicks::Now(); | |
| 117 | |
| 118 if (end - start > TimeDelta::FromMilliseconds(500)) { | |
| 119 LOG(WARNING) << "100ms sleep took over 500ms, making the results of this " | |
| 120 << "timing-sensitive test suspicious. Aborting now."; | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 // Alarm should not have fired before it was disarmed. | |
| 125 EXPECT_EQ(0, watchdog.alarm_counter()); | |
| 126 | |
| 127 // Sleep past the point where it would have fired if it wasn't disarmed, | |
| 128 // and verify that it didn't fire. | |
| 129 PlatformThread::Sleep(1000); | |
| 130 EXPECT_EQ(0, watchdog.alarm_counter()); | |
| 131 | |
| 132 // ...but even after disarming, we can still use the alarm... | |
| 133 // Set a time greater than the timeout into the past. | |
| 134 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(10)); | |
| 135 // It should almost instantly go off, but certainly in less than 5 minutes. | |
| 136 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5), | |
| 137 watchdog.alarm_counter() > 0); | |
| 138 | |
| 139 EXPECT_EQ(1, watchdog.alarm_counter()); | |
| 140 } | |
| 141 | |
| 142 } // namespace | |
| OLD | NEW |