From b294b4fbd56debb3b1506963660c8fda49ad7ef8 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Tue, 11 Jul 2017 21:14:21 +0200 Subject: [PATCH] Test FsPollHandle (#90) Test FsPollHandle --- src/uvw/fs_poll.hpp | 7 +++++-- test/CMakeLists.txt | 3 +++ test/uvw/fs_poll.cpp | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/uvw/fs_poll.hpp b/src/uvw/fs_poll.hpp index 891dca29..50725813 100644 --- a/src/uvw/fs_poll.hpp +++ b/src/uvw/fs_poll.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "handle.hpp" #include "util.hpp" @@ -45,6 +46,8 @@ class FsPollHandle final: public Handle { } public: + using Time = std::chrono::duration; + using Handle::Handle; /** @@ -63,8 +66,8 @@ public: * @param file The path to the file to be checked. * @param interval Milliseconds between successive checks. */ - void start(std::string file, unsigned int interval) { - invoke(&uv_fs_poll_start, get(), &startCallback, file.data(), interval); + void start(std::string file, Time interval) { + invoke(&uv_fs_poll_start, get(), &startCallback, file.data(), interval.count()); } /** diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c517eb7c..77bb3edb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -119,9 +119,12 @@ add_test(NAME ${TARGET_FS_EVENT} COMMAND ${TARGET_FS_EVENT}) # Test TARGET_FS_POLL set(TARGET_FS_POLL_SOURCES $ uvw/fs_poll.cpp) +set(TARGET_FS_POLL_DIR ${CMAKE_CURRENT_BINARY_DIR}/fs_poll) +file(MAKE_DIRECTORY ${TARGET_FS_POLL_DIR}) add_executable(${TARGET_FS_POLL} ${TARGET_FS_POLL_SOURCES}) target_include_directories(${TARGET_FS_POLL} PRIVATE ${UVW_SRC_DIR}) target_link_libraries(${TARGET_FS_POLL} PRIVATE ${COMMON_LINK_LIBS}) +target_compile_definitions(${TARGET_FS_POLL} PRIVATE TARGET_FS_POLL_DIR="${TARGET_FS_POLL_DIR}") add_test(NAME ${TARGET_FS_POLL} COMMAND ${TARGET_FS_POLL}) # Test TARGET_FS_REQ diff --git a/test/uvw/fs_poll.cpp b/test/uvw/fs_poll.cpp index 2b6ab436..a6e80d2d 100644 --- a/test/uvw/fs_poll.cpp +++ b/test/uvw/fs_poll.cpp @@ -2,11 +2,39 @@ #include -TEST(FsPoll, TODO) { +TEST(FsPoll, Functionalities) { + const std::string filename = std::string{TARGET_FS_POLL_DIR} + std::string{"/test.file"}; + auto loop = uvw::Loop::getDefault(); - auto handle = uvw::FsPollHandle::create(loop); + auto handle = loop->resource(); + auto request = loop->resource(); - handle = nullptr; + bool checkFsPollEvent = false; - // TODO + handle->on([](const auto &, auto &) { FAIL(); }); + request->on([](const auto &, auto &) { FAIL(); }); + + handle->on([&checkFsPollEvent](const auto &, auto &hndl) { + ASSERT_FALSE(checkFsPollEvent); + checkFsPollEvent = true; + hndl.stop(); + hndl.close(); + ASSERT_TRUE(hndl.closing()); + }); + + request->on>([](const auto &, auto &req) { + req.close(); + }); + + request->openSync(filename, O_CREAT | O_RDWR | O_TRUNC, 0755); + handle->start(filename, uvw::FsPollHandle::Time{5}); + request->write(std::unique_ptr{new char[1]{ 42 }}, 1, 0); + + ASSERT_EQ(handle->path(), filename); + ASSERT_TRUE(handle->active()); + ASSERT_FALSE(handle->closing()); + + loop->run(); + + ASSERT_TRUE(checkFsPollEvent); }