event: Add watchdog functions
diff --git a/src/sdeventplus/event.cpp b/src/sdeventplus/event.cpp
index a955f41..b472602 100644
--- a/src/sdeventplus/event.cpp
+++ b/src/sdeventplus/event.cpp
@@ -51,4 +51,24 @@
return r;
}
+int Event::get_watchdog()
+{
+ int r = intf->sd_event_get_watchdog(event.get());
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_get_watchdog");
+ }
+ return r;
+}
+
+int Event::set_watchdog(int b)
+{
+ int r = intf->sd_event_set_watchdog(event.get(), b);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_set_watchdog");
+ }
+ return r;
+}
+
} // namespace sdeventplus
diff --git a/src/sdeventplus/event.hpp b/src/sdeventplus/event.hpp
index 9c72de2..9206ee9 100644
--- a/src/sdeventplus/event.hpp
+++ b/src/sdeventplus/event.hpp
@@ -17,6 +17,8 @@
static Event get_default(SdEventInterface* intf = &sdevent_impl);
int loop();
+ int get_watchdog();
+ int set_watchdog(int b);
private:
SdEventInterface* intf;
diff --git a/src/sdeventplus/internal/sdevent.hpp b/src/sdeventplus/internal/sdevent.hpp
index fd4bd77..670d46a 100644
--- a/src/sdeventplus/internal/sdevent.hpp
+++ b/src/sdeventplus/internal/sdevent.hpp
@@ -16,6 +16,8 @@
virtual sd_event *sd_event_unref(sd_event *event) const = 0;
virtual int sd_event_loop(sd_event *event) const = 0;
+ virtual int sd_event_get_watchdog(sd_event *event) const = 0;
+ virtual int sd_event_set_watchdog(sd_event *event, int b) const = 0;
virtual sd_event_source *
sd_event_source_ref(sd_event_source *source) const = 0;
@@ -51,6 +53,16 @@
return ::sd_event_loop(event);
}
+ int sd_event_get_watchdog(sd_event *event) const override
+ {
+ return ::sd_event_get_watchdog(event);
+ }
+
+ int sd_event_set_watchdog(sd_event *event, int b) const override
+ {
+ return ::sd_event_set_watchdog(event, b);
+ }
+
sd_event_source *sd_event_source_ref(sd_event_source *source) const override
{
return ::sd_event_source_ref(source);
diff --git a/src/sdeventplus/test/sdevent.hpp b/src/sdeventplus/test/sdevent.hpp
index c43b983..e85b13e 100644
--- a/src/sdeventplus/test/sdevent.hpp
+++ b/src/sdeventplus/test/sdevent.hpp
@@ -16,6 +16,8 @@
MOCK_CONST_METHOD1(sd_event_unref, sd_event *(sd_event *));
MOCK_CONST_METHOD1(sd_event_loop, int(sd_event *));
+ MOCK_CONST_METHOD1(sd_event_get_watchdog, int(sd_event *));
+ MOCK_CONST_METHOD2(sd_event_set_watchdog, int(sd_event *, int b));
MOCK_CONST_METHOD1(sd_event_source_ref,
sd_event_source *(sd_event_source *));
diff --git a/test/event.cpp b/test/event.cpp
index da935cf..1c30d20 100644
--- a/test/event.cpp
+++ b/test/event.cpp
@@ -1,5 +1,6 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <memory>
#include <sdeventplus/event.hpp>
#include <sdeventplus/exception.hpp>
#include <sdeventplus/test/sdevent.hpp>
@@ -67,14 +68,31 @@
EXPECT_THROW(Event::get_default(&mock), SdEventError);
}
-TEST_F(EventTest, LoopSuccess)
+class EventMethodTest : public EventTest
+{
+ protected:
+ std::unique_ptr<Event> event;
+
+ void SetUp()
+ {
+ event =
+ std::make_unique<Event>(expected_event, std::false_type(), &mock);
+ }
+
+ void TearDown()
+ {
+ EXPECT_CALL(mock, sd_event_unref(expected_event))
+ .WillOnce(Return(nullptr));
+ }
+};
+
+TEST_F(EventMethodTest, LoopSuccess)
{
EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(0));
- EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
- EXPECT_EQ(0, Event(expected_event, std::false_type(), &mock).loop());
+ EXPECT_EQ(0, event->loop());
}
-TEST_F(EventTest, LoopUserError)
+TEST_F(EventMethodTest, LoopUserError)
{
const int user_error = 10;
EXPECT_CALL(mock, sd_event_loop(expected_event))
@@ -84,12 +102,46 @@
Event(expected_event, std::false_type(), &mock).loop());
}
-TEST_F(EventTest, LoopInternalError)
+TEST_F(EventMethodTest, LoopInternalError)
{
EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(-EINVAL));
- EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
- EXPECT_THROW(Event(expected_event, std::false_type(), &mock).loop(),
- SdEventError);
+ EXPECT_THROW(event->loop(), SdEventError);
+}
+
+TEST_F(EventMethodTest, GetWatchdogSuccess)
+{
+ EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
+ .WillOnce(Return(0));
+ EXPECT_EQ(0, event->get_watchdog());
+
+ EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
+ .WillOnce(Return(2));
+ EXPECT_EQ(2, event->get_watchdog());
+}
+
+TEST_F(EventMethodTest, GetWatchdogError)
+{
+ EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(event->get_watchdog(), SdEventError);
+}
+
+TEST_F(EventMethodTest, SetWatchdogSuccess)
+{
+ EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 0))
+ .WillOnce(Return(0));
+ EXPECT_EQ(0, event->set_watchdog(0));
+
+ EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 1))
+ .WillOnce(Return(2));
+ EXPECT_EQ(2, event->set_watchdog(1));
+}
+
+TEST_F(EventMethodTest, SetWatchdogError)
+{
+ EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 1))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(event->set_watchdog(1), SdEventError);
}
} // namespace