From 2c76e6c367f8afa25e86f6b4caf62fb1146bf8cc Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Sat, 4 Apr 2020 17:08:36 +0200 Subject: [PATCH] Split the async tests into their own test case --- .../continuable/detail/transforms/wait.hpp | 5 +- test/playground/test-playground.cpp | 8 - test/unit-test/CMakeLists.txt | 21 ++- .../async/test-continuable-async.cpp | 137 ++++++++++++++++++ .../multi/test-continuable-transforms.cpp | 107 +------------- 5 files changed, 159 insertions(+), 119 deletions(-) create mode 100644 test/unit-test/async/test-continuable-async.cpp diff --git a/include/continuable/detail/transforms/wait.hpp b/include/continuable/detail/transforms/wait.hpp index fbb4079..012aab3 100644 --- a/include/continuable/detail/transforms/wait.hpp +++ b/include/continuable/detail/transforms/wait.hpp @@ -76,8 +76,7 @@ Result wait_relaxed(continuable_base&& continuable) { std::move(continuable) .next([&](auto&&... args) { - sync_result = typename Result::from( - std::forward(args)...); + sync_result = Result::from(std::forward(args)...); ready.store(true, std::memory_order_release); cv.notify_all(); @@ -141,7 +140,7 @@ Result wait_unsafe(continuable_base&& continuable, if (auto locked = frame.lock()) { { std::lock_guard rw_lock(locked->rw_mutex); - locked->sync_result = typename Result::from( + locked->sync_result = Result::from( std::forward(args)...); } diff --git a/test/playground/test-playground.cpp b/test/playground/test-playground.cpp index a8761ba..fd43d55 100644 --- a/test/playground/test-playground.cpp +++ b/test/playground/test-playground.cpp @@ -20,15 +20,7 @@ SOFTWARE. **/ -#include -#include -#include #include -#include -#include - -using namespace cti; -using namespace std::chrono_literals; int main(int, char**) { return 0; diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt index 053a6e3..3197347 100644 --- a/test/unit-test/CMakeLists.txt +++ b/test/unit-test/CMakeLists.txt @@ -10,10 +10,8 @@ target_link_libraries(test-continuable-base PUBLIC gtest gtest-main - asio continuable continuable-features-flags - continuable-features-warnings continuable-features-noexcept) add_executable(test-continuable-single @@ -30,6 +28,7 @@ add_executable(test-continuable-single target_link_libraries(test-continuable-single PUBLIC + continuable-features-warnings test-continuable-base) target_include_directories(test-continuable-single @@ -40,6 +39,22 @@ add_test( NAME continuable-unit-tests-single COMMAND test-continuable-single) +add_executable(test-continuable-async + ${CMAKE_CURRENT_LIST_DIR}/async/test-continuable-async.cpp) + +target_link_libraries(test-continuable-async + PUBLIC + test-continuable-base + asio) + +target_include_directories(test-continuable-async + PUBLIC + ${CMAKE_CURRENT_LIST_DIR}/async) + +add_test( + NAME continuable-unit-tests-async + COMMAND test-continuable-async) + if (CTI_CONTINUABLE_WITH_LIGHT_TESTS) set(STEP_RANGE 0) else() @@ -75,6 +90,7 @@ foreach(STEP RANGE ${STEP_RANGE}) target_link_libraries(${PROJECT_NAME} PUBLIC + continuable-features-warnings test-continuable-base) target_include_directories(${PROJECT_NAME} @@ -99,6 +115,7 @@ add_executable(${PROJECT_NAME} ${TEST_SOURCES}) target_link_libraries(${PROJECT_NAME} PUBLIC + continuable-features-warnings test-continuable-base) target_include_directories(${PROJECT_NAME} diff --git a/test/unit-test/async/test-continuable-async.cpp b/test/unit-test/async/test-continuable-async.cpp new file mode 100644 index 0000000..84ad601 --- /dev/null +++ b/test/unit-test/async/test-continuable-async.cpp @@ -0,0 +1,137 @@ + +/* + Copyright(c) 2015 - 2019 Denis Blank + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files(the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and / or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions : + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +**/ + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace cti; +using namespace std::chrono_literals; + +class async_test_helper { +public: + async_test_helper() + : context_(1) + , timer_(context_) + , work_(std::make_shared(context_)) + , thread_([&] { + context_.run(); + }) {} + + ~async_test_helper() { + assert(work_); + timer_.cancel(); + work_.reset(); + thread_.join(); + } + + auto wait_for(asio::steady_timer::duration duration) { + timer_.expires_after(duration); + return timer_.async_wait(use_continuable); + } + +private: + asio::io_context context_; + asio::steady_timer timer_; + std::shared_ptr work_; + std::thread thread_; +}; + +#ifdef CONTINUABLE_HAS_EXCEPTIONS +TYPED_TEST(single_dimension_tests, wait_test_sync) { + // We test here whether it deadlocks automatically + this->supply().apply(cti::transforms::wait()); + + ASSERT_EQ(this->supply(36354).apply(cti::transforms::wait()), 36354); + + ASSERT_EQ(this->supply(47463, 3746).apply(cti::transforms::wait()), + std::make_tuple(47463, 3746)); +} + +TYPED_TEST(single_dimension_tests, wait_test_async) { + { + async_test_helper helper; + helper.wait_for(50ms).then(this->supply()).apply(cti::transforms::wait()); + } + + { + async_test_helper helper; + ASSERT_EQ(helper.wait_for(50ms) + .then(this->supply(36354)) + .apply(cti::transforms::wait()), + 36354); + } + + { + async_test_helper helper; + ASSERT_EQ(helper.wait_for(50ms) + .then(this->supply(47463, 3746)) + .apply(cti::transforms::wait()), + std::make_tuple(47463, 3746)); + } +} + +TYPED_TEST(single_dimension_tests, wait_test_ready) { + make_ready_continuable().apply(cti::transforms::wait()); + + ASSERT_EQ(make_ready_continuable(36354).apply(cti::transforms::wait()), + 36354); + + ASSERT_EQ(make_ready_continuable(47463, 3746).apply(cti::transforms::wait()), + std::make_tuple(47463, 3746)); +} + +TYPED_TEST(single_dimension_tests, wait_test_exception) { + ASSERT_THROW(make_exceptional_continuable(supply_test_exception()) + .apply(cti::transforms::wait()), + test_exception); +} +#endif // CONTINUABLE_HAS_EXCEPTIONS + +TYPED_TEST(single_dimension_tests, wait_for_test_sync) { + this->supply().apply(cti::transforms::wait_for(50ms)); + + ASSERT_EQ( + this->supply(36354).apply(cti::transforms::wait_for(50ms)).get_value(), + 36354); + + ASSERT_EQ(this->supply(47463, 3746) + .apply(cti::transforms::wait_for(50ms)) + .get_value(), + std::make_tuple(47463, 3746)); + + ASSERT_TRUE(make_continuable([](auto&&) {}) + .apply(cti::transforms::wait_for(50ms)) + .is_empty()); +} + +TYPED_TEST(single_dimension_tests, wait_for_test_async) { + async_test_helper helper; + result<> res = helper.wait_for(500ms).apply(cti::transforms::wait_for(50ms)); + ASSERT_FALSE(res.is_exception()); +} diff --git a/test/unit-test/multi/test-continuable-transforms.cpp b/test/unit-test/multi/test-continuable-transforms.cpp index a0d10ab..3f79126 100644 --- a/test/unit-test/multi/test-continuable-transforms.cpp +++ b/test/unit-test/multi/test-continuable-transforms.cpp @@ -25,10 +25,8 @@ #include #include #include -#include +#include #include -#include -#include #include using namespace cti; @@ -73,106 +71,3 @@ TYPED_TEST(single_dimension_tests, to_future_test) { EXPECT_EQ(future.get(), canary); } } - -class async_test_helper { -public: - async_test_helper() - : context_(1) - , timer_(context_) - , work_(std::make_shared(context_)) - , thread_([&] { - context_.run(); - }) {} - - ~async_test_helper() { - assert(work_); - timer_.cancel(); - work_.reset(); - thread_.join(); - } - - auto wait_for(asio::steady_timer::duration duration) { - timer_.expires_after(duration); - return timer_.async_wait(use_continuable); - } - -private: - asio::io_context context_; - asio::steady_timer timer_; - std::shared_ptr work_; - std::thread thread_; -}; - -#ifdef CONTINUABLE_HAS_EXCEPTIONS -TYPED_TEST(single_dimension_tests, wait_test_sync) { - // We test here whether it deadlocks automatically - this->supply().apply(cti::transforms::wait()); - - ASSERT_EQ(this->supply(36354).apply(cti::transforms::wait()), 36354); - - ASSERT_EQ(this->supply(47463, 3746).apply(cti::transforms::wait()), - std::make_tuple(47463, 3746)); -} - -TYPED_TEST(single_dimension_tests, wait_test_async) { - { - async_test_helper helper; - helper.wait_for(50ms).then(this->supply()).apply(cti::transforms::wait()); - } - - { - async_test_helper helper; - ASSERT_EQ(helper.wait_for(50ms) - .then(this->supply(36354)) - .apply(cti::transforms::wait()), - 36354); - } - - { - async_test_helper helper; - ASSERT_EQ(helper.wait_for(50ms) - .then(this->supply(47463, 3746)) - .apply(cti::transforms::wait()), - std::make_tuple(47463, 3746)); - } -} - -TYPED_TEST(single_dimension_tests, wait_test_ready) { - make_ready_continuable().apply(cti::transforms::wait()); - - ASSERT_EQ(make_ready_continuable(36354).apply(cti::transforms::wait()), - 36354); - - ASSERT_EQ(make_ready_continuable(47463, 3746).apply(cti::transforms::wait()), - std::make_tuple(47463, 3746)); -} - -TYPED_TEST(single_dimension_tests, wait_test_exception) { - ASSERT_THROW(make_exceptional_continuable(supply_test_exception()) - .apply(cti::transforms::wait()), - test_exception); -} -#endif // CONTINUABLE_HAS_EXCEPTIONS - -TYPED_TEST(single_dimension_tests, wait_for_test_sync) { - this->supply().apply(cti::transforms::wait_for(50ms)); - - ASSERT_EQ( - this->supply(36354).apply(cti::transforms::wait_for(50ms)).get_value(), - 36354); - - ASSERT_EQ(this->supply(47463, 3746) - .apply(cti::transforms::wait_for(50ms)) - .get_value(), - std::make_tuple(47463, 3746)); - - ASSERT_TRUE(make_continuable([](auto&&) {}) - .apply(cti::transforms::wait_for(50ms)) - .is_empty()); -} - -TYPED_TEST(single_dimension_tests, wait_for_test_async) { - async_test_helper helper; - result<> res = helper.wait_for(500ms).apply(cti::transforms::wait_for(50ms)); - ASSERT_FALSE(res.is_exception()); -}