Revert "fix promise"

This reverts commit ded85e7d19.
pull/30994/head
Craig Tiller 2 years ago
parent e85bc008bf
commit c2acef1958
  1. 13
      src/core/lib/event_engine/promise.h
  2. 8
      test/core/event_engine/common_closures_test.cc
  3. 2
      test/core/event_engine/posix/event_poller_posix_test.cc

@ -25,8 +25,8 @@ namespace experimental {
/// A minimal promise implementation. /// A minimal promise implementation.
/// ///
/// This is light-duty, syntactical sugar around cv wait & signal, which is /// This is light-duty, syntactical sugar around cv wait & signal, which is
/// useful in some cases. /// useful in some cases. A more robust implementation is being worked on
/// TODO(ctiller): Find a new name for this type. /// separately.
template <typename T> template <typename T>
class Promise { class Promise {
public: public:
@ -36,10 +36,13 @@ class Promise {
explicit Promise(T&& val) : val_(val) {} explicit Promise(T&& val) : val_(val) {}
// The getter will wait until the setter has been called, and will return the // The getter will wait until the setter has been called, and will return the
// value passed during Set. // value passed during Set.
T& Get() { T& Get() { return WaitWithTimeout(absl::Hours(1)); }
// The getter will wait with timeout until the setter has been called, and
// will return the value passed during Set.
T& WaitWithTimeout(absl::Duration d) {
grpc_core::MutexLock lock(&mu_); grpc_core::MutexLock lock(&mu_);
while (!set_) { if (!set_) {
cv_.Wait(&mu_); cv_.WaitWithTimeout(&mu_, d);
} }
return val_; return val_;
} }

@ -31,7 +31,7 @@ TEST_F(AnyInvocableClosureTest, CallsItsFunction) {
Promise<bool> promise; Promise<bool> promise;
AnyInvocableClosure closure([&promise] { promise.Set(true); }); AnyInvocableClosure closure([&promise] { promise.Set(true); });
closure.Run(); closure.Run();
ASSERT_TRUE(promise.Get()); ASSERT_TRUE(promise.WaitWithTimeout(absl::Seconds(3)));
} }
class SelfDeletingClosureTest : public testing::Test {}; class SelfDeletingClosureTest : public testing::Test {};
@ -41,7 +41,7 @@ TEST_F(SelfDeletingClosureTest, CallsItsFunction) {
auto* closure = auto* closure =
SelfDeletingClosure::Create([&promise] { promise.Set(true); }); SelfDeletingClosure::Create([&promise] { promise.Set(true); });
closure->Run(); closure->Run();
ASSERT_TRUE(promise.Get()); ASSERT_TRUE(promise.WaitWithTimeout(absl::Seconds(3)));
// ASAN should catch if this closure is not deleted // ASAN should catch if this closure is not deleted
} }
@ -52,8 +52,8 @@ TEST_F(SelfDeletingClosureTest, CallsItsFunctionAndIsDestroyed) {
SelfDeletingClosure::Create([&fn_called] { fn_called.Set(true); }, SelfDeletingClosure::Create([&fn_called] { fn_called.Set(true); },
[&destroyed] { destroyed.Set(true); }); [&destroyed] { destroyed.Set(true); });
closure->Run(); closure->Run();
ASSERT_TRUE(fn_called.Get()); ASSERT_TRUE(fn_called.WaitWithTimeout(absl::Seconds(3)));
ASSERT_TRUE(destroyed.Get()); ASSERT_TRUE(destroyed.WaitWithTimeout(absl::Seconds(3)));
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {

@ -680,7 +680,7 @@ class Worker : public grpc_core::DualRefCounted<Worker> {
} }
void Wait() { void Wait() {
EXPECT_TRUE(promise.Get()); EXPECT_TRUE(promise.WaitWithTimeout(absl::Seconds(60)));
WeakUnref(); WeakUnref();
} }

Loading…
Cancel
Save