event: Add trivial functions
diff --git a/src/sdeventplus/event.cpp b/src/sdeventplus/event.cpp
index f65750e..96d4190 100644
--- a/src/sdeventplus/event.cpp
+++ b/src/sdeventplus/event.cpp
@@ -54,6 +54,50 @@
return sdevent;
}
+int Event::prepare() const
+{
+ int r = sdevent->sd_event_prepare(event.get());
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_prepare");
+ }
+ return r;
+}
+
+int Event::wait(MaybeTimeout timeout) const
+{
+ // An unsigned -1 timeout value means infinity in sd_event
+ uint64_t timeout_usec = timeout ? timeout->count() : -1;
+ int r = sdevent->sd_event_wait(event.get(), timeout_usec);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_wait");
+ }
+ return r;
+}
+
+int Event::dispatch() const
+{
+ int r = sdevent->sd_event_dispatch(event.get());
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_dispatch");
+ }
+ return r;
+}
+
+int Event::run(MaybeTimeout timeout) const
+{
+ // An unsigned -1 timeout value means infinity in sd_event
+ uint64_t timeout_usec = timeout ? timeout->count() : -1;
+ int r = sdevent->sd_event_run(event.get(), timeout_usec);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_run");
+ }
+ return r;
+}
+
int Event::loop() const
{
int r = sdevent->sd_event_loop(event.get());
@@ -64,6 +108,27 @@
return r;
}
+int Event::exit(int code) const
+{
+ int r = sdevent->sd_event_exit(event.get(), code);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_exit");
+ }
+ return r;
+}
+
+int Event::get_exit_code() const
+{
+ int code;
+ int r = sdevent->sd_event_get_exit_code(event.get(), &code);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_get_exit_code");
+ }
+ return code;
+}
+
int Event::get_watchdog() const
{
int r = sdevent->sd_event_get_watchdog(event.get());
diff --git a/src/sdeventplus/event.hpp b/src/sdeventplus/event.hpp
index b979cfc..b8ffb97 100644
--- a/src/sdeventplus/event.hpp
+++ b/src/sdeventplus/event.hpp
@@ -1,7 +1,9 @@
#pragma once
+#include <experimental/optional>
#include <sdeventplus/internal/sdevent.hpp>
#include <sdeventplus/internal/sdref.hpp>
+#include <sdeventplus/internal/utils.hpp>
#include <systemd/sd-event.h>
namespace sdeventplus
@@ -10,6 +12,9 @@
class Event
{
public:
+ using Timeout = SdEventDuration;
+ using MaybeTimeout = std::experimental::optional<Timeout>;
+
Event(sd_event* event,
const internal::SdEvent* sdevent = &internal::sdevent_impl);
Event(sd_event* event, std::false_type,
@@ -23,7 +28,14 @@
sd_event* get() const;
const internal::SdEvent* getSdEvent() const;
+ int prepare() const;
+ int wait(MaybeTimeout timeout) const;
+ int dispatch() const;
+ int run(MaybeTimeout timeout) const;
int loop() const;
+ int exit(int code) const;
+
+ int get_exit_code() const;
int get_watchdog() const;
int set_watchdog(int b) const;
diff --git a/src/sdeventplus/internal/sdevent.hpp b/src/sdeventplus/internal/sdevent.hpp
index 4baff05..be8e0a8 100644
--- a/src/sdeventplus/internal/sdevent.hpp
+++ b/src/sdeventplus/internal/sdevent.hpp
@@ -17,11 +17,17 @@
virtual sd_event* sd_event_ref(sd_event* event) const = 0;
virtual sd_event* sd_event_unref(sd_event* event) const = 0;
+ virtual int sd_event_prepare(sd_event* event) const = 0;
+ virtual int sd_event_wait(sd_event* event, uint64_t usec) const = 0;
+ virtual int sd_event_dispatch(sd_event* event) const = 0;
+ virtual int sd_event_run(sd_event* event, uint64_t usec) const = 0;
virtual int sd_event_loop(sd_event* event) const = 0;
+ virtual int sd_event_exit(sd_event* event, int code) const = 0;
virtual int sd_event_now(sd_event* event, clockid_t clock,
uint64_t* usec) const = 0;
+ virtual int sd_event_get_exit_code(sd_event* event, int* code) 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;
@@ -73,17 +79,47 @@
return ::sd_event_unref(event);
}
+ int sd_event_prepare(sd_event* event) const override
+ {
+ return ::sd_event_prepare(event);
+ }
+
+ int sd_event_wait(sd_event* event, uint64_t usec) const override
+ {
+ return ::sd_event_wait(event, usec);
+ }
+
+ int sd_event_dispatch(sd_event* event) const override
+ {
+ return ::sd_event_dispatch(event);
+ }
+
+ int sd_event_run(sd_event* event, uint64_t usec) const override
+ {
+ return ::sd_event_run(event, usec);
+ }
+
int sd_event_loop(sd_event* event) const override
{
return ::sd_event_loop(event);
}
+ int sd_event_exit(sd_event* event, int code) const override
+ {
+ return ::sd_event_exit(event, code);
+ }
+
int sd_event_now(sd_event* event, clockid_t clock,
uint64_t* usec) const override
{
return ::sd_event_now(event, clock, usec);
}
+ int sd_event_get_exit_code(sd_event* event, int* code) const override
+ {
+ return ::sd_event_get_exit_code(event, code);
+ }
+
int sd_event_get_watchdog(sd_event* event) const override
{
return ::sd_event_get_watchdog(event);
diff --git a/src/sdeventplus/test/sdevent.hpp b/src/sdeventplus/test/sdevent.hpp
index 5f0e400..fd05b30 100644
--- a/src/sdeventplus/test/sdevent.hpp
+++ b/src/sdeventplus/test/sdevent.hpp
@@ -17,10 +17,16 @@
MOCK_CONST_METHOD1(sd_event_ref, sd_event*(sd_event*));
MOCK_CONST_METHOD1(sd_event_unref, sd_event*(sd_event*));
+ MOCK_CONST_METHOD1(sd_event_prepare, int(sd_event*));
+ MOCK_CONST_METHOD2(sd_event_wait, int(sd_event*, uint64_t));
+ MOCK_CONST_METHOD1(sd_event_dispatch, int(sd_event*));
+ MOCK_CONST_METHOD2(sd_event_run, int(sd_event*, uint64_t));
MOCK_CONST_METHOD1(sd_event_loop, int(sd_event*));
+ MOCK_CONST_METHOD2(sd_event_exit, int(sd_event*, int));
MOCK_CONST_METHOD3(sd_event_now, int(sd_event*, clockid_t, uint64_t*));
+ MOCK_CONST_METHOD2(sd_event_get_exit_code, int(sd_event*, int*));
MOCK_CONST_METHOD1(sd_event_get_watchdog, int(sd_event*));
MOCK_CONST_METHOD2(sd_event_set_watchdog, int(sd_event*, int b));
diff --git a/test/event.cpp b/test/event.cpp
index 7149ac4..f5547ae 100644
--- a/test/event.cpp
+++ b/test/event.cpp
@@ -94,6 +94,94 @@
}
};
+TEST_F(EventMethodTest, PrepareSuccessNone)
+{
+ EXPECT_CALL(mock, sd_event_prepare(expected_event)).WillOnce(Return(0));
+ EXPECT_EQ(0, event->prepare());
+}
+
+TEST_F(EventMethodTest, PrepareSuccessReady)
+{
+ const int events_ready = 10;
+ EXPECT_CALL(mock, sd_event_prepare(expected_event))
+ .WillOnce(Return(events_ready));
+ EXPECT_EQ(events_ready, event->prepare());
+}
+
+TEST_F(EventMethodTest, PrepareInternalError)
+{
+ EXPECT_CALL(mock, sd_event_prepare(expected_event))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(event->prepare(), SdEventError);
+}
+
+TEST_F(EventMethodTest, WaitSuccessNone)
+{
+ const std::chrono::microseconds timeout{20};
+ EXPECT_CALL(mock, sd_event_wait(expected_event, timeout.count()))
+ .WillOnce(Return(0));
+ EXPECT_EQ(0, event->wait(timeout));
+}
+
+TEST_F(EventMethodTest, WaitSuccessReady)
+{
+ const int events_ready = 10;
+ EXPECT_CALL(mock, sd_event_wait(expected_event, static_cast<uint64_t>(-1)))
+ .WillOnce(Return(events_ready));
+ EXPECT_EQ(events_ready, event->wait(std::experimental::nullopt));
+}
+
+TEST_F(EventMethodTest, WaitInternalError)
+{
+ EXPECT_CALL(mock, sd_event_wait(expected_event, static_cast<uint64_t>(-1)))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(event->wait(std::experimental::nullopt), SdEventError);
+}
+
+TEST_F(EventMethodTest, DispatchInitial)
+{
+ EXPECT_CALL(mock, sd_event_dispatch(expected_event)).WillOnce(Return(0));
+ EXPECT_EQ(0, event->dispatch());
+}
+
+TEST_F(EventMethodTest, DispatchDone)
+{
+ const int done_code = 10;
+ EXPECT_CALL(mock, sd_event_dispatch(expected_event))
+ .WillOnce(Return(done_code));
+ EXPECT_EQ(done_code, event->dispatch());
+}
+
+TEST_F(EventMethodTest, DispatchInternalError)
+{
+ EXPECT_CALL(mock, sd_event_dispatch(expected_event))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(event->dispatch(), SdEventError);
+}
+
+TEST_F(EventMethodTest, RunSuccessNone)
+{
+ const std::chrono::microseconds timeout{20};
+ EXPECT_CALL(mock, sd_event_run(expected_event, timeout.count()))
+ .WillOnce(Return(0));
+ EXPECT_EQ(0, event->run(timeout));
+}
+
+TEST_F(EventMethodTest, RunSuccessReady)
+{
+ const int events_ready = 10;
+ EXPECT_CALL(mock, sd_event_run(expected_event, static_cast<uint64_t>(-1)))
+ .WillOnce(Return(events_ready));
+ EXPECT_EQ(events_ready, event->run(std::experimental::nullopt));
+}
+
+TEST_F(EventMethodTest, RunInternalError)
+{
+ EXPECT_CALL(mock, sd_event_run(expected_event, static_cast<uint64_t>(-1)))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(event->run(std::experimental::nullopt), SdEventError);
+}
+
TEST_F(EventMethodTest, LoopSuccess)
{
EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(0));
@@ -114,6 +202,45 @@
EXPECT_THROW(event->loop(), SdEventError);
}
+TEST_F(EventMethodTest, ExitSuccess)
+{
+ EXPECT_CALL(mock, sd_event_exit(expected_event, 0)).WillOnce(Return(2));
+ EXPECT_EQ(2, event->exit(0));
+}
+
+TEST_F(EventMethodTest, ExitUserError)
+{
+ const int user_error = 10;
+ EXPECT_CALL(mock, sd_event_exit(expected_event, user_error))
+ .WillOnce(Return(user_error));
+ EXPECT_EQ(user_error, event->exit(user_error));
+}
+
+TEST_F(EventMethodTest, ExitInternalError)
+{
+ EXPECT_CALL(mock, sd_event_exit(expected_event, 5))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(event->exit(5), SdEventError);
+}
+
+TEST_F(EventMethodTest, GetExitCodeSuccess)
+{
+ EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
+ .WillOnce(DoAll(SetArgPointee<1>(1), Return(0)));
+ EXPECT_EQ(1, event->get_exit_code());
+
+ EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
+ .WillOnce(DoAll(SetArgPointee<1>(0), Return(2)));
+ EXPECT_EQ(0, event->get_exit_code());
+}
+
+TEST_F(EventMethodTest, GetExitCodeError)
+{
+ EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(event->get_exit_code(), SdEventError);
+}
+
TEST_F(EventMethodTest, GetWatchdogSuccess)
{
EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))