This source file includes following definitions.
- alarm_counter_
- Alarm
- alarm_counter
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/threading/watchdog.h"
#include "base/logging.h"
#include "base/synchronization/spin_wait.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
class WatchdogCounter : public Watchdog {
public:
WatchdogCounter(const TimeDelta& duration,
const std::string& thread_watched_name,
bool enabled)
: Watchdog(duration, thread_watched_name, enabled),
alarm_counter_(0) {
}
virtual ~WatchdogCounter() {}
virtual void Alarm() OVERRIDE {
alarm_counter_++;
Watchdog::Alarm();
}
int alarm_counter() { return alarm_counter_; }
private:
int alarm_counter_;
DISALLOW_COPY_AND_ASSIGN(WatchdogCounter);
};
class WatchdogTest : public testing::Test {
public:
virtual void SetUp() OVERRIDE {
Watchdog::ResetStaticData();
}
};
}
TEST_F(WatchdogTest, StartupShutdownTest) {
Watchdog watchdog1(TimeDelta::FromMilliseconds(300), "Disabled", false);
Watchdog watchdog2(TimeDelta::FromMilliseconds(300), "Enabled", true);
}
TEST_F(WatchdogTest, ArmDisarmTest) {
Watchdog watchdog1(TimeDelta::FromMilliseconds(300), "Disabled", false);
watchdog1.Arm();
watchdog1.Disarm();
watchdog1.Arm();
watchdog1.Disarm();
Watchdog watchdog2(TimeDelta::FromMilliseconds(300), "Enabled", true);
watchdog2.Arm();
watchdog2.Disarm();
watchdog2.Arm();
watchdog2.Disarm();
}
TEST_F(WatchdogTest, AlarmTest) {
WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Enabled", true);
watchdog.Arm();
SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5),
watchdog.alarm_counter() > 0);
EXPECT_EQ(1, watchdog.alarm_counter());
}
TEST_F(WatchdogTest, AlarmPriorTimeTest) {
WatchdogCounter watchdog(TimeDelta(), "Enabled2", true);
watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(2));
SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5),
watchdog.alarm_counter() > 0);
EXPECT_EQ(1, watchdog.alarm_counter());
}
TEST_F(WatchdogTest, ConstructorDisabledTest) {
WatchdogCounter watchdog(TimeDelta::FromMilliseconds(10), "Disabled", false);
watchdog.Arm();
PlatformThread::Sleep(TimeDelta::FromMilliseconds(500));
EXPECT_EQ(0, watchdog.alarm_counter());
}
TEST_F(WatchdogTest, DisarmTest) {
WatchdogCounter watchdog(TimeDelta::FromSeconds(1), "Enabled3", true);
TimeTicks start = TimeTicks::Now();
watchdog.Arm();
PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
watchdog.Disarm();
TimeTicks end = TimeTicks::Now();
if (end - start > TimeDelta::FromMilliseconds(500)) {
LOG(WARNING) << "100ms sleep took over 500ms, making the results of this "
<< "timing-sensitive test suspicious. Aborting now.";
return;
}
EXPECT_EQ(0, watchdog.alarm_counter());
PlatformThread::Sleep(TimeDelta::FromSeconds(1));
EXPECT_EQ(0, watchdog.alarm_counter());
watchdog.ArmSomeTimeDeltaAgo(TimeDelta::FromSeconds(10));
SPIN_FOR_TIMEDELTA_OR_UNTIL_TRUE(TimeDelta::FromMinutes(5),
watchdog.alarm_counter() > 0);
EXPECT_EQ(1, watchdog.alarm_counter());
}
}