mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 16:56:44 +08:00
Implement the wait transforms tests
This commit is contained in:
parent
ab9669fa2a
commit
c69385be5f
@ -53,6 +53,7 @@ namespace cti {}
|
|||||||
#include <continuable/continuable-promise-base.hpp>
|
#include <continuable/continuable-promise-base.hpp>
|
||||||
#include <continuable/continuable-promisify.hpp>
|
#include <continuable/continuable-promisify.hpp>
|
||||||
#include <continuable/continuable-result.hpp>
|
#include <continuable/continuable-result.hpp>
|
||||||
|
#include <continuable/continuable-transforms.hpp>
|
||||||
#include <continuable/continuable-traverse-async.hpp>
|
#include <continuable/continuable-traverse-async.hpp>
|
||||||
#include <continuable/continuable-traverse.hpp>
|
#include <continuable/continuable-traverse.hpp>
|
||||||
#include <continuable/continuable-types.hpp>
|
#include <continuable/continuable-types.hpp>
|
||||||
|
|||||||
@ -31,28 +31,5 @@ using namespace cti;
|
|||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
int main(int, char**) {
|
int main(int, char**) {
|
||||||
asio::io_context context(1);
|
|
||||||
asio::steady_timer timer(context);
|
|
||||||
auto work = std::make_shared<asio::io_context::work>(context);
|
|
||||||
|
|
||||||
timer.expires_after(5s);
|
|
||||||
|
|
||||||
std::thread thread([&] {
|
|
||||||
context.run();
|
|
||||||
puts("io_context finished");
|
|
||||||
});
|
|
||||||
|
|
||||||
result<int> res = timer.async_wait(cti::use_continuable)
|
|
||||||
.then([] {
|
|
||||||
return 1;
|
|
||||||
})
|
|
||||||
.apply(transforms::wait_for(1s));
|
|
||||||
|
|
||||||
assert(res.is_empty());
|
|
||||||
puts("async_wait finished");
|
|
||||||
work.reset();
|
|
||||||
timer.cancel();
|
|
||||||
|
|
||||||
thread.join();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -86,16 +86,13 @@ public:
|
|||||||
|
|
||||||
~async_test_helper() {
|
~async_test_helper() {
|
||||||
assert(work_);
|
assert(work_);
|
||||||
}
|
timer_.cancel();
|
||||||
|
|
||||||
void stop() {
|
|
||||||
assert(work_);
|
|
||||||
work_.reset();
|
work_.reset();
|
||||||
thread_.join();
|
thread_.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto wait_for(asio::steady_timer::duration duration) {
|
auto wait_for(asio::steady_timer::duration duration) {
|
||||||
timer_.expires_after(std::chrono::seconds(1));
|
timer_.expires_after(duration);
|
||||||
return timer_.async_wait(use_continuable);
|
return timer_.async_wait(use_continuable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,26 +103,76 @@ private:
|
|||||||
std::thread thread_;
|
std::thread thread_;
|
||||||
};
|
};
|
||||||
|
|
||||||
TYPED_TEST(single_dimension_tests, to_wait_test_sync) {
|
#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) {
|
||||||
{
|
{
|
||||||
this->supply().apply(cti::transforms::wait()); //
|
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, to_wait_test_async) {
|
TYPED_TEST(single_dimension_tests, wait_test_ready) {
|
||||||
{
|
make_ready_continuable().apply(cti::transforms::wait());
|
||||||
this->supply().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, to_wait_test_ready) {
|
TYPED_TEST(single_dimension_tests, wait_test_exception) {
|
||||||
{
|
ASSERT_THROW(make_exceptional_continuable<void>(supply_test_exception())
|
||||||
this->supply().apply(cti::transforms::wait()); //
|
.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, to_wait_test_exception) {
|
TYPED_TEST(single_dimension_tests, wait_for_test_async) {
|
||||||
{
|
async_test_helper helper;
|
||||||
this->supply().apply(cti::transforms::wait()); //
|
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