added tests for loop + bug fixing

This commit is contained in:
Michele Caini 2016-07-18 17:24:18 +02:00
parent 73b59b32a2
commit 79adde31f7
4 changed files with 77 additions and 47 deletions

View File

@ -49,7 +49,7 @@ protected:
public:
void cancel() {
invoke(&uv_cancel, this->template get<uv_req_t>());
this->invoke(&uv_cancel, this->template get<uv_req_t>());
}
std::size_t size() const noexcept {

View File

@ -49,25 +49,6 @@ const IpTraits<IPv6>::NameFuncType IpTraits<IPv6>::NameFunc = uv_ip6_name;
}
class UVWException final: std::runtime_error {
public:
explicit UVWException(int code)
: runtime_error{uv_strerror(code)}, ec{code}
{ }
const char* name() const noexcept {
return uv_err_name(ec);
}
const char* error() const noexcept {
return uv_strerror(ec);
}
private:
int ec;
};
struct FileDescriptor {
using Type = uv_file;

View File

@ -4,37 +4,37 @@
set(UVW_SRC_DIR ${CMAKE_SOURCE_DIR}/${PROJECT_SRC_DIR})
# List of available targets
set(TARGET_FOOBAR foobar)
# Test TARGET_FOOBAR_SRC_DIR
set(
COMMON_INCLUDE_DIRS
${UVW_SRC_DIR}
${GOOGLETEST_INCLUDE_DIRS}
${UV_INCLUDE_DIRS}
)
set(
TARGET_FOOBAR_SOURCES
main.cpp
COMMON_LINK_LIBS
${CMAKE_THREAD_LIBS_INIT}
${GOOGLETEST_LIBRARIES}
${UV_LIBRARIES}
)
add_executable(
${TARGET_FOOBAR}
${TARGET_FOOBAR_SOURCES}
)
# List of available targets
target_include_directories(
${TARGET_FOOBAR}
PRIVATE ${UVW_SRC_DIR}
PRIVATE ${GOOGLETEST_INCLUDE_DIRS}
PRIVATE ${UV_INCLUDE_DIRS}
)
set(TARGET_MAIN main)
set(TARGET_LOOP loop)
target_link_libraries(
${TARGET_FOOBAR}
PRIVATE ${CMAKE_THREAD_LIBS_INIT}
PRIVATE ${GOOGLETEST_LIBRARIES}
PRIVATE ${UV_LIBRARIES}
)
# Test TARGET_MAIN
add_test(
NAME ${TARGET_FOOBAR}
COMMAND ${TARGET_FOOBAR}
)
set(TARGET_MAIN_SOURCES main.cpp)
add_executable(${TARGET_MAIN} ${TARGET_MAIN_SOURCES})
target_include_directories(${TARGET_MAIN} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_MAIN} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_MAIN} COMMAND ${TARGET_MAIN})
# Test TARGET_LOOP
set(TARGET_LOOP_SOURCES uvw/loop.cpp)
add_executable(${TARGET_LOOP} ${TARGET_LOOP_SOURCES})
target_include_directories(${TARGET_LOOP} PRIVATE ${COMMON_INCLUDE_DIRS})
target_link_libraries(${TARGET_LOOP} PRIVATE ${COMMON_LINK_LIBS})
add_test(NAME ${TARGET_LOOP} COMMAND ${TARGET_LOOP})

49
test/uvw/loop.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <gtest/gtest.h>
#include <uvw.hpp>
TEST(Loop, Basics) {
auto def = uvw::Loop::getDefault();
ASSERT_TRUE(static_cast<bool>(def));
ASSERT_FALSE(def->alive());
ASSERT_NO_THROW(def->stop());
def->walk([](uvw::BaseHandle &) { ASSERT_TRUE(false); });
auto loop = uvw::Loop::create();
auto handle = loop->resource<uvw::Prepare>();
auto req = loop->resource<uvw::Work>();
auto err = [](uvw::ErrorEvent, auto &) { ASSERT_TRUE(false); };
loop->on<uvw::ErrorEvent>(err);
req->on<uvw::ErrorEvent>(err);
handle->on<uvw::ErrorEvent>(err);
ASSERT_TRUE(static_cast<bool>(handle));
ASSERT_TRUE(static_cast<bool>(req));
ASSERT_FALSE(loop->alive());
handle->start();
handle->on<uvw::PrepareEvent>([](uvw::PrepareEvent, uvw::Prepare &handle) {
handle.loop().walk([](uvw::BaseHandle &) {
static bool trigger = true;
ASSERT_TRUE(trigger);
trigger = false;
});
handle.close();
});
ASSERT_TRUE(loop->alive());
ASSERT_NO_THROW(loop->run());
loop->walk([](uvw::BaseHandle &) { ASSERT_TRUE(false); });
ASSERT_NO_THROW(loop->runOnce());
ASSERT_NO_THROW(loop->runWait());
ASSERT_FALSE(loop->alive());
}