From 13cafbf4d139a4f983edf9215668240bb67935a1 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Mon, 25 Mar 2024 14:00:37 +0800 Subject: [PATCH] fix Except --- include/sled/futures/promise.h | 30 +++++++++++++++--------------- src/futures/promise_test.cc | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/sled/futures/promise.h b/include/sled/futures/promise.h index b0d0514..e869167 100644 --- a/include/sled/futures/promise.h +++ b/include/sled/futures/promise.h @@ -715,14 +715,14 @@ public: // resolve/reject // - bool Resolve() { return state_->resolve(); } + bool Resolve() { return state_->Resolve(); } - bool Reject(std::exception_ptr e) noexcept { return state_->reject(e); } + bool Reject(std::exception_ptr e) noexcept { return state_->Reject(e); } template bool Reject(E &&e) { - return state_->reject(std::make_exception_ptr(std::forward(e))); + return state_->Reject(std::make_exception_ptr(std::forward(e))); } // @@ -890,7 +890,7 @@ private: : promise_wait_status::timeout; } - bool resolve() + bool Resolve() { sled::MutexLock lock(&mutex_); // std::unique_lock lock(mutex_); @@ -902,7 +902,7 @@ private: return true; } - bool reject(std::exception_ptr e) noexcept + bool Reject(std::exception_ptr e) noexcept { sled::MutexLock lock(&mutex_); // std::lock_guard guard(mutex_); @@ -923,21 +923,21 @@ private: if (has_reject) { try { eggs::invoke(std::forward(f), e); - n.resolve(); + n.Resolve(); } catch (...) { - n.reject(std::current_exception()); + n.Reject(std::current_exception()); } } else { - n.reject(e); + n.Reject(e); } }; auto resolve_h = [n = next, f = std::forward(on_resolve)]() mutable { try { eggs::invoke(std::forward(f)); - n.resolve(); + n.Resolve(); } catch (...) { - n.reject(std::current_exception()); + n.Reject(std::current_exception()); } }; @@ -954,21 +954,21 @@ private: if (has_reject) { try { auto r = eggs::invoke(std::forward(f), e); - n.resolve(std::move(r)); + n.Resolve(std::move(r)); } catch (...) { - n.reject(std::current_exception()); + n.Reject(std::current_exception()); } } else { - n.reject(e); + n.Reject(e); } }; auto resolve_h = [n = next, f = std::forward(on_resolve)]() mutable { try { auto r = eggs::invoke(std::forward(f)); - n.resolve(std::move(r)); + n.Resolve(std::move(r)); } catch (...) { - n.reject(std::current_exception()); + n.Reject(std::current_exception()); } }; diff --git a/src/futures/promise_test.cc b/src/futures/promise_test.cc index 4efe3a4..80494a8 100644 --- a/src/futures/promise_test.cc +++ b/src/futures/promise_test.cc @@ -35,3 +35,19 @@ TEST(Future, Basic) p.Resolve(1); EXPECT_EQ(future.Get(), 21); } + +TEST(Future, Except) +{ + auto p = sled::Promise(); + p.Resolve(1); + p.GetFuture() + .Then([](int) { throw std::runtime_error("test"); }) + .Except([](std::exception_ptr e) { + try { + std::rethrow_exception(e); + } catch (const std::exception &e) { + EXPECT_STREQ(e.what(), "test"); + } + }) + .Get(); +}