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_IDLE idle)
set(TARGET_LOOP loop) set(TARGET_LOOP loop)
set(TARGET_PREPARE prepare) set(TARGET_PREPARE prepare)
set(TARGET_TIMER timer)
set(TARGET_WORK work) set(TARGET_WORK work)
# Test TARGET_MAIN # 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}) target_link_libraries(${TARGET_PREPARE} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_PREPARE} COMMAND ${TARGET_PREPARE}) 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 # Test TARGET_WORK
set(TARGET_WORK_SOURCES uvw/work.cpp) set(TARGET_WORK_SOURCES uvw/work.cpp)

View File

@ -27,7 +27,7 @@ TEST(Idle, StartAndStop) {
ASSERT_TRUE(handle->active()); ASSERT_TRUE(handle->active());
ASSERT_FALSE(handle->closing()); ASSERT_FALSE(handle->closing());
loop->run<uvw::Loop::Mode::NOWAIT>(); loop->run();
ASSERT_FALSE(checkErrorEvent); ASSERT_FALSE(checkErrorEvent);
ASSERT_TRUE(checkIdleEvent); ASSERT_TRUE(checkIdleEvent);

View File

@ -27,7 +27,7 @@ TEST(Prepare, StartAndStop) {
ASSERT_TRUE(handle->active()); ASSERT_TRUE(handle->active());
ASSERT_FALSE(handle->closing()); ASSERT_FALSE(handle->closing());
loop->run<uvw::Loop::Mode::NOWAIT>(); loop->run();
ASSERT_FALSE(checkErrorEvent); ASSERT_FALSE(checkErrorEvent);
ASSERT_TRUE(checkPrepareEvent); ASSERT_TRUE(checkPrepareEvent);

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();
}