diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2022-07-26 17:40:00 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2022-08-18 10:07:37 +0200 |
commit | fe61cad7490da8a879597f851f4a89856d44838e (patch) | |
tree | 29844ee84fe8b5c547ab8c58b8464ab450be0873 /spec/ruby/shared | |
parent | b3718edee28d5155ebc383d17ab58867e20f4aa2 (diff) | |
download | ruby-fe61cad7490da8a879597f851f4a89856d44838e.tar.gz |
Implement SizedQueue#push(timeout: sec)
[Feature #18944]
If both `non_block=true` and `timeout:` are supplied, ArgumentError
is raised.
Diffstat (limited to 'spec/ruby/shared')
-rw-r--r-- | spec/ruby/shared/sizedqueue/enque.rb | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/spec/ruby/shared/sizedqueue/enque.rb b/spec/ruby/shared/sizedqueue/enque.rb index 6ef12349f8..126470594a 100644 --- a/spec/ruby/shared/sizedqueue/enque.rb +++ b/spec/ruby/shared/sizedqueue/enque.rb @@ -47,4 +47,61 @@ describe :sizedqueue_enq, shared: true do t.join q.pop.should == 1 end + + describe "with a timeout" do + ruby_version_is "3.2" do + it "returns self if the item was pushed in time" do + q = @object.call(1) + q << 1 + + t = Thread.new { + q.send(@method, 2, timeout: 1).should == q + } + Thread.pass until t.status == "sleep" && q.num_waiting == 1 + q.pop + t.join + end + + it "does nothing if the timeout is nil" do + q = @object.call(1) + q << 1 + t = Thread.new { + q.send(@method, 2, timeout: nil).should == q + } + t.join(0.2).should == nil + q.pop + t.join + end + + it "returns nil if no item is available in time" do + q = @object.call(1) + q << 1 + t = Thread.new { + q.send(@method, 2, timeout: 0.1).should == nil + } + t.join + end + + it "raise TypeError if timeout is not a valid numeric" do + q = @object.call(1) + -> { q.send(@method, 2, timeout: "1") }.should raise_error( + TypeError, + "no implicit conversion to float from string", + ) + + -> { q.send(@method, 2, timeout: false) }.should raise_error( + TypeError, + "no implicit conversion to float from false", + ) + end + + it "raise ArgumentError if non_block = true is passed too" do + q = @object.call(1) + -> { q.send(@method, 2, true, timeout: 1) }.should raise_error( + ArgumentError, + "can't set a timeout if non_block is enabled", + ) + end + end + end end |