From 60defe0a68a40d1b3225cf6b971ea195e19ae2e2 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 6 Oct 2022 15:53:16 +0200 Subject: thread_sync.c: Clarify and document the behavior of timeout == 0 [Feature #18982] Instead of introducing an `exception: false` argument to have `non_block` return nil rather than raise, we can clearly document that a timeout of 0 immediately returns. The code is refactored a bit to avoid doing a time calculation in such case. --- spec/ruby/shared/queue/deque.rb | 7 +++++++ spec/ruby/shared/sizedqueue/enque.rb | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'spec/ruby/shared') diff --git a/spec/ruby/shared/queue/deque.rb b/spec/ruby/shared/queue/deque.rb index ed32bd29c8..616e56ec8a 100644 --- a/spec/ruby/shared/queue/deque.rb +++ b/spec/ruby/shared/queue/deque.rb @@ -87,6 +87,13 @@ describe :queue_deq, shared: true do t.join end + it "immediately returns nil if no item is available and the timeout is 0" do + q = @object.call + q << 1 + q.send(@method, timeout: 0).should == 1 + q.send(@method, timeout: 0).should == nil + end + it "raise TypeError if timeout is not a valid numeric" do q = @object.call -> { q.send(@method, timeout: "1") }.should raise_error( diff --git a/spec/ruby/shared/sizedqueue/enque.rb b/spec/ruby/shared/sizedqueue/enque.rb index 126470594a..059f1025a7 100644 --- a/spec/ruby/shared/sizedqueue/enque.rb +++ b/spec/ruby/shared/sizedqueue/enque.rb @@ -73,7 +73,13 @@ describe :sizedqueue_enq, shared: true do t.join end - it "returns nil if no item is available in time" do + it "returns nil if no space is avialable and timeout is 0" do + q = @object.call(1) + q.send(@method, 1, timeout: 0).should == q + q.send(@method, 2, timeout: 0).should == nil + end + + it "returns nil if no space is available in time" do q = @object.call(1) q << 1 t = Thread.new { -- cgit v1.2.3