From 773f581586ba4551b65eb1b81a3e6eb0a8241e84 Mon Sep 17 00:00:00 2001 From: Kasumi Hanazuki Date: Wed, 21 Dec 2022 14:59:42 +0000 Subject: [PATCH] Let `Promises.any_fulfilled_future` take an `Event` This patch teaches `Promises.any_fulfilled_future` to treat a resolved `Event` as a fulfilled `Future` value of `nil`. Closes: https://github.com/ruby-concurrency/concurrent-ruby/issues/963 Co-authored-by: Petr Chalupa --- lib/concurrent-ruby/concurrent/promises.rb | 4 ++-- spec/concurrent/promises_spec.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/concurrent-ruby/concurrent/promises.rb b/lib/concurrent-ruby/concurrent/promises.rb index cbfd7f198..fd2c4d0bd 100644 --- a/lib/concurrent-ruby/concurrent/promises.rb +++ b/lib/concurrent-ruby/concurrent/promises.rb @@ -2074,8 +2074,8 @@ class AnyFulfilledFuturePromise < AnyResolvedFuturePromise private - def resolvable?(countdown, future, index) - future.fulfilled? || + def resolvable?(countdown, event_or_future, index) + (event_or_future.is_a?(Event) ? event_or_future.resolved? : event_or_future.fulfilled?) || # inlined super from BlockedPromise countdown.zero? end diff --git a/spec/concurrent/promises_spec.rb b/spec/concurrent/promises_spec.rb index 1f9e4c5e2..2d4db7454 100644 --- a/spec/concurrent/promises_spec.rb +++ b/spec/concurrent/promises_spec.rb @@ -157,6 +157,24 @@ def behaves_as_delay(delay, value) expect(any.value!).to eq :value end + + it 'treats a resolved Event as a fulfilled Future' do + any = any_fulfilled_future( + resolved_event, + fulfilled_future(:value), + ) + + expect(any.value!).to eq nil + end + + it 'treats a pending Event as a pending Future' do + any = any_fulfilled_future( + resolvable_event, + fulfilled_future(:value), + ) + + expect(any.value!).to eq :value + end end describe '.zip' do