From df0fd2f2da85b3ad359f387de068132ecc116bb8 Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Thu, 2 Mar 2017 23:35:49 +0100 Subject: [PATCH] Add tests for checking the correct invalidation on destruction --- include/continuable/continuable-testing.hpp | 16 +++++++++ test/unit-test/CMakeLists.txt | 4 +-- test/unit-test/test-continuable-base.cpp | 36 +++++++++++++++++-- ...on.cpp => test-continuable-regression.cpp} | 2 +- test/unit-test/test-continuable.hpp | 10 +++--- 5 files changed, 58 insertions(+), 10 deletions(-) rename test/unit-test/{test-continuable-recursion.cpp => test-continuable-regression.cpp} (97%) diff --git a/include/continuable/continuable-testing.hpp b/include/continuable/continuable-testing.hpp index e71f022..b29a852 100644 --- a/include/continuable/continuable-testing.hpp +++ b/include/continuable/continuable-testing.hpp @@ -53,6 +53,15 @@ template void expect_async_completion(C&& continuable) { EXPECT_TRUE(called); } +template void expect_async_incomplete(C&& continuable) { + std::forward(continuable).then([](auto&&... args) { + // Workaround for our known GCC bug. + util::unused(std::forward(args)...); + + FAIL(); + }); +} + template void expect_async_validation(C&& continuable, V&& validator) { expect_async_completion( @@ -128,6 +137,13 @@ void assert_async_types(C&& continuable, util::identity expected) { #define EXPECT_ASYNC_COMPLETION(CONTINUABLE) \ cti::detail::testing::expect_async_completion(CONTINUABLE); +/// Expects the final callback of the given continuable is never called +/// with any result. +/// +/// \since version 1.0.0 +#define EXPECT_ASYNC_INCOMPLETE(CONTINUABLE) \ + cti::detail::testing::expect_async_incomplete(CONTINUABLE); + /// Expects the continuation to be called and forwards it's arguments to /// the given validator which can then do assertions on the result. #define EXPECT_ASYNC_VALIDATION(CONTINUABLE, VALIDATOR) \ diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt index 335f2d6..f5333c3 100644 --- a/test/unit-test/CMakeLists.txt +++ b/test/unit-test/CMakeLists.txt @@ -1,4 +1,4 @@ -foreach(STEP RANGE 5) +foreach(STEP RANGE 4) set(PROJECT_NAME test-continuable-${STEP}) set(TEST_NAME continuable-unit-tests-${STEP}) @@ -9,7 +9,7 @@ foreach(STEP RANGE 5) ${CMAKE_CURRENT_LIST_DIR}/test-continuable-connection-any.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-connection-seq.cpp ${CMAKE_CURRENT_LIST_DIR}/test-continuable-erasure.cpp - ${CMAKE_CURRENT_LIST_DIR}/test-continuable-recursion.cpp) + ${CMAKE_CURRENT_LIST_DIR}/test-continuable-regression.cpp) target_link_libraries(${PROJECT_NAME} PRIVATE diff --git a/test/unit-test/test-continuable-base.cpp b/test/unit-test/test-continuable-base.cpp index 280b2a6..7fc63ea 100644 --- a/test/unit-test/test-continuable-base.cpp +++ b/test/unit-test/test-continuable-base.cpp @@ -26,7 +26,7 @@ using namespace cti; using namespace cti::detail; -TYPED_TEST(single_dimension_tests, are_supplyd_on_destruct) { +TYPED_TEST(single_dimension_tests, are_called_on_destruct) { { auto allowed = false; @@ -45,6 +45,38 @@ TYPED_TEST(single_dimension_tests, are_supplyd_on_destruct) { ASSERT_ASYNC_TYPES(this->supply(tag1{}), tag1); } +template auto create_incomplete(T* me) { + return me->make(identity<>{}, identity{}, [](auto&& callback) mutable { + // Destruct the callback here + auto destroy = std::forward(callback); + (void)destroy; + }); +} + +template auto assert_invocation(T* me) { + return me->make(identity<>{}, identity{}, + [](auto&& /*callback*/) mutable { FAIL(); }); +} + +TYPED_TEST(single_dimension_tests, are_incomplete_when_released) { + auto chain = this->supply().then([] { + int i = 0; + }); + chain.release(); + EXPECT_ASYNC_INCOMPLETE(std::move(chain)); +} + +TYPED_TEST(single_dimension_tests, are_not_dispatched_when_released) { + auto chain = assert_invocation(this); + chain.release(); + EXPECT_ASYNC_INCOMPLETE(std::move(chain)); +} + +TYPED_TEST(single_dimension_tests, are_not_finished_when_not_continued) { + auto chain = create_incomplete(this); + EXPECT_ASYNC_INCOMPLETE(std::move(chain)); +} + TYPED_TEST(single_dimension_tests, are_chainable) { EXPECT_ASYNC_RESULT(this->supply().then([] { return; // void @@ -88,7 +120,7 @@ TYPED_TEST(single_dimension_tests, are_chainable) { } } -TYPED_TEST(single_dimension_tests, are_executable_through_dispatchers) { +TYPED_TEST(single_dimension_tests, are_convertible_to_futures) { auto is_ready = [](auto& future) { // Check that the future is ready return future.wait_for(std::chrono::seconds(0)) == diff --git a/test/unit-test/test-continuable-recursion.cpp b/test/unit-test/test-continuable-regression.cpp similarity index 97% rename from test/unit-test/test-continuable-recursion.cpp rename to test/unit-test/test-continuable-regression.cpp index ef1033c..ce39766 100644 --- a/test/unit-test/test-continuable-recursion.cpp +++ b/test/unit-test/test-continuable-regression.cpp @@ -28,7 +28,7 @@ using namespace cti; using namespace cti::detail; -TEST(recursion_tests, are_multiple_args_mergeable) { +TEST(regression_tests, are_multiple_args_mergeable) { { auto tp = std::make_tuple(1, 2, 3); util::merge(tp, tp, tp, tp, tp); diff --git a/test/unit-test/test-continuable.hpp b/test/unit-test/test-continuable.hpp index 28afa23..aa1541b 100644 --- a/test/unit-test/test-continuable.hpp +++ b/test/unit-test/test-continuable.hpp @@ -148,24 +148,24 @@ template struct provide_continuation_seq_right { // Feel free to uncomment more tests, however this will increase the // build time significantly. using single_types = ::testing::Types< -#if UNIT_TEST_STEP == 1 +#if UNIT_TEST_STEP == 0 provide_copyable, // provide_unique, // provide_copyable_erasure, provide_unique_erasure -#elif UNIT_TEST_STEP == 2 +#elif UNIT_TEST_STEP == 1 // Some instantiations out commented for compilation speed reasons // provide_continuation_and_left, provide_continuation_and_left // provide_continuation_and_left, // provide_continuation_and_left, // provide_continuation_and_right, -#elif UNIT_TEST_STEP == 3 +#elif UNIT_TEST_STEP == 2 provide_continuation_and_right // provide_continuation_and_left, -#elif UNIT_TEST_STEP == 4 +#elif UNIT_TEST_STEP == 3 provide_continuation_and_left -#elif UNIT_TEST_STEP == 5 +#elif UNIT_TEST_STEP == 4 provide_continuation_seq_right #endif >;