blob: 87166f41a7ac261ce0e19b9047eb17c442fe1eb0 [file] [log] [blame]
William A. Kennington IIIaf60e632019-01-16 15:00:18 -08001#include "../watchdog.hpp"
2
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +05303#include <chrono>
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -08004#include <memory>
William A. Kennington IIIf505fc02018-09-12 18:30:09 -07005#include <sdbusplus/bus.hpp>
6#include <sdeventplus/event.hpp>
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +05307
William A. Kennington IIIf505fc02018-09-12 18:30:09 -07008#include <gtest/gtest.h>
9
Ofer Yehiellic35135d2019-06-14 11:30:25 -070010namespace phosphor
11{
12namespace watchdog
13{
14
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053015using namespace std::chrono;
16using namespace std::chrono_literals;
17
Ofer Yehiellic35135d2019-06-14 11:30:25 -070018constexpr auto TEST_MIN_INTERVAL = duration<uint64_t, std::deci>(2);
19
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053020// Test Watchdog functionality
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070021class WdogTest : public ::testing::Test
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053022{
Patrick Venture8f6c5152018-09-11 17:45:33 -070023 public:
William A. Kennington III3964f002019-01-16 15:43:43 -080024 // The unit time used to measure the timer
25 // This should be large enough to accomodate drift
William A. Kennington III5e350792019-01-16 15:46:01 -080026 using Quantum = duration<uint64_t, std::deci>;
William A. Kennington III3964f002019-01-16 15:43:43 -080027
Patrick Venture8f6c5152018-09-11 17:45:33 -070028 // Gets called as part of each TEST_F construction
29 WdogTest() :
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070030 event(sdeventplus::Event::get_default()),
Patrick Venture8f6c5152018-09-11 17:45:33 -070031 bus(sdbusplus::bus::new_default()),
Ofer Yehiellic35135d2019-06-14 11:30:25 -070032 wdog(std::make_unique<Watchdog>(
33 bus, TEST_PATH, event, Watchdog::ActionTargetMap(), std::nullopt,
34 milliseconds(TEST_MIN_INTERVAL).count())),
35
William A. Kennington III3964f002019-01-16 15:43:43 -080036 defaultInterval(Quantum(3))
Ofer Yehiellic35135d2019-06-14 11:30:25 -070037
Patrick Venture8f6c5152018-09-11 17:45:33 -070038 {
William A. Kennington III3964f002019-01-16 15:43:43 -080039 wdog->interval(milliseconds(defaultInterval).count());
Patrick Venture8f6c5152018-09-11 17:45:33 -070040 // Initially the watchdog would be disabled
41 EXPECT_FALSE(wdog->enabled());
42 }
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053043
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070044 // sdevent Event handle
45 sdeventplus::Event event;
46
Patrick Venture8f6c5152018-09-11 17:45:33 -070047 // sdbusplus handle
48 sdbusplus::bus::bus bus;
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053049
Patrick Venture8f6c5152018-09-11 17:45:33 -070050 // Watchdog object
Ofer Yehiellic35135d2019-06-14 11:30:25 -070051 std::unique_ptr<Watchdog> wdog;
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053052
Patrick Venture8f6c5152018-09-11 17:45:33 -070053 // This is the default interval as given in Interface definition
William A. Kennington III3964f002019-01-16 15:43:43 -080054 Quantum defaultInterval;
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053055
Patrick Venture8f6c5152018-09-11 17:45:33 -070056 protected:
57 // Dummy name for object path
58 // This is just to satisfy the constructor. Does not have
59 // a need to check if the objects paths have been created.
60 static constexpr auto TEST_PATH = "/test/path";
William A. Kennington III99c69de2018-03-01 10:59:22 -080061
Patrick Venture8f6c5152018-09-11 17:45:33 -070062 // Returns how long it took for the current watchdog timer to be
63 // disabled or have its timeRemaining reset.
William A. Kennington III3964f002019-01-16 15:43:43 -080064 Quantum waitForWatchdog(Quantum timeLimit);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053065};
Ofer Yehiellic35135d2019-06-14 11:30:25 -070066
67} // namespace watchdog
68} // namespace phosphor