| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 "base/threading/watchdog.h" | 5 #include "base/threading/watchdog.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/spin_wait.h" | 8 #include "base/spin_wait.h" |
| 9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 watchdog.alarm_counter() > 0); | 92 watchdog.alarm_counter() > 0); |
| 93 | 93 |
| 94 EXPECT_EQ(1, watchdog.alarm_counter()); | 94 EXPECT_EQ(1, watchdog.alarm_counter()); |
| 95 } | 95 } |
| 96 | 96 |
| 97 // Make sure a disable alarm does nothing, even if we arm it. | 97 // Make sure a disable alarm does nothing, even if we arm it. |
| 98 TEST_F(WatchdogTest, ConstructorDisabledTest) { | 98 TEST_F(WatchdogTest, ConstructorDisabledTest) { |
| 99 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Disabled", false); | 99 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Disabled", false); |
| 100 watchdog.Arm(); | 100 watchdog.Arm(); |
| 101 // Alarm should not fire, as it was disabled. | 101 // Alarm should not fire, as it was disabled. |
| 102 PlatformThread::Sleep(500); | 102 PlatformThread::Sleep(TimeDelta::FromMilliseconds(500)); |
| 103 EXPECT_EQ(0, watchdog.alarm_counter()); | 103 EXPECT_EQ(0, watchdog.alarm_counter()); |
| 104 } | 104 } |
| 105 | 105 |
| 106 // Make sure Disarming will prevent firing, even after Arming. | 106 // Make sure Disarming will prevent firing, even after Arming. |
| 107 TEST_F(WatchdogTest, DisarmTest) { | 107 TEST_F(WatchdogTest, DisarmTest) { |
| 108 WatchdogCounter watchdog(TimeDelta::FromSeconds(1), "Enabled3", true); | 108 WatchdogCounter watchdog(TimeDelta::FromSeconds(1), "Enabled3", true); |
| 109 | 109 |
| 110 TimeTicks start = TimeTicks::Now(); | 110 TimeTicks start = TimeTicks::Now(); |
| 111 watchdog.Arm(); | 111 watchdog.Arm(); |
| 112 PlatformThread::Sleep(100); // Sleep a bit, but not past the alarm point. | 112 // Sleep a bit, but not past the alarm point. |
| 113 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
| 113 watchdog.Disarm(); | 114 watchdog.Disarm(); |
| 114 TimeTicks end = TimeTicks::Now(); | 115 TimeTicks end = TimeTicks::Now(); |
| 115 | 116 |
| 116 if (end - start > TimeDelta::FromMilliseconds(500)) { | 117 if (end - start > TimeDelta::FromMilliseconds(500)) { |
| 117 LOG(WARNING) << "100ms sleep took over 500ms, making the results of this " | 118 LOG(WARNING) << "100ms sleep took over 500ms, making the results of this " |
| 118 << "timing-sensitive test suspicious. Aborting now."; | 119 << "timing-sensitive test suspicious. Aborting now."; |
| 119 return; | 120 return; |
| 120 } | 121 } |
| 121 | 122 |
| 122 // Alarm should not have fired before it was disarmed. | 123 // Alarm should not have fired before it was disarmed. |
| 123 EXPECT_EQ(0, watchdog.alarm_counter()); | 124 EXPECT_EQ(0, watchdog.alarm_counter()); |
| 124 | 125 |
| 125 // Sleep past the point where it would have fired if it wasn't disarmed, | 126 // Sleep past the point where it would have fired if it wasn't disarmed, |
| 126 // and verify that it didn't fire. | 127 // and verify that it didn't fire. |
| 127 PlatformThread::Sleep(1000); | 128 PlatformThread::Sleep(TimeDelta::FromSeconds(1)); |
| 128 EXPECT_EQ(0, watchdog.alarm_counter()); | 129 EXPECT_EQ(0, watchdog.alarm_counter()); |
| 129 | 130 |
| 130 // ...but even after disarming, we can still use the alarm... | 131 // ...but even after disarming, we can still use the alarm... |
| 131 // Set a time greater than the timeout into the past. | 132 // Set a time greater than the timeout into the past. |
| 132 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(10)); | 133 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(10)); |
| 133 // It should almost instantly go off, but certainly in less than 5 minutes. | 134 // It should almost instantly go off, but certainly in less than 5 minutes. |
| 134 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5), | 135 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5), |
| 135 watchdog.alarm_counter() > 0); | 136 watchdog.alarm_counter() > 0); |
| 136 | 137 |
| 137 EXPECT_EQ(1, watchdog.alarm_counter()); | 138 EXPECT_EQ(1, watchdog.alarm_counter()); |
| 138 } | 139 } |
| 139 | 140 |
| 140 } // namespace base | 141 } // namespace base |
| OLD | NEW |