Convert to using sdeventplus
This gets rid of the ad-hoc timer class in favor of using the timer
source built into sdeventplus.
Tested:
Unit tests pass and everything still builds. Manually verified the
functionality is still in tact on a zaius machine.
Change-Id: I90619f0fe5a9fdfcecd24a49de672c0c99dc95e9
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index 48b3559..1c44162 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -5,12 +5,12 @@
# Build/add utest to test suite
check_PROGRAMS = argument_test \
- timer_test \
watchdog_test
utestCPPFLAGS = $(GTEST_MAIN_CFLAGS) \
$(AM_CPPFLAGS) \
$(SDBUSPLUS_CFLAGS) \
+ $(SDEVENTPLUS_CFLAGS) \
$(PHOSPHOR_LOGGING_CFLAGS) \
$(PHOSPHOR_DBUS_INTERFACES_CFLAGS)
@@ -20,6 +20,7 @@
$(PTHREAD_LIBS) \
$(OESDK_TESTCASE_FLAGS) \
$(SDBUSPLUS_LIBS) \
+ $(SDEVENTPLUS_LIBS) \
$(PHOSPHOR_LOGGING_LIBS) \
$(PHOSPHOR_DBUS_INTERFACES_LIBS)
@@ -27,14 +28,9 @@
argument_test_CXXFLAGS = ${utestCXXFLAGS}
argument_test_LDFLAGS = ${utestLDFLAGS}
-timer_test_CPPFLAGS = ${utestCPPFLAGS}
-timer_test_CXXFLAGS = ${utestCXXFLAGS}
-timer_test_LDFLAGS = ${utestLDFLAGS}
-
watchdog_test_CPPFLAGS = ${utestCPPFLAGS}
watchdog_test_CXXFLAGS = ${utestCXXFLAGS}
watchdog_test_LDFLAGS = ${utestLDFLAGS}
argument_test_SOURCES = ../argument.cpp argument_test.cpp
-timer_test_SOURCES = ../timer.cpp timer_test.cpp
-watchdog_test_SOURCES = ../timer.cpp ../watchdog.cpp watchdog_test.cpp
+watchdog_test_SOURCES = ../watchdog.cpp watchdog_test.cpp
diff --git a/test/timer_test.cpp b/test/timer_test.cpp
deleted file mode 100644
index ea00228..0000000
--- a/test/timer_test.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-#include <chrono>
-#include <timer_test.hpp>
-
-using namespace std::chrono;
-using namespace std::chrono_literals;
-
-/** @brief Starts the timer and expects it to
- * expire in configured time and expects the
- * deault callback handler to kick-in
- */
-TEST_F(TimerTest, testTimerForExpirationDefaultTimeoutHandler)
-{
- // Expect timer to expire in 2 seconds
- auto expireTime = seconds(2s);
-
- phosphor::watchdog::Timer timer(eventP);
-
- // Set the expiration and enable the timer
- timer.start(duration_cast<milliseconds>(expireTime));
- timer.setEnabled<std::true_type>();
-
- // Waiting 2 seconds to expect expiration
- int count = 0;
- while (count < expireTime.count() && !timer.expired())
- {
- // Returns -0- on timeout and positive number on dispatch
- auto sleepTime = duration_cast<microseconds>(seconds(1));
- if (!sd_event_run(eventP.get(), sleepTime.count()))
- {
- count++;
- }
- }
- EXPECT_TRUE(timer.expired());
- EXPECT_EQ(expireTime.count() - 1, count);
-
- // Make sure secondary callback was not called.
- EXPECT_FALSE(expired);
-}
-
-/** @brief Starts the timer and expects it to expire
- * in configured time and expects the secondary
- * callback to be called into along with default.
- */
-TEST_F(TimerTest, testTimerForExpirationSecondCallBack)
-{
- // Expect timer to expire in 2 seconds
- auto expireTime = seconds(2s);
-
- phosphor::watchdog::Timer timer(
- eventP, std::bind(&TimerTest::timeOutHandler, this));
-
- // Set the expiration and enable the timer
- timer.start(duration_cast<milliseconds>(expireTime));
- timer.setEnabled<std::true_type>();
-
- // Waiting 2 seconds to expect expiration
- int count = 0;
- while (count < expireTime.count() && !timer.expired())
- {
- // Returns -0- on timeout and positive number on dispatch
- auto sleepTime = duration_cast<microseconds>(seconds(1));
- if (!sd_event_run(eventP.get(), sleepTime.count()))
- {
- count++;
- }
- }
- EXPECT_TRUE(timer.expired());
- EXPECT_EQ(expireTime.count() - 1, count);
-
- // This gets set as part of secondary callback
- EXPECT_TRUE(expired);
-}
diff --git a/test/timer_test.hpp b/test/timer_test.hpp
deleted file mode 100644
index cab9086..0000000
--- a/test/timer_test.hpp
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <iostream>
-#include <timer.hpp>
-
-#include <gtest/gtest.h>
-
-// Base class for testing Timer
-class TimerTest : public testing::Test
-{
- public:
- // systemd event handler
- sd_event* events;
-
- // Need this so that events can be initialized.
- int rc;
-
- // Tells if the watchdog timer expired.
- bool expired = false;
-
- // Gets called as part of each TEST_F construction
- TimerTest() : rc(sd_event_default(&events)), eventP(events)
- {
- // Check for successful creation of
- // event handler and bus handler
- EXPECT_GE(rc, 0);
-
- // Its already wrapped in eventP
- events = nullptr;
- }
-
- // unique_ptr for sd_event
- phosphor::watchdog::EventPtr eventP;
-
- // Handler called by timer expiration
- inline void timeOutHandler()
- {
- std::cout << "Time out handler called" << std::endl;
- expired = true;
- }
-};
diff --git a/test/watchdog_test.cpp b/test/watchdog_test.cpp
index aa0ae0f..06b43b8 100644
--- a/test/watchdog_test.cpp
+++ b/test/watchdog_test.cpp
@@ -1,7 +1,7 @@
#include "watchdog_test.hpp"
-#include <chrono>
#include <memory>
+#include <thread>
#include <utility>
using namespace phosphor::watchdog;
@@ -15,9 +15,8 @@
{
previousTimeRemaining = wdog->timeRemaining();
- // Returns -0- on timeout and positive number on dispatch
- auto sleepTime = 1s;
- if (!sd_event_run(eventP.get(), microseconds(sleepTime).count()))
+ constexpr auto sleepTime = 1s;
+ if (event.run(sleepTime) == 0)
{
ret += sleepTime;
}
@@ -128,6 +127,10 @@
// Sleep for 1 second
std::this_thread::sleep_for(1s);
+ // Timer should still be running unexpired
+ EXPECT_FALSE(wdog->timerExpired());
+ EXPECT_TRUE(wdog->timerEnabled());
+
// Next timer will expire in 5 seconds from now.
auto expireTime = 5s;
auto expireTimeMs = milliseconds(expireTime).count();
@@ -137,9 +140,6 @@
EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime));
EXPECT_TRUE(wdog->timerExpired());
EXPECT_FALSE(wdog->timerEnabled());
-
- // Make sure secondary callback was not called.
- EXPECT_FALSE(expired);
}
/** @brief Make sure the Interval can be updated directly.
@@ -218,7 +218,7 @@
.action = Watchdog::Action::PowerOff,
.interval = static_cast<uint64_t>(fallbackIntervalMs),
};
- wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP,
+ wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event,
Watchdog::ActionTargetMap(),
std::move(fallback));
EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs)));
@@ -301,7 +301,7 @@
.interval = static_cast<uint64_t>(fallbackIntervalMs),
.always = false,
};
- wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP,
+ wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event,
Watchdog::ActionTargetMap(),
std::move(fallback));
EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs)));
@@ -355,7 +355,7 @@
.interval = static_cast<uint64_t>(fallbackIntervalMs),
.always = true,
};
- wdog = std::make_unique<Watchdog>(bus, TEST_PATH, eventP,
+ wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event,
Watchdog::ActionTargetMap(),
std::move(fallback));
EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs)));
diff --git a/test/watchdog_test.hpp b/test/watchdog_test.hpp
index 50fa8f1..4c798a7 100644
--- a/test/watchdog_test.hpp
+++ b/test/watchdog_test.hpp
@@ -1,30 +1,33 @@
#include <chrono>
#include <memory>
-#include <timer_test.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdeventplus/event.hpp>
#include <watchdog.hpp>
+#include <gtest/gtest.h>
+
using namespace std::chrono;
using namespace std::chrono_literals;
// Test Watchdog functionality
-class WdogTest : public TimerTest
+class WdogTest : public ::testing::Test
{
public:
// Gets called as part of each TEST_F construction
WdogTest() :
+ event(sdeventplus::Event::get_default()),
bus(sdbusplus::bus::new_default()),
wdog(std::make_unique<phosphor::watchdog::Watchdog>(bus, TEST_PATH,
- eventP)),
+ event)),
defaultInterval(milliseconds(wdog->interval())), defaultDrift(30)
{
- // Check for successful creation of
- // event handler and bus handler
- EXPECT_GE(rc, 0);
-
// Initially the watchdog would be disabled
EXPECT_FALSE(wdog->enabled());
}
+ // sdevent Event handle
+ sdeventplus::Event event;
+
// sdbusplus handle
sdbusplus::bus::bus bus;