blob: c74b9419fa8177c139271ef36779f11425c6c50a [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.commit3f4a7322008-07-27 06:49:38 +09004
5// Tests for Watchdog class.
6
7#include "base/logging.h"
8#include "base/watchdog.h"
9#include "base/spin_wait.h"
10#include "base/time.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace {
14
15//------------------------------------------------------------------------------
16// Provide a derived class to facilitate testing.
17
18// TODO(JAR): Remove default argument from constructor, and make mandatory.
19class WatchdogCounter : public Watchdog {
20 public:
21 WatchdogCounter(const TimeDelta& duration,
22 const std::wstring& thread_watched_name,
23 bool enabled = true)
24 : Watchdog(duration, thread_watched_name, enabled), alarm_counter_(0) {
25 }
26
27 virtual ~WatchdogCounter() {}
28
29 virtual void Alarm() {
30 alarm_counter_++;
31 Watchdog::Alarm();
32 }
33
34 int alarm_counter() { return alarm_counter_; }
35
36 private:
37 int alarm_counter_;
38
39 DISALLOW_EVIL_CONSTRUCTORS(WatchdogCounter);
40};
41
42class WatchdogTest : public testing::Test {
43};
44
45
46//------------------------------------------------------------------------------
47// Actual tests
48
49// Minimal constructor/destructor test.
50TEST(WatchdogTest, StartupShutdownTest) {
51 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), L"Disabled", false);
52 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), L"Enabled", true);
53
54 // The following test is depricated, and should be removed when the
55 // default argument constructor is no longer accepted.
56 Watchdog watchdog3(TimeDelta::FromMilliseconds(300), L"Default");
57}
58
59// Test ability to call Arm and Disarm repeatedly.
60TEST(WatchdogTest, ArmDisarmTest) {
61 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), L"Disabled", false);
62 watchdog1.Arm();
63 watchdog1.Disarm();
64 watchdog1.Arm();
65 watchdog1.Disarm();
66
67 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), L"Enabled", true);
68 watchdog2.Arm();
69 watchdog2.Disarm();
70 watchdog2.Arm();
71 watchdog2.Disarm();
72
73 // The following test is depricated, and should be removed when the
74 // default argument constructor is no longer accepted.
75 Watchdog watchdog3(TimeDelta::FromMilliseconds(300), L"Default");
76 watchdog3.Arm();
77 watchdog3.Disarm();
78 watchdog3.Arm();
79 watchdog3.Disarm();
80}
81
82// Make sure a basic alarm fires when the time has expired.
83TEST(WatchdogTest, AlarmTest) {
84 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), L"Enabled", true);
85 watchdog.Arm();
86 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1),
87 watchdog.alarm_counter() > 0);
88 EXPECT_EQ(1, watchdog.alarm_counter());
89
90 // Set a time greater than the timeout into the past.
91 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2));
92 // It should instantly go off, but certainly in less than a second.
93 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1),
94 watchdog.alarm_counter() > 1);
95
96 EXPECT_EQ(2, watchdog.alarm_counter());
97}
98
99// Make sure a disable alarm does nothing, even if we arm it.
100TEST(WatchdogTest, ConstructorDisabledTest) {
101 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), L"Disabled", false);
102 watchdog.Arm();
103 // Alarm should not fire, as it was disabled.
104 Sleep(500);
105 EXPECT_EQ(0, watchdog.alarm_counter());
106}
107
108// Make sure Disarming will prevent firing, even after Arming.
109TEST(WatchdogTest, DisarmTest) {
110 WatchdogCounter watchdog(TimeDelta::FromSeconds(1), L"Enabled", true);
111 watchdog.Arm();
112 Sleep(100); // Don't sleep too long
113 watchdog.Disarm();
114 // Alarm should not fire.
115 Sleep(1500);
116 EXPECT_EQ(0, watchdog.alarm_counter());
117
118 // ...but even after disarming, we can still use the alarm...
119 // Set a time greater than the timeout into the past.
120 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2));
121 // It should almost instantly go off, but certainly in less than a second.
122 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1),
123 watchdog.alarm_counter() > 0);
124
125 EXPECT_EQ(1, watchdog.alarm_counter());
126}
127
128} // namespace
license.botf003cfe2008-08-24 09:55:55 +0900129