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.
///
/// This is light-duty, syntactical sugar around cv wait & signal, which is
/// useful in some cases.
/// TODO(ctiller): Find a new name for this type.
/// useful in some cases. A more robust implementation is being worked on
/// separately.
template <typename T>
class Promise {
public:
@ -36,10 +36,13 @@ class Promise {
explicit Promise(T&& val) : val_(val) {}
// The getter will wait until the setter has been called, and will return the
// 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_);
while (!set_) {
cv_.Wait(&mu_);
if (!set_) {
cv_.WaitWithTimeout(&mu_, d);
}
return val_;
}

@ -31,7 +31,7 @@ TEST_F(AnyInvocableClosureTest, CallsItsFunction) {
Promise<bool> promise;
AnyInvocableClosure closure([&promise] { promise.Set(true); });
closure.Run();
ASSERT_TRUE(promise.Get());
ASSERT_TRUE(promise.WaitWithTimeout(absl::Seconds(3)));
}
class SelfDeletingClosureTest : public testing::Test {};
@ -41,7 +41,7 @@ TEST_F(SelfDeletingClosureTest, CallsItsFunction) {
auto* closure =
SelfDeletingClosure::Create([&promise] { promise.Set(true); });
closure->Run();
ASSERT_TRUE(promise.Get());
ASSERT_TRUE(promise.WaitWithTimeout(absl::Seconds(3)));
// 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); },
[&destroyed] { destroyed.Set(true); });
closure->Run();
ASSERT_TRUE(fn_called.Get());
ASSERT_TRUE(destroyed.Get());
ASSERT_TRUE(fn_called.WaitWithTimeout(absl::Seconds(3)));
ASSERT_TRUE(destroyed.WaitWithTimeout(absl::Seconds(3)));
}
int main(int argc, char** argv) {

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

Loading…
Cancel
Save