Test FsPollHandle (#90)

Test FsPollHandle
This commit is contained in:
Michele Caini 2017-07-11 21:14:21 +02:00 committed by GitHub
parent 799965298a
commit b294b4fbd5
3 changed files with 40 additions and 6 deletions

View File

@ -4,6 +4,7 @@
#include <utility>
#include <string>
#include <memory>
#include <chrono>
#include <uv.h>
#include "handle.hpp"
#include "util.hpp"
@ -45,6 +46,8 @@ class FsPollHandle final: public Handle<FsPollHandle, uv_fs_poll_t> {
}
public:
using Time = std::chrono::duration<unsigned int, std::milli>;
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());
}
/**

View File

@ -119,9 +119,12 @@ add_test(NAME ${TARGET_FS_EVENT} COMMAND ${TARGET_FS_EVENT})
# Test TARGET_FS_POLL
set(TARGET_FS_POLL_SOURCES $<TARGET_OBJECTS:odr> 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

View File

@ -2,11 +2,39 @@
#include <uvw.hpp>
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<uvw::FsPollHandle>();
auto request = loop->resource<uvw::FileReq>();
handle = nullptr;
bool checkFsPollEvent = false;
// TODO
handle->on<uvw::ErrorEvent>([](const auto &, auto &) { FAIL(); });
request->on<uvw::ErrorEvent>([](const auto &, auto &) { FAIL(); });
handle->on<uvw::FsPollEvent>([&checkFsPollEvent](const auto &, auto &hndl) {
ASSERT_FALSE(checkFsPollEvent);
checkFsPollEvent = true;
hndl.stop();
hndl.close();
ASSERT_TRUE(hndl.closing());
});
request->on<uvw::FsEvent<uvw::FileReq::Type::WRITE>>([](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<char[]>{new char[1]{ 42 }}, 1, 0);
ASSERT_EQ(handle->path(), filename);
ASSERT_TRUE(handle->active());
ASSERT_FALSE(handle->closing());
loop->run();
ASSERT_TRUE(checkFsPollEvent);
}