blob: 3ecfa7d27102bdae7c9ec725414eee33abf4fc0f [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commitd7cae122008-07-26 21:49:384
5// Tests for Watchdog class.
6
[email protected]95ab1552008-11-25 22:46:597#include "base/platform_thread.h"
initial.commitd7cae122008-07-26 21:49:388#include "base/spin_wait.h"
9#include "base/time.h"
[email protected]95ab1552008-11-25 22:46:5910#include "base/watchdog.h"
initial.commitd7cae122008-07-26 21:49:3811#include "testing/gtest/include/gtest/gtest.h"
12
[email protected]e1acf6f2008-10-27 20:43:3313using base::TimeDelta;
14
initial.commitd7cae122008-07-26 21:49:3815namespace {
16
17//------------------------------------------------------------------------------
18// Provide a derived class to facilitate testing.
19
initial.commitd7cae122008-07-26 21:49:3820class WatchdogCounter : public Watchdog {
21 public:
22 WatchdogCounter(const TimeDelta& duration,
[email protected]95ab1552008-11-25 22:46:5923 const std::string& thread_watched_name,
24 bool enabled)
initial.commitd7cae122008-07-26 21:49:3825 : Watchdog(duration, thread_watched_name, enabled), alarm_counter_(0) {
26 }
27
28 virtual ~WatchdogCounter() {}
29
30 virtual void Alarm() {
31 alarm_counter_++;
32 Watchdog::Alarm();
33 }
34
35 int alarm_counter() { return alarm_counter_; }
36
37 private:
38 int alarm_counter_;
39
[email protected]95ab1552008-11-25 22:46:5940 DISALLOW_COPY_AND_ASSIGN(WatchdogCounter);
initial.commitd7cae122008-07-26 21:49:3841};
42
43class WatchdogTest : public testing::Test {
[email protected]fb48b6ef2009-07-28 20:37:5544 public:
45 void SetUp() {
46 Watchdog::ResetStaticData();
47 }
initial.commitd7cae122008-07-26 21:49:3848};
49
50
51//------------------------------------------------------------------------------
52// Actual tests
53
54// Minimal constructor/destructor test.
[email protected]fb48b6ef2009-07-28 20:37:5555TEST_F(WatchdogTest, StartupShutdownTest) {
[email protected]95ab1552008-11-25 22:46:5956 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), "Disabled", false);
57 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), "Enabled", true);
initial.commitd7cae122008-07-26 21:49:3858}
59
60// Test ability to call Arm and Disarm repeatedly.
[email protected]fb48b6ef2009-07-28 20:37:5561TEST_F(WatchdogTest, ArmDisarmTest) {
[email protected]95ab1552008-11-25 22:46:5962 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), "Disabled", false);
initial.commitd7cae122008-07-26 21:49:3863 watchdog1.Arm();
64 watchdog1.Disarm();
65 watchdog1.Arm();
66 watchdog1.Disarm();
67
[email protected]95ab1552008-11-25 22:46:5968 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), "Enabled", true);
initial.commitd7cae122008-07-26 21:49:3869 watchdog2.Arm();
70 watchdog2.Disarm();
71 watchdog2.Arm();
72 watchdog2.Disarm();
initial.commitd7cae122008-07-26 21:49:3873}
74
75// Make sure a basic alarm fires when the time has expired.
[email protected]fb48b6ef2009-07-28 20:37:5576TEST_F(WatchdogTest, AlarmTest) {
[email protected]95ab1552008-11-25 22:46:5977 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Enabled", true);
initial.commitd7cae122008-07-26 21:49:3878 watchdog.Arm();
[email protected]95afaf6022009-01-08 18:29:2079 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5),
initial.commitd7cae122008-07-26 21:49:3880 watchdog.alarm_counter() > 0);
81 EXPECT_EQ(1, watchdog.alarm_counter());
[email protected]af860292009-06-05 01:34:2582}
initial.commitd7cae122008-07-26 21:49:3883
[email protected]af860292009-06-05 01:34:2584// Make sure a basic alarm fires when the time has expired.
[email protected]fb48b6ef2009-07-28 20:37:5585TEST_F(WatchdogTest, AlarmPriorTimeTest) {
[email protected]af860292009-06-05 01:34:2586 WatchdogCounter watchdog(TimeDelta::TimeDelta(), "Enabled2", true);
87 // Set a time in the past.
initial.commitd7cae122008-07-26 21:49:3888 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2));
[email protected]95afaf6022009-01-08 18:29:2089 // It should instantly go off, but certainly in less than 5 minutes.
90 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5),
[email protected]af860292009-06-05 01:34:2591 watchdog.alarm_counter() > 0);
initial.commitd7cae122008-07-26 21:49:3892
[email protected]af860292009-06-05 01:34:2593 EXPECT_EQ(1, watchdog.alarm_counter());
initial.commitd7cae122008-07-26 21:49:3894}
95
96// Make sure a disable alarm does nothing, even if we arm it.
[email protected]fb48b6ef2009-07-28 20:37:5597TEST_F(WatchdogTest, ConstructorDisabledTest) {
[email protected]95ab1552008-11-25 22:46:5998 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Disabled", false);
initial.commitd7cae122008-07-26 21:49:3899 watchdog.Arm();
100 // Alarm should not fire, as it was disabled.
[email protected]95ab1552008-11-25 22:46:59101 PlatformThread::Sleep(500);
initial.commitd7cae122008-07-26 21:49:38102 EXPECT_EQ(0, watchdog.alarm_counter());
103}
104
105// Make sure Disarming will prevent firing, even after Arming.
[email protected]fb48b6ef2009-07-28 20:37:55106TEST_F(WatchdogTest, DisarmTest) {
[email protected]af860292009-06-05 01:34:25107 WatchdogCounter watchdog(TimeDelta::FromSeconds(5), "Enabled3", true);
initial.commitd7cae122008-07-26 21:49:38108 watchdog.Arm();
[email protected]95ab1552008-11-25 22:46:59109 PlatformThread::Sleep(100); // Don't sleep too long
initial.commitd7cae122008-07-26 21:49:38110 watchdog.Disarm();
111 // Alarm should not fire.
[email protected]95afaf6022009-01-08 18:29:20112 PlatformThread::Sleep(5500);
initial.commitd7cae122008-07-26 21:49:38113 EXPECT_EQ(0, watchdog.alarm_counter());
114
115 // ...but even after disarming, we can still use the alarm...
116 // Set a time greater than the timeout into the past.
117 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2));
[email protected]95afaf6022009-01-08 18:29:20118 // It should almost instantly go off, but certainly in less than 5 minutes.
119 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5),
initial.commitd7cae122008-07-26 21:49:38120 watchdog.alarm_counter() > 0);
121
122 EXPECT_EQ(1, watchdog.alarm_counter());
123}
124
125} // namespace