This commit is contained in:
Michele Caini 2016-08-16 11:02:01 +02:00
parent 165dcf18af
commit 193cece430
8 changed files with 159 additions and 21 deletions

View File

@ -26,6 +26,7 @@ set(TARGET_CHECK check)
set(TARGET_IDLE idle)
set(TARGET_LOOP loop)
set(TARGET_PREPARE prepare)
set(TARGET_TIMER timer)
set(TARGET_WORK work)
# Test TARGET_MAIN
@ -76,6 +77,14 @@ target_include_directories(${TARGET_PREPARE} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_PREPARE} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_PREPARE} COMMAND ${TARGET_PREPARE})
# Test TARGET_TIMER
set(TARGET_TIMER_SOURCES uvw/timer.cpp)
add_executable(${TARGET_TIMER} ${TARGET_TIMER_SOURCES})
target_include_directories(${TARGET_TIMER} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_TIMER} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_TIMER} COMMAND ${TARGET_TIMER})
# Test TARGET_WORK
set(TARGET_WORK_SOURCES uvw/work.cpp)

View File

@ -9,12 +9,12 @@ TEST(Async, Send) {
bool checkErrorEvent = false;
bool checkAsyncEvent = false;
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &){
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
handle->on<uvw::AsyncEvent>([&checkAsyncEvent](const auto &, auto &handle){
handle->on<uvw::AsyncEvent>([&checkAsyncEvent](const auto &, auto &handle) {
ASSERT_FALSE(checkAsyncEvent);
checkAsyncEvent = true;
handle.close();
@ -37,7 +37,7 @@ TEST(Async, Fake) {
auto loop = uvw::Loop::getDefault();
auto handle = loop->resource<uvw::AsyncHandle>();
auto l = [](const auto &, auto &){ ASSERT_FALSE(true); };
auto l = [](const auto &, auto &) { ASSERT_FALSE(true); };
handle->on<uvw::ErrorEvent>(l);
handle->on<uvw::AsyncEvent>(l);

View File

@ -9,12 +9,12 @@ TEST(Check, StartAndStop) {
bool checkErrorEvent = false;
bool checkCheckEvent = false;
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &){
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
handle->on<uvw::CheckEvent>([&checkCheckEvent](const auto &, auto &handle){
handle->on<uvw::CheckEvent>([&checkCheckEvent](const auto &, auto &handle) {
ASSERT_FALSE(checkCheckEvent);
checkCheckEvent = true;
handle.stop();
@ -38,7 +38,7 @@ TEST(Check, Fake) {
auto loop = uvw::Loop::getDefault();
auto handle = loop->resource<uvw::CheckHandle>();
auto l = [](const auto &, auto &){ ASSERT_FALSE(true); };
auto l = [](const auto &, auto &) { ASSERT_FALSE(true); };
handle->on<uvw::ErrorEvent>(l);
handle->on<uvw::CheckEvent>(l);

View File

@ -9,12 +9,12 @@ TEST(Idle, StartAndStop) {
bool checkErrorEvent = false;
bool checkIdleEvent = false;
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &){
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
handle->on<uvw::IdleEvent>([&checkIdleEvent](const auto &, auto &handle){
handle->on<uvw::IdleEvent>([&checkIdleEvent](const auto &, auto &handle) {
ASSERT_FALSE(checkIdleEvent);
checkIdleEvent = true;
handle.stop();
@ -27,7 +27,7 @@ TEST(Idle, StartAndStop) {
ASSERT_TRUE(handle->active());
ASSERT_FALSE(handle->closing());
loop->run<uvw::Loop::Mode::NOWAIT>();
loop->run();
ASSERT_FALSE(checkErrorEvent);
ASSERT_TRUE(checkIdleEvent);
@ -38,7 +38,7 @@ TEST(Idle, Fake) {
auto loop = uvw::Loop::getDefault();
auto handle = loop->resource<uvw::IdleHandle>();
auto l = [](const auto &, auto &){ ASSERT_FALSE(true); };
auto l = [](const auto &, auto &) { ASSERT_FALSE(true); };
handle->on<uvw::ErrorEvent>(l);
handle->on<uvw::IdleEvent>(l);

View File

@ -13,7 +13,7 @@ TEST(Loop, PartiallyDone) {
auto loop = uvw::Loop::create();
auto handle = loop->resource<uvw::PrepareHandle>();
auto req = loop->resource<uvw::WorkReq>([](){});
auto req = loop->resource<uvw::WorkReq>([]() {});
auto err = [](const auto &, auto &) { ASSERT_TRUE(false); };

View File

@ -9,12 +9,12 @@ TEST(Prepare, StartAndStop) {
bool checkErrorEvent = false;
bool checkPrepareEvent = false;
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &){
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
handle->on<uvw::PrepareEvent>([&checkPrepareEvent](const auto &, auto &handle){
handle->on<uvw::PrepareEvent>([&checkPrepareEvent](const auto &, auto &handle) {
ASSERT_FALSE(checkPrepareEvent);
checkPrepareEvent = true;
handle.stop();
@ -27,7 +27,7 @@ TEST(Prepare, StartAndStop) {
ASSERT_TRUE(handle->active());
ASSERT_FALSE(handle->closing());
loop->run<uvw::Loop::Mode::NOWAIT>();
loop->run();
ASSERT_FALSE(checkErrorEvent);
ASSERT_TRUE(checkPrepareEvent);
@ -38,7 +38,7 @@ TEST(Prepare, Fake) {
auto loop = uvw::Loop::getDefault();
auto handle = loop->resource<uvw::PrepareHandle>();
auto l = [](const auto &, auto &){ ASSERT_FALSE(true); };
auto l = [](const auto &, auto &) { ASSERT_FALSE(true); };
handle->on<uvw::ErrorEvent>(l);
handle->on<uvw::PrepareEvent>(l);

129
test/uvw/timer.cpp Normal file
View File

@ -0,0 +1,129 @@
#include <gtest/gtest.h>
#include <uvw.hpp>
TEST(Timer, StartAndStop) {
auto loop = uvw::Loop::getDefault();
auto handleNoRepeat = loop->resource<uvw::TimerHandle>();
auto handleRepeat = loop->resource<uvw::TimerHandle>();
bool checkErrorEvent = false;
bool checkTimerNoRepeatEvent = false;
bool checkTimerRepeatEvent = false;
handleNoRepeat->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
handleRepeat->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
handleNoRepeat->on<uvw::TimerEvent>([&checkTimerNoRepeatEvent](const auto &, auto &handle) {
ASSERT_FALSE(checkTimerNoRepeatEvent);
checkTimerNoRepeatEvent = true;
handle.stop();
handle.close();
ASSERT_TRUE(handle.closing());
});
handleRepeat->on<uvw::TimerEvent>([&checkTimerRepeatEvent](const auto &, auto &handle) {
if(checkTimerRepeatEvent) {
handle.stop();
handle.close();
ASSERT_TRUE(handle.closing());
} else {
checkTimerRepeatEvent = true;
ASSERT_FALSE(handle.closing());
}
});
handleNoRepeat->start(uvw::TimerHandle::Time{0}, uvw::TimerHandle::Time{0});
handleRepeat->start(uvw::TimerHandle::Time{0}, uvw::TimerHandle::Time{1});
ASSERT_TRUE(handleNoRepeat->active());
ASSERT_FALSE(handleNoRepeat->closing());
ASSERT_TRUE(handleRepeat->active());
ASSERT_FALSE(handleRepeat->closing());
loop->run();
ASSERT_FALSE(checkErrorEvent);
ASSERT_TRUE(checkTimerNoRepeatEvent);
ASSERT_TRUE(checkTimerRepeatEvent);
}
TEST(Timer, Again) {
auto loop = uvw::Loop::getDefault();
auto handle = loop->resource<uvw::TimerHandle>();
bool checkErrorEvent = false;
bool checkTimerEvent = true;
handle->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
handle->on<uvw::TimerEvent>([&checkTimerEvent](const auto &, auto &handle) {
static bool guard = false;
if(guard) {
handle.stop();
handle.close();
checkTimerEvent = true;
ASSERT_TRUE(handle.closing());
} else {
guard = true;
handle.again();
ASSERT_EQ(handle.repeat(), uvw::TimerHandle::Time{1});
ASSERT_FALSE(handle.closing());
}
});
ASSERT_NO_THROW(handle->again());
ASSERT_FALSE(handle->active());
ASSERT_TRUE(checkErrorEvent);
checkErrorEvent = false;
handle->start(uvw::TimerHandle::Time{0}, uvw::TimerHandle::Time{1});
ASSERT_TRUE(handle->active());
ASSERT_FALSE(handle->closing());
loop->run();
ASSERT_FALSE(checkErrorEvent);
ASSERT_TRUE(checkTimerEvent);
}
TEST(Timer, Repeat) {
auto loop = uvw::Loop::getDefault();
auto handle = loop->resource<uvw::TimerHandle>();
ASSERT_NO_THROW(handle->repeat(uvw::TimerHandle::Time{42}));
ASSERT_EQ(handle->repeat(), uvw::TimerHandle::Time{42});
}
TEST(Timer, Fake) {
auto loop = uvw::Loop::getDefault();
auto handle = loop->resource<uvw::TimerHandle>();
auto l = [](const auto &, auto &) { ASSERT_FALSE(true); };
handle->on<uvw::ErrorEvent>(l);
handle->on<uvw::TimerEvent>(l);
handle->start(uvw::TimerHandle::Time{0}, uvw::TimerHandle::Time{0});
handle->close();
ASSERT_FALSE(handle->active());
ASSERT_TRUE(handle->closing());
loop->run();
}

View File

@ -9,17 +9,17 @@ TEST(Work, RunTask) {
bool checkWorkEvent = false;
bool checkTask = false;
auto req = loop->resource<uvw::WorkReq>([&checkTask](){
auto req = loop->resource<uvw::WorkReq>([&checkTask]() {
ASSERT_FALSE(checkTask);
checkTask = true;
});
req->on<uvw::WorkEvent>([&checkWorkEvent](const auto &, auto &){
req->on<uvw::WorkEvent>([&checkWorkEvent](const auto &, auto &) {
ASSERT_FALSE(checkWorkEvent);
checkWorkEvent = true;
});
req->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &){
req->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});
@ -39,17 +39,17 @@ TEST(Work, Cancellation) {
bool checkWorkEvent = false;
bool checkTask = false;
auto req = loop->resource<uvw::WorkReq>([&checkTask](){
auto req = loop->resource<uvw::WorkReq>([&checkTask]() {
ASSERT_FALSE(checkTask);
checkTask = true;
});
req->on<uvw::WorkEvent>([&checkWorkEvent](const auto &, auto &){
req->on<uvw::WorkEvent>([&checkWorkEvent](const auto &, auto &) {
ASSERT_FALSE(checkWorkEvent);
checkWorkEvent = true;
});
req->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &){
req->on<uvw::ErrorEvent>([&checkErrorEvent](const auto &, auto &) {
ASSERT_FALSE(checkErrorEvent);
checkErrorEvent = true;
});