mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 16:56:44 +08:00
Split the async tests into their own test case
This commit is contained in:
parent
0fb66a7eec
commit
2c76e6c367
@ -76,8 +76,7 @@ Result wait_relaxed(continuable_base<Data, Annotation>&& continuable) {
|
||||
|
||||
std::move(continuable)
|
||||
.next([&](auto&&... args) {
|
||||
sync_result = typename Result::from(
|
||||
std::forward<decltype(args)>(args)...);
|
||||
sync_result = Result::from(std::forward<decltype(args)>(args)...);
|
||||
|
||||
ready.store(true, std::memory_order_release);
|
||||
cv.notify_all();
|
||||
@ -141,7 +140,7 @@ Result wait_unsafe(continuable_base<Data, Annotation>&& continuable,
|
||||
if (auto locked = frame.lock()) {
|
||||
{
|
||||
std::lock_guard<std::mutex> rw_lock(locked->rw_mutex);
|
||||
locked->sync_result = typename Result::from(
|
||||
locked->sync_result = Result::from(
|
||||
std::forward<decltype(args)>(args)...);
|
||||
}
|
||||
|
||||
|
||||
@ -20,15 +20,7 @@
|
||||
SOFTWARE.
|
||||
**/
|
||||
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <continuable/continuable-transforms.hpp>
|
||||
#include <continuable/continuable.hpp>
|
||||
#include <continuable/external/asio.hpp>
|
||||
#include <asio.hpp>
|
||||
|
||||
using namespace cti;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
int main(int, char**) {
|
||||
return 0;
|
||||
|
||||
@ -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}
|
||||
|
||||
137
test/unit-test/async/test-continuable-async.cpp
Normal file
137
test/unit-test/async/test-continuable-async.cpp
Normal file
@ -0,0 +1,137 @@
|
||||
|
||||
/*
|
||||
Copyright(c) 2015 - 2019 Denis Blank <denis.blank at outlook dot com>
|
||||
|
||||
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 <chrono>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <continuable/continuable-transforms.hpp>
|
||||
#include <continuable/continuable.hpp>
|
||||
#include <continuable/external/asio.hpp>
|
||||
#include <asio.hpp>
|
||||
#include <test-continuable.hpp>
|
||||
|
||||
using namespace cti;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
class async_test_helper {
|
||||
public:
|
||||
async_test_helper()
|
||||
: context_(1)
|
||||
, timer_(context_)
|
||||
, work_(std::make_shared<asio::io_context::work>(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<asio::io_context::work> 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<void>(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<void>([](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());
|
||||
}
|
||||
@ -25,10 +25,8 @@
|
||||
#include <future>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <continuable/continuable-transforms.hpp>
|
||||
#include <continuable/transforms/future.hpp>
|
||||
#include <continuable/continuable.hpp>
|
||||
#include <continuable/external/asio.hpp>
|
||||
#include <asio.hpp>
|
||||
#include <test-continuable.hpp>
|
||||
|
||||
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<asio::io_context::work>(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<asio::io_context::work> 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<void>(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<void>([](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());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user