avoid sending error events when the return value is enough (wip)

This commit is contained in:
Michele Caini 2022-04-07 23:02:16 +02:00
parent 374495c740
commit 93a9df216d
27 changed files with 102 additions and 99 deletions

View File

@ -30,7 +30,7 @@ public:
* Unlike other handle initialization functions, it immediately starts the
* handle.
*
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -26,7 +26,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -75,7 +75,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -38,7 +38,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -15,16 +15,12 @@ UVW_INLINE int idle_handle::init() {
return leak_if(uv_idle_init(parent().raw(), raw()));
}
UVW_INLINE void idle_handle::start() {
if(auto err = uv_idle_start(raw(), &start_callback); err != 0) {
publish(error_event{err});
}
UVW_INLINE int idle_handle::start() {
return uv_idle_start(raw(), &start_callback);
}
UVW_INLINE void idle_handle::stop() {
if(auto err = uv_idle_stop(raw()); err != 0) {
publish(error_event{err});
}
UVW_INLINE int idle_handle::stop() {
return uv_idle_stop(raw());
}
} // namespace uvw

View File

@ -34,7 +34,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;
@ -43,13 +43,17 @@ public:
*
* An idle event will be emitted once per loop iteration, right before
* polling the prepare handles.
*
* @return Underlying return value.
*/
void start();
int start();
/**
* @brief Stops the handle.
*
* @return Underlying return value.
*/
void stop();
int stop();
};
} // namespace uvw

View File

@ -63,8 +63,8 @@ UVW_INLINE int loop::close() {
return ret;
}
bool loop::run(run_mode mode) UVW_NOEXCEPT {
return (uv_run(uv_loop.get(), static_cast<uv_run_mode>(mode)) == 0);
int loop::run(run_mode mode) UVW_NOEXCEPT {
return uv_run(uv_loop.get(), static_cast<uv_run_mode>(mode));
}
UVW_INLINE bool loop::alive() const UVW_NOEXCEPT {
@ -96,10 +96,8 @@ UVW_INLINE void loop::update() const UVW_NOEXCEPT {
return uv_update_time(uv_loop.get());
}
UVW_INLINE void loop::fork() UVW_NOEXCEPT {
if(auto err = uv_loop_fork(uv_loop.get()); err) {
publish(error_event{err});
}
UVW_INLINE int loop::fork() UVW_NOEXCEPT {
return uv_loop_fork(uv_loop.get());
}
UVW_INLINE void loop::data(std::shared_ptr<void> ud) {

View File

@ -132,12 +132,12 @@ public:
* See the official
* [documentation](http://docs.libuv.org/en/v1.x/loop.html#c.uv_loop_configure)
* for further details.
*
* @return Underlying return value.
*/
template<typename... Args>
void configure(option flag, Args &&...args) {
if(auto err = uv_loop_configure(uv_loop.get(), static_cast<uv_loop_option>(flag), std::forward<Args>(args)...); err) {
publish(error_event{err});
}
int configure(option flag, Args &&...args) {
return uv_loop_configure(uv_loop.get(), static_cast<uv_loop_option>(flag), std::forward<Args>(args)...);
}
/**
@ -172,7 +172,7 @@ public:
* Call this function only when the loop has finished executing and all open
* handles and requests have been closed, or the loop will emit an error.
*
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int close();
@ -192,9 +192,9 @@ public:
* [documentation](http://docs.libuv.org/en/v1.x/loop.html#c.uv_run)
* for further details.
*
* @return True when done, false in all other cases.
* @return Underlying return value.
*/
bool run(run_mode mode = run_mode::DEFAULT) UVW_NOEXCEPT;
int run(run_mode mode = run_mode::DEFAULT) UVW_NOEXCEPT;
/**
* @brief Checks if there are active resources.
@ -358,8 +358,10 @@ public:
* See the official
* [documentation](http://docs.libuv.org/en/v1.x/loop.html#c.uv_loop_fork)
* for further details.
*
* @return Underlying return value.
*/
void fork() UVW_NOEXCEPT;
int fork() UVW_NOEXCEPT;
/**
* @brief Gets user-defined data. `uvw` won't use this field in any case.

View File

@ -43,7 +43,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -67,7 +67,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -15,16 +15,12 @@ UVW_INLINE int prepare_handle::init() {
return leak_if(uv_prepare_init(parent().raw(), raw()));
}
UVW_INLINE void prepare_handle::start() {
if(auto err = uv_prepare_start(raw(), &start_callback); err != 0) {
publish(error_event{err});
}
UVW_INLINE int prepare_handle::start() {
return uv_prepare_start(raw(), &start_callback);
}
UVW_INLINE void prepare_handle::stop() {
if(auto err = uv_prepare_stop(raw()); err != 0) {
publish(error_event{err});
}
UVW_INLINE int prepare_handle::stop() {
return uv_prepare_stop(raw());
}
} // namespace uvw

View File

@ -26,7 +26,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;
@ -37,13 +37,16 @@ public:
* polling for I/O.
*
* The handle will start emitting prepare events when needed.
*
* @return Underlying return value.
*/
void start();
int start();
/**
* @brief Stops the handle.
* @return Underlying return value.
*/
void stop();
int stop();
};
} // namespace uvw

View File

@ -90,7 +90,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -36,7 +36,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -51,7 +51,7 @@ public:
/**
* @brief Initializes the handle. No socket is created as of yet.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -15,22 +15,16 @@ UVW_INLINE int timer_handle::init() {
return leak_if(uv_timer_init(parent().raw(), raw()));
}
UVW_INLINE void timer_handle::start(timer_handle::time timeout, timer_handle::time repeat) {
if(auto err = uv_timer_start(raw(), &start_callback, timeout.count(), repeat.count()); err != 0) {
publish(error_event{err});
}
UVW_INLINE int timer_handle::start(timer_handle::time timeout, timer_handle::time repeat) {
return uv_timer_start(raw(), &start_callback, timeout.count(), repeat.count());
}
UVW_INLINE void timer_handle::stop() {
if(auto err = uv_timer_stop(raw()); err != 0) {
publish(error_event{err});
}
UVW_INLINE int timer_handle::stop() {
return uv_timer_stop(raw());
}
UVW_INLINE void timer_handle::again() {
if(auto err = uv_timer_again(raw()); err != 0) {
publish(error_event{err});
}
UVW_INLINE int timer_handle::again() {
return uv_timer_again(raw());
}
UVW_INLINE void timer_handle::repeat(timer_handle::time repeat) {

View File

@ -29,7 +29,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;
@ -44,13 +44,16 @@ public:
* `std::chrono::duration<uint64_t, std::milli>`).
* @param repeat Milliseconds between successive events (use
* `std::chrono::duration<uint64_t, std::milli>`).
*
* @return Underlying return value.
*/
void start(time timeout, time repeat);
int start(time timeout, time repeat);
/**
* @brief Stops the handle.
* @return Underlying return value.
*/
void stop();
int stop();
/**
* @brief Stops the timer and restarts it if it was repeating.
@ -58,8 +61,10 @@ public:
* Stop the timer, and if it is repeating restart it using the repeat value
* as the timeout.<br/>
* If the timer has never been started before it emits an error event.
*
* @return Underlying return value.
*/
void again();
int again();
/**
* @brief Sets the repeat interval value.

View File

@ -58,7 +58,7 @@ public:
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -92,7 +92,7 @@ public:
/**
* @brief Initializes the handle. The actual socket is created lazily.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
int init() final;

View File

@ -27,7 +27,7 @@ struct uv_type {
/**
* @brief Initializes the handle.
* @return Underlying code in case of errors, 0 otherwise.
* @return Underlying return value.
*/
virtual int init() {
return 0;

View File

@ -23,10 +23,8 @@ UVW_INLINE void work_req::after_work_callback(uv_work_t* req, int status) {
}
}
UVW_INLINE void work_req::queue() {
if(auto err = this->leak_if(uv_queue_work(parent().raw(), raw(), &work_callback, &after_work_callback)); err != 0) {
publish(error_event{err});
}
UVW_INLINE int work_req::queue() {
return this->leak_if(uv_queue_work(parent().raw(), raw(), &work_callback, &after_work_callback));
}
} // namespace uvw

View File

@ -41,8 +41,10 @@ public:
* A work event will be emitted on the loop thread when the task is
* finished.<br/>
* This request can be cancelled with `cancel()`.
*
* @return Underlying return value.
*/
void queue();
int queue();
private:
task func{};

View File

@ -11,14 +11,17 @@ TEST(Idle, StartAndStop) {
handle->on<uvw::idle_event>([&checkIdleEvent](const auto &, auto &hndl) {
ASSERT_FALSE(checkIdleEvent);
checkIdleEvent = true;
hndl.stop();
ASSERT_EQ(0, hndl.stop());
hndl.close();
ASSERT_TRUE(hndl.closing());
});
handle->start();
ASSERT_EQ(0, handle->start());
ASSERT_TRUE(handle->active());
ASSERT_FALSE(handle->closing());

View File

@ -32,7 +32,7 @@ TEST(Loop, Functionalities) {
#ifndef _MSC_VER
// fork isn't implemented on Windows in libuv and it returns an error by default
ASSERT_NO_THROW(loop->fork());
ASSERT_EQ(0, loop->fork());
#endif
ASSERT_FALSE(loop->alive());
@ -50,12 +50,12 @@ TEST(Loop, Functionalities) {
});
ASSERT_TRUE(loop->alive());
ASSERT_NO_THROW(loop->run());
ASSERT_EQ(0, loop->run());
loop->walk([](auto &) { FAIL(); });
ASSERT_NO_THROW(loop->run(uvw::loop::run_mode::ONCE));
ASSERT_NO_THROW(loop->run(uvw::loop::run_mode::NOWAIT));
ASSERT_EQ(0, loop->run(uvw::loop::run_mode::ONCE));
ASSERT_EQ(0, loop->run(uvw::loop::run_mode::NOWAIT));
ASSERT_FALSE(loop->alive());
}
@ -106,8 +106,8 @@ TEST(Loop, UserData) {
TEST(Loop, Configure) {
auto loop = uvw::loop::create();
ASSERT_NO_THROW(loop->configure(uvw::loop::option::BLOCK_SIGNAL, 9));
ASSERT_NO_THROW(loop->run());
ASSERT_EQ(0, loop->configure(uvw::loop::option::IDLE_TIME));
ASSERT_EQ(0, loop->run());
}
TEST(Loop, IdleTime) {

View File

@ -11,14 +11,17 @@ TEST(Prepare, StartAndStop) {
handle->on<uvw::prepare_event>([&checkPrepareEvent](const auto &, auto &hndl) {
ASSERT_FALSE(checkPrepareEvent);
checkPrepareEvent = true;
hndl.stop();
ASSERT_EQ(0, hndl.stop());
hndl.close();
ASSERT_TRUE(hndl.closing());
});
handle->start();
ASSERT_EQ(0, handle->start());
ASSERT_TRUE(handle->active());
ASSERT_FALSE(handle->closing());

View File

@ -14,16 +14,22 @@ TEST(Timer, StartAndStop) {
handleNoRepeat->on<uvw::timer_event>([&checkTimerNoRepeatEvent](const auto &, auto &handle) {
ASSERT_FALSE(checkTimerNoRepeatEvent);
checkTimerNoRepeatEvent = true;
handle.stop();
ASSERT_EQ(0, handle.stop());
handle.close();
ASSERT_TRUE(handle.closing());
});
handleRepeat->on<uvw::timer_event>([&checkTimerRepeatEvent](const auto &, auto &handle) {
if(checkTimerRepeatEvent) {
handle.stop();
ASSERT_EQ(0, handle.stop());
handle.close();
ASSERT_TRUE(handle.closing());
} else {
checkTimerRepeatEvent = true;
@ -31,8 +37,8 @@ TEST(Timer, StartAndStop) {
}
});
handleNoRepeat->start(uvw::timer_handle::time{0}, uvw::timer_handle::time{0});
handleRepeat->start(uvw::timer_handle::time{0}, uvw::timer_handle::time{1});
ASSERT_EQ(0, handleNoRepeat->start(uvw::timer_handle::time{0}, uvw::timer_handle::time{0}));
ASSERT_EQ(0, handleRepeat->start(uvw::timer_handle::time{0}, uvw::timer_handle::time{1}));
ASSERT_TRUE(handleNoRepeat->active());
ASSERT_FALSE(handleNoRepeat->closing());
@ -50,13 +56,9 @@ TEST(Timer, Again) {
auto loop = uvw::loop::get_default();
auto handle = loop->resource<uvw::timer_handle>();
bool checkErrorEvent = false;
bool checkTimerEvent = false;
handle->on<uvw::error_event>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
handle->on<uvw::error_event>([](const auto &, auto &) { FAIL(); });
handle->on<uvw::timer_event>([&checkTimerEvent](const auto &, auto &hndl) {
static bool guard = false;
@ -68,17 +70,15 @@ TEST(Timer, Again) {
ASSERT_TRUE(hndl.closing());
} else {
guard = true;
hndl.again();
ASSERT_EQ(0, hndl.again());
ASSERT_EQ(hndl.repeat(), uvw::timer_handle::time{1});
ASSERT_FALSE(hndl.closing());
}
});
ASSERT_NO_THROW(handle->again());
ASSERT_NE(0, handle->again());
ASSERT_FALSE(handle->active());
ASSERT_TRUE(checkErrorEvent);
checkErrorEvent = false;
handle->start(uvw::timer_handle::time{0}, uvw::timer_handle::time{1});
ASSERT_TRUE(handle->active());
@ -86,7 +86,6 @@ TEST(Timer, Again) {
loop->run();
ASSERT_FALSE(checkErrorEvent);
ASSERT_TRUE(checkTimerEvent);
handle->close();
@ -94,9 +93,7 @@ TEST(Timer, Again) {
ASSERT_FALSE(handle->active());
ASSERT_TRUE(handle->closing());
handle->start(uvw::timer_handle::time{0}, uvw::timer_handle::time{1});
ASSERT_TRUE(checkErrorEvent);
ASSERT_NE(0, handle->start(uvw::timer_handle::time{0}, uvw::timer_handle::time{1}));
}
TEST(Timer, Repeat) {

View File

@ -21,7 +21,9 @@ TEST(Work, RunTask) {
});
handle->start();
req->queue();
ASSERT_EQ(0, req->queue());
loop->run();
ASSERT_TRUE(checkTask);