From 89435a5662320a7b77c359a15a3457ae666ae722 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 20 Nov 2023 17:53:47 -0500 Subject: Tests: Add dedicated test to cover cmUVHandlePtr types Move the case added by commit 70d88a5361 (cmUVHandlePtr: Add uv_idle_ptr, 2023-11-06) to a dedicated test. --- Tests/CMakeLib/CMakeLists.txt | 1 + Tests/CMakeLib/testUVHandlePtr.cxx | 36 ++++++++++++++++++++++++++++++++++++ Tests/CMakeLib/testUVRAII.cxx | 25 ------------------------- 3 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 Tests/CMakeLib/testUVHandlePtr.cxx diff --git a/Tests/CMakeLib/CMakeLists.txt b/Tests/CMakeLib/CMakeLists.txt index 0e71cea..979d449 100644 --- a/Tests/CMakeLib/CMakeLists.txt +++ b/Tests/CMakeLib/CMakeLists.txt @@ -25,6 +25,7 @@ set(CMakeLib_TESTS testXMLParser.cxx testXMLSafe.cxx testFindPackageCommand.cxx + testUVHandlePtr.cxx testUVProcessChain.cxx testUVRAII.cxx testUVStreambuf.cxx diff --git a/Tests/CMakeLib/testUVHandlePtr.cxx b/Tests/CMakeLib/testUVHandlePtr.cxx new file mode 100644 index 0000000..5aab556 --- /dev/null +++ b/Tests/CMakeLib/testUVHandlePtr.cxx @@ -0,0 +1,36 @@ +#include + +#include + +#include "cmUVHandlePtr.h" + +static bool testIdle() +{ + bool idled = false; + + cm::uv_loop_ptr loop; + loop.init(); + + cm::uv_idle_ptr idle; + idle.init(*loop, &idled); + uv_idle_start(idle, [](uv_idle_t* handle) { + auto idledPtr = static_cast(handle->data); + *idledPtr = true; + uv_idle_stop(handle); + }); + uv_run(loop, UV_RUN_DEFAULT); + + if (!idled) { + std::cerr << "uv_idle_ptr did not trigger callback" << std::endl; + return false; + } + + return true; +} + +int testUVHandlePtr(int, char** const) +{ + bool passed = true; + passed = testIdle() && passed; + return passed ? 0 : -1; +} diff --git a/Tests/CMakeLib/testUVRAII.cxx b/Tests/CMakeLib/testUVRAII.cxx index 1b08778..9e79d5c 100644 --- a/Tests/CMakeLib/testUVRAII.cxx +++ b/Tests/CMakeLib/testUVRAII.cxx @@ -219,30 +219,6 @@ static bool testLoopDestructor() return true; } -static bool testIdle() -{ - bool idled = false; - - cm::uv_loop_ptr loop; - loop.init(); - - cm::uv_idle_ptr idle; - idle.init(*loop, &idled); - uv_idle_start(idle, [](uv_idle_t* handle) { - auto idledPtr = static_cast(handle->data); - *idledPtr = true; - uv_idle_stop(handle); - }); - uv_run(loop, UV_RUN_DEFAULT); - - if (!idled) { - std::cerr << "uv_idle_ptr did not trigger callback" << std::endl; - return false; - } - - return true; -} - int testUVRAII(int, char** const) { if (!testAsyncShutdown()) { @@ -255,6 +231,5 @@ int testUVRAII(int, char** const) passed = testAllMoves() && passed; passed = testLoopReset() && passed; passed = testLoopDestructor() && passed; - passed = testIdle() && passed; return passed ? 0 : -1; } -- cgit v0.12 From f906e2482fc2ce9dc36fe7d49f8963dea2bb6ef9 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 20 Nov 2023 17:56:20 -0500 Subject: Tests: Factor out callback in uv_idle_ptr test case --- Tests/CMakeLib/testUVHandlePtr.cxx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Tests/CMakeLib/testUVHandlePtr.cxx b/Tests/CMakeLib/testUVHandlePtr.cxx index 5aab556..f367047 100644 --- a/Tests/CMakeLib/testUVHandlePtr.cxx +++ b/Tests/CMakeLib/testUVHandlePtr.cxx @@ -11,13 +11,15 @@ static bool testIdle() cm::uv_loop_ptr loop; loop.init(); - cm::uv_idle_ptr idle; - idle.init(*loop, &idled); - uv_idle_start(idle, [](uv_idle_t* handle) { + auto cb = [](uv_idle_t* handle) { auto idledPtr = static_cast(handle->data); *idledPtr = true; uv_idle_stop(handle); - }); + }; + + cm::uv_idle_ptr idle; + idle.init(*loop, &idled); + uv_idle_start(idle, cb); uv_run(loop, UV_RUN_DEFAULT); if (!idled) { -- cgit v0.12 From 8908f277d94ad9e0a1e04a2d9a8c69db955265e9 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 20 Nov 2023 18:10:39 -0500 Subject: Tests: Add dedicated test case for uv_timer_ptr --- Tests/CMakeLib/testUVHandlePtr.cxx | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Tests/CMakeLib/testUVHandlePtr.cxx b/Tests/CMakeLib/testUVHandlePtr.cxx index f367047..02f8954 100644 --- a/Tests/CMakeLib/testUVHandlePtr.cxx +++ b/Tests/CMakeLib/testUVHandlePtr.cxx @@ -30,9 +30,36 @@ static bool testIdle() return true; } +static bool testTimer() +{ + bool timed = false; + + cm::uv_loop_ptr loop; + loop.init(); + + auto cb = [](uv_timer_t* handle) { + auto timedPtr = static_cast(handle->data); + *timedPtr = true; + uv_timer_stop(handle); + }; + + cm::uv_timer_ptr timer; + timer.init(*loop, &timed); + timer.start(cb, 10, 0); + uv_run(loop, UV_RUN_DEFAULT); + + if (!timed) { + std::cerr << "uv_timer_ptr did not trigger callback" << std::endl; + return false; + } + + return true; +} + int testUVHandlePtr(int, char** const) { bool passed = true; passed = testIdle() && passed; + passed = testTimer() && passed; return passed ? 0 : -1; } -- cgit v0.12 From 9dd14b2946eb821c1b8e5a5a8f81653710826a01 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 20 Nov 2023 15:52:07 -0500 Subject: cmUVHandlePtr: Add uv_timer_ptr::stop method This was missing w.r.t. the pattern established for other handle wrappers. --- Source/cmUVHandlePtr.cxx | 6 ++++++ Source/cmUVHandlePtr.h | 2 ++ Tests/CMakeLib/testUVHandlePtr.cxx | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/Source/cmUVHandlePtr.cxx b/Source/cmUVHandlePtr.cxx index 951ef3b..ecd9030 100644 --- a/Source/cmUVHandlePtr.cxx +++ b/Source/cmUVHandlePtr.cxx @@ -241,6 +241,12 @@ int uv_timer_ptr::start(uv_timer_cb cb, uint64_t timeout, uint64_t repeat) return uv_timer_start(*this, cb, timeout, repeat); } +void uv_timer_ptr::stop() +{ + assert(this->handle); + uv_timer_stop(*this); +} + #ifndef CMAKE_BOOTSTRAP uv_tty_ptr::operator uv_stream_t*() const { diff --git a/Source/cmUVHandlePtr.h b/Source/cmUVHandlePtr.h index e1bf0d0..7f70b36 100644 --- a/Source/cmUVHandlePtr.h +++ b/Source/cmUVHandlePtr.h @@ -238,6 +238,8 @@ struct uv_timer_ptr : public uv_handle_ptr_ int init(uv_loop_t& loop, void* data = nullptr); int start(uv_timer_cb cb, uint64_t timeout, uint64_t repeat); + + void stop(); }; struct uv_tty_ptr : public uv_handle_ptr_ diff --git a/Tests/CMakeLib/testUVHandlePtr.cxx b/Tests/CMakeLib/testUVHandlePtr.cxx index 02f8954..d6fdb77 100644 --- a/Tests/CMakeLib/testUVHandlePtr.cxx +++ b/Tests/CMakeLib/testUVHandlePtr.cxx @@ -53,6 +53,16 @@ static bool testTimer() return false; } + timed = false; + timer.start(cb, 10, 0); + timer.stop(); + uv_run(loop, UV_RUN_DEFAULT); + + if (timed) { + std::cerr << "uv_timer_ptr::stop did not stop callback" << std::endl; + return false; + } + return true; } -- cgit v0.12 From fb7ee82271cabed2197e1ed0c5be70453184e4e2 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 20 Nov 2023 15:52:53 -0500 Subject: cmUVHandlePtr: Add uv_idle_ptr::{start,stop} methods These were missing w.r.t. the pattern established for other handle wrappers. --- Source/cmUVHandlePtr.cxx | 12 ++++++++++++ Source/cmUVHandlePtr.h | 4 ++++ Tests/CMakeLib/testUVHandlePtr.cxx | 13 ++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Source/cmUVHandlePtr.cxx b/Source/cmUVHandlePtr.cxx index ecd9030..ca295e3 100644 --- a/Source/cmUVHandlePtr.cxx +++ b/Source/cmUVHandlePtr.cxx @@ -266,6 +266,18 @@ int uv_idle_ptr::init(uv_loop_t& loop, void* data) return uv_idle_init(&loop, *this); } +int uv_idle_ptr::start(uv_idle_cb cb) +{ + assert(this->handle); + return uv_idle_start(*this, cb); +} + +void uv_idle_ptr::stop() +{ + assert(this->handle); + uv_idle_stop(*this); +} + template class uv_handle_ptr_base_; #define UV_HANDLE_PTR_INSTANTIATE_EXPLICIT(NAME) \ diff --git a/Source/cmUVHandlePtr.h b/Source/cmUVHandlePtr.h index 7f70b36..7617415 100644 --- a/Source/cmUVHandlePtr.h +++ b/Source/cmUVHandlePtr.h @@ -201,6 +201,10 @@ struct uv_idle_ptr : public uv_handle_ptr_ CM_INHERIT_CTOR(uv_idle_ptr, uv_handle_ptr_, ); int init(uv_loop_t& loop, void* data = nullptr); + + int start(uv_idle_cb cb); + + void stop(); }; struct uv_signal_ptr : public uv_handle_ptr_ diff --git a/Tests/CMakeLib/testUVHandlePtr.cxx b/Tests/CMakeLib/testUVHandlePtr.cxx index d6fdb77..3c070b0 100644 --- a/Tests/CMakeLib/testUVHandlePtr.cxx +++ b/Tests/CMakeLib/testUVHandlePtr.cxx @@ -19,7 +19,7 @@ static bool testIdle() cm::uv_idle_ptr idle; idle.init(*loop, &idled); - uv_idle_start(idle, cb); + idle.start(cb); uv_run(loop, UV_RUN_DEFAULT); if (!idled) { @@ -27,6 +27,17 @@ static bool testIdle() return false; } + idled = false; + + idle.start(cb); + idle.stop(); + uv_run(loop, UV_RUN_DEFAULT); + + if (idled) { + std::cerr << "uv_idle_ptr::stop did not stop callback" << std::endl; + return false; + } + return true; } -- cgit v0.12