Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: base/watchdog_unittest.cc

Issue 11326: Port base/watchdog to Linux. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // 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 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 // Tests for Watchdog class. 5 // Tests for Watchdog class.
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/watchdog.h" 8 #include "base/platform_thread.h"
9 #include "base/spin_wait.h" 9 #include "base/spin_wait.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "base/watchdog.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 using base::TimeDelta; 14 using base::TimeDelta;
14 15
15 namespace { 16 namespace {
16 17
17 //------------------------------------------------------------------------------ 18 //------------------------------------------------------------------------------
18 // Provide a derived class to facilitate testing. 19 // Provide a derived class to facilitate testing.
19 20
20 // TODO(JAR): Remove default argument from constructor, and make mandatory.
21 class WatchdogCounter : public Watchdog { 21 class WatchdogCounter : public Watchdog {
22 public: 22 public:
23 WatchdogCounter(const TimeDelta& duration, 23 WatchdogCounter(const TimeDelta& duration,
24 const std::wstring& thread_watched_name, 24 const std::string& thread_watched_name,
25 bool enabled = true) 25 bool enabled)
26 : Watchdog(duration, thread_watched_name, enabled), alarm_counter_(0) { 26 : Watchdog(duration, thread_watched_name, enabled), alarm_counter_(0) {
27 } 27 }
28 28
29 virtual ~WatchdogCounter() {} 29 virtual ~WatchdogCounter() {}
30 30
31 virtual void Alarm() { 31 virtual void Alarm() {
32 alarm_counter_++; 32 alarm_counter_++;
33 Watchdog::Alarm(); 33 Watchdog::Alarm();
34 } 34 }
35 35
36 int alarm_counter() { return alarm_counter_; } 36 int alarm_counter() { return alarm_counter_; }
37 37
38 private: 38 private:
39 int alarm_counter_; 39 int alarm_counter_;
40 40
41 DISALLOW_EVIL_CONSTRUCTORS(WatchdogCounter); 41 DISALLOW_COPY_AND_ASSIGN(WatchdogCounter);
42 }; 42 };
43 43
44 class WatchdogTest : public testing::Test { 44 class WatchdogTest : public testing::Test {
45 }; 45 };
46 46
47 47
48 //------------------------------------------------------------------------------ 48 //------------------------------------------------------------------------------
49 // Actual tests 49 // Actual tests
50 50
51 // Minimal constructor/destructor test. 51 // Minimal constructor/destructor test.
52 TEST(WatchdogTest, StartupShutdownTest) { 52 TEST(WatchdogTest, StartupShutdownTest) {
53 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), L"Disabled", false); 53 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), "Disabled", false);
54 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), L"Enabled", true); 54 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), "Enabled", true);
55
56 // The following test is depricated, and should be removed when the
57 // default argument constructor is no longer accepted.
58 Watchdog watchdog3(TimeDelta::FromMilliseconds(300), L"Default");
59 } 55 }
60 56
61 // Test ability to call Arm and Disarm repeatedly. 57 // Test ability to call Arm and Disarm repeatedly.
62 TEST(WatchdogTest, ArmDisarmTest) { 58 TEST(WatchdogTest, ArmDisarmTest) {
63 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), L"Disabled", false); 59 Watchdog watchdog1(TimeDelta::FromMilliseconds(300), "Disabled", false);
64 watchdog1.Arm(); 60 watchdog1.Arm();
65 watchdog1.Disarm(); 61 watchdog1.Disarm();
66 watchdog1.Arm(); 62 watchdog1.Arm();
67 watchdog1.Disarm(); 63 watchdog1.Disarm();
68 64
69 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), L"Enabled", true); 65 Watchdog watchdog2(TimeDelta::FromMilliseconds(300), "Enabled", true);
70 watchdog2.Arm(); 66 watchdog2.Arm();
71 watchdog2.Disarm(); 67 watchdog2.Disarm();
72 watchdog2.Arm(); 68 watchdog2.Arm();
73 watchdog2.Disarm(); 69 watchdog2.Disarm();
74
75 // The following test is depricated, and should be removed when the
76 // default argument constructor is no longer accepted.
77 Watchdog watchdog3(TimeDelta::FromMilliseconds(300), L"Default");
78 watchdog3.Arm();
79 watchdog3.Disarm();
80 watchdog3.Arm();
81 watchdog3.Disarm();
82 } 70 }
83 71
84 // Make sure a basic alarm fires when the time has expired. 72 // Make sure a basic alarm fires when the time has expired.
85 TEST(WatchdogTest, AlarmTest) { 73 TEST(WatchdogTest, AlarmTest) {
86 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), L"Enabled", true); 74 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Enabled", true);
87 watchdog.Arm(); 75 watchdog.Arm();
88 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1), 76 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1),
89 watchdog.alarm_counter() > 0); 77 watchdog.alarm_counter() > 0);
90 EXPECT_EQ(1, watchdog.alarm_counter()); 78 EXPECT_EQ(1, watchdog.alarm_counter());
91 79
92 // Set a time greater than the timeout into the past. 80 // Set a time greater than the timeout into the past.
93 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2)); 81 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2));
94 // It should instantly go off, but certainly in less than a second. 82 // It should instantly go off, but certainly in less than a second.
95 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1), 83 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1),
96 watchdog.alarm_counter() > 1); 84 watchdog.alarm_counter() > 1);
97 85
98 EXPECT_EQ(2, watchdog.alarm_counter()); 86 EXPECT_EQ(2, watchdog.alarm_counter());
99 } 87 }
100 88
101 // Make sure a disable alarm does nothing, even if we arm it. 89 // Make sure a disable alarm does nothing, even if we arm it.
102 TEST(WatchdogTest, ConstructorDisabledTest) { 90 TEST(WatchdogTest, ConstructorDisabledTest) {
103 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), L"Disabled", false); 91 WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Disabled", false);
104 watchdog.Arm(); 92 watchdog.Arm();
105 // Alarm should not fire, as it was disabled. 93 // Alarm should not fire, as it was disabled.
106 Sleep(500); 94 PlatformThread::Sleep(500);
107 EXPECT_EQ(0, watchdog.alarm_counter()); 95 EXPECT_EQ(0, watchdog.alarm_counter());
108 } 96 }
109 97
110 // Make sure Disarming will prevent firing, even after Arming. 98 // Make sure Disarming will prevent firing, even after Arming.
111 TEST(WatchdogTest, DisarmTest) { 99 TEST(WatchdogTest, DisarmTest) {
112 WatchdogCounter watchdog(TimeDelta::FromSeconds(1), L"Enabled", true); 100 WatchdogCounter watchdog(TimeDelta::FromSeconds(1), "Enabled", true);
113 watchdog.Arm(); 101 watchdog.Arm();
114 Sleep(100); // Don't sleep too long 102 PlatformThread::Sleep(100); // Don't sleep too long
115 watchdog.Disarm(); 103 watchdog.Disarm();
116 // Alarm should not fire. 104 // Alarm should not fire.
117 Sleep(1500); 105 PlatformThread::Sleep(1500);
118 EXPECT_EQ(0, watchdog.alarm_counter()); 106 EXPECT_EQ(0, watchdog.alarm_counter());
119 107
120 // ...but even after disarming, we can still use the alarm... 108 // ...but even after disarming, we can still use the alarm...
121 // Set a time greater than the timeout into the past. 109 // Set a time greater than the timeout into the past.
122 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2)); 110 watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2));
123 // It should almost instantly go off, but certainly in less than a second. 111 // It should almost instantly go off, but certainly in less than a second.
124 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1), 112 SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromSeconds(1),
125 watchdog.alarm_counter() > 0); 113 watchdog.alarm_counter() > 0);
126 114
127 EXPECT_EQ(1, watchdog.alarm_counter()); 115 EXPECT_EQ(1, watchdog.alarm_counter());
128 } 116 }
129 117
130 } // namespace 118 } // namespace
131 119
OLDNEW
« base/watchdog.h ('K') | « base/watchdog.cc ('k') | chrome/browser/jankometer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698