Test that coroutines propagate thrown exceptions back to co_await

This commit is contained in:
Denis Blank 2017-12-28 04:16:13 +01:00
parent 6908f22996
commit 5d95b5c3e3
2 changed files with 46 additions and 2 deletions

View File

@ -204,7 +204,7 @@ public:
return *this;
}
expected& operator=(types::error_type error) {
set_error(std::move(error));
set_exception(std::move(error));
return *this;
}
@ -226,7 +226,7 @@ public:
init(std::move(value));
set(detail::slot_t::value);
}
void set_error(types::error_type error) {
void set_exception(types::error_type error) {
weak_destroy();
init(std::move(error));
set(detail::slot_t::error);

View File

@ -23,6 +23,10 @@
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#ifndef CONTINUABLE_WITH_NO_EXCEPTIONS
#include <exception>
#endif // CONTINUABLE_WITH_NO_EXCEPTIONS
#include "test-continuable.hpp"
namespace std {
@ -75,4 +79,44 @@ TYPED_TEST(single_dimension_tests, are_awaitable) {
})));
}
#ifndef CONTINUABLE_WITH_NO_EXCEPTIONS
struct await_exception : std::exception {
char const* what() const noexcept override {
return "await_exception";
}
bool operator==(await_exception const&) const noexcept {
return true;
}
};
/// Resolves the given promise asynchonously through an exception
template <typename S, typename T>
void resolve_async_exceptional(S&& supplier, T&& promise) {
co_await supplier();
ASSERT_THROW(co_await supplier().then([] { throw await_exception{}; }),
await_exception);
promise.set_value();
co_return;
}
TYPED_TEST(single_dimension_tests, are_awaitable_with_exceptions) {
auto const& supply = [&] {
// Supplies the current tested continuable
return this->supply();
};
ASSERT_ASYNC_COMPLETION(
this->supply().then(cti::make_continuable<void>([&](auto&& promise) {
// Resolve the cotinuable through a coroutine
resolve_async_exceptional(supply,
std::forward<decltype(promise)>(promise));
})));
}
#endif // CONTINUABLE_WITH_NO_EXCEPTIONS
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE