summaryrefslogtreecommitdiff
path: root/spec/ruby/shared
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-27 20:38:57 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-27 20:38:57 +0000
commit50441014ffd3645f258e56b9415b7787c910408b (patch)
tree3d5eef5ad1ea7e389dc51fc87437664b984ac184 /spec/ruby/shared
parent49cd16bfaf4f03885058ce748119bc8ea2de735a (diff)
Update to ruby/spec@cdd6ff7
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/shared')
-rw-r--r--spec/ruby/shared/kernel/raise.rb11
-rw-r--r--spec/ruby/shared/queue/clear.rb12
-rw-r--r--spec/ruby/shared/queue/close.rb14
-rw-r--r--spec/ruby/shared/queue/closed.rb12
-rw-r--r--spec/ruby/shared/queue/deque.rb85
-rw-r--r--spec/ruby/shared/queue/empty.rb12
-rw-r--r--spec/ruby/shared/queue/enque.rb18
-rw-r--r--spec/ruby/shared/queue/length.rb9
-rw-r--r--spec/ruby/shared/queue/num_waiting.rb16
-rw-r--r--spec/ruby/shared/sizedqueue/enque.rb50
-rw-r--r--spec/ruby/shared/sizedqueue/max.rb47
-rw-r--r--spec/ruby/shared/sizedqueue/new.rb18
-rw-r--r--spec/ruby/shared/sizedqueue/num_waiting.rb12
13 files changed, 311 insertions, 5 deletions
diff --git a/spec/ruby/shared/kernel/raise.rb b/spec/ruby/shared/kernel/raise.rb
index 4128a636ad..3cff55ac6e 100644
--- a/spec/ruby/shared/kernel/raise.rb
+++ b/spec/ruby/shared/kernel/raise.rb
@@ -44,11 +44,11 @@ describe :kernel_raise, shared: true do
it "re-raises the previously rescued exception if no exception is specified" do
lambda do
begin
- raise Exception, "outer"
+ @object.raise Exception, "outer"
ScratchPad.record :no_abort
rescue
begin
- raise StandardError, "inner"
+ @object.raise StandardError, "inner"
rescue
end
@@ -62,16 +62,17 @@ describe :kernel_raise, shared: true do
it "re-raises a previously rescued exception without overwriting the backtrace" do
begin
- raise 'raised'
+ initial_raise_line = __LINE__; @object.raise 'raised'
rescue => raised
begin
- raise_again_line = __LINE__; raise raised
+ raise_again_line = __LINE__; @object.raise raised
rescue => raised_again
# This spec is written using #backtrace and matching the line number
# from the string, as backtrace_locations is a more advanced
# method that is not always supported by implementations.
- raised_again.backtrace.first.should_not include(":#{raise_again_line}:")
+ raised_again.backtrace.first.should include("#{__FILE__}:#{initial_raise_line}:")
+ raised_again.backtrace.first.should_not include("#{__FILE__}:#{raise_again_line}:")
end
end
end
diff --git a/spec/ruby/shared/queue/clear.rb b/spec/ruby/shared/queue/clear.rb
new file mode 100644
index 0000000000..5db5a5b497
--- /dev/null
+++ b/spec/ruby/shared/queue/clear.rb
@@ -0,0 +1,12 @@
+describe :queue_clear, shared: true do
+ it "removes all objects from the queue" do
+ queue = @object.call
+ queue << Object.new
+ queue << 1
+ queue.empty?.should be_false
+ queue.clear
+ queue.empty?.should be_true
+ end
+
+ # TODO: test for atomicity of Queue#clear
+end
diff --git a/spec/ruby/shared/queue/close.rb b/spec/ruby/shared/queue/close.rb
new file mode 100644
index 0000000000..0e7c69acba
--- /dev/null
+++ b/spec/ruby/shared/queue/close.rb
@@ -0,0 +1,14 @@
+describe :queue_close, shared: true do
+ it "may be called multiple times" do
+ q = @object.call
+ q.close
+ q.closed?.should be_true
+ q.close # no effect
+ q.closed?.should be_true
+ end
+
+ it "returns self" do
+ q = @object.call
+ q.close.should == q
+ end
+end
diff --git a/spec/ruby/shared/queue/closed.rb b/spec/ruby/shared/queue/closed.rb
new file mode 100644
index 0000000000..b3cea0c524
--- /dev/null
+++ b/spec/ruby/shared/queue/closed.rb
@@ -0,0 +1,12 @@
+describe :queue_closed?, shared: true do
+ it "returns false initially" do
+ queue = @object.call
+ queue.closed?.should be_false
+ end
+
+ it "returns true when the queue is closed" do
+ queue = @object.call
+ queue.close
+ queue.closed?.should be_true
+ end
+end
diff --git a/spec/ruby/shared/queue/deque.rb b/spec/ruby/shared/queue/deque.rb
new file mode 100644
index 0000000000..3590f367ba
--- /dev/null
+++ b/spec/ruby/shared/queue/deque.rb
@@ -0,0 +1,85 @@
+describe :queue_deq, shared: true do
+ it "removes an item from the queue" do
+ q = @object.call
+ q << Object.new
+ q.size.should == 1
+ q.send @method
+ q.size.should == 0
+ end
+
+ it "returns items in the order they were added" do
+ q = @object.call
+ q << 1
+ q << 2
+ q.send(@method).should == 1
+ q.send(@method).should == 2
+ end
+
+ it "blocks the thread until there are items in the queue" do
+ q = @object.call
+ v = 0
+
+ th = Thread.new do
+ q.send(@method)
+ v = 1
+ end
+
+ v.should == 0
+ q << Object.new
+ th.join
+ v.should == 1
+ end
+
+ it "removes an item from a closed queue" do
+ q = @object.call
+ q << 1
+ q.close
+ q.send(@method).should == 1
+ end
+
+ it "returns nil for a closed empty queue" do
+ q = @object.call
+ q.close
+ q.send(@method).should == nil
+ end
+
+ it "returns nil for an empty queue that becomes closed" do
+ q = @object.call
+
+ t = Thread.new {
+ q.send(@method).should == nil
+ }
+
+ Thread.pass until t.status == "sleep" && q.num_waiting == 1
+ q.close
+ t.join
+ end
+
+ describe "in non-blocking mode" do
+ it "removes an item from the queue" do
+ q = @object.call
+ q << Object.new
+ q.size.should == 1
+ q.send(@method, true)
+ q.size.should == 0
+ end
+
+ it "raises a ThreadError if the queue is empty" do
+ q = @object.call
+ lambda { q.send(@method, true) }.should raise_error(ThreadError)
+ end
+
+ it "removes an item from a closed queue" do
+ q = @object.call
+ q << 1
+ q.close
+ q.send(@method, true).should == 1
+ end
+
+ it "raises a ThreadError for a closed empty queue" do
+ q = @object.call
+ q.close
+ lambda { q.send(@method, true) }.should raise_error(ThreadError)
+ end
+ end
+end
diff --git a/spec/ruby/shared/queue/empty.rb b/spec/ruby/shared/queue/empty.rb
new file mode 100644
index 0000000000..4acd831d48
--- /dev/null
+++ b/spec/ruby/shared/queue/empty.rb
@@ -0,0 +1,12 @@
+describe :queue_empty?, shared: true do
+ it "returns true on an empty Queue" do
+ queue = @object.call
+ queue.empty?.should be_true
+ end
+
+ it "returns false when Queue is not empty" do
+ queue = @object.call
+ queue << Object.new
+ queue.empty?.should be_false
+ end
+end
diff --git a/spec/ruby/shared/queue/enque.rb b/spec/ruby/shared/queue/enque.rb
new file mode 100644
index 0000000000..ad413e1f46
--- /dev/null
+++ b/spec/ruby/shared/queue/enque.rb
@@ -0,0 +1,18 @@
+describe :queue_enq, shared: true do
+ it "adds an element to the Queue" do
+ q = @object.call
+ q.size.should == 0
+ q.send @method, Object.new
+ q.size.should == 1
+ q.send @method, Object.new
+ q.size.should == 2
+ end
+
+ it "is an error for a closed queue" do
+ q = @object.call
+ q.close
+ lambda {
+ q.send @method, Object.new
+ }.should raise_error(ClosedQueueError)
+ end
+end
diff --git a/spec/ruby/shared/queue/length.rb b/spec/ruby/shared/queue/length.rb
new file mode 100644
index 0000000000..a0143a4e19
--- /dev/null
+++ b/spec/ruby/shared/queue/length.rb
@@ -0,0 +1,9 @@
+describe :queue_length, shared: true do
+ it "returns the number of elements" do
+ q = @object.call
+ q.send(@method).should == 0
+ q << Object.new
+ q << Object.new
+ q.send(@method).should == 2
+ end
+end
diff --git a/spec/ruby/shared/queue/num_waiting.rb b/spec/ruby/shared/queue/num_waiting.rb
new file mode 100644
index 0000000000..b054951e45
--- /dev/null
+++ b/spec/ruby/shared/queue/num_waiting.rb
@@ -0,0 +1,16 @@
+describe :queue_num_waiting, shared: true do
+ it "reports the number of threads waiting on the queue" do
+ q = @object.call
+ threads = []
+
+ 5.times do |i|
+ q.num_waiting.should == i
+ t = Thread.new { q.deq }
+ Thread.pass until q.num_waiting == i+1
+ threads << t
+ end
+
+ threads.each { q.enq Object.new }
+ threads.each {|t| t.join }
+ end
+end
diff --git a/spec/ruby/shared/sizedqueue/enque.rb b/spec/ruby/shared/sizedqueue/enque.rb
new file mode 100644
index 0000000000..b724101269
--- /dev/null
+++ b/spec/ruby/shared/sizedqueue/enque.rb
@@ -0,0 +1,50 @@
+describe :sizedqueue_enq, shared: true do
+ it "blocks if queued elements exceed size" do
+ q = @object.call(1)
+
+ q.size.should == 0
+ q.send(@method, :first_element)
+ q.size.should == 1
+
+ blocked_thread = Thread.new { q.send(@method, :second_element) }
+ sleep 0.01 until blocked_thread.stop?
+
+ q.size.should == 1
+ q.pop.should == :first_element
+
+ blocked_thread.join
+ q.size.should == 1
+ q.pop.should == :second_element
+ q.size.should == 0
+ end
+
+ it "raises a ThreadError if queued elements exceed size when not blocking" do
+ q = @object.call(2)
+
+ non_blocking = true
+ add_to_queue = lambda { q.send(@method, Object.new, non_blocking) }
+
+ q.size.should == 0
+ add_to_queue.call
+ q.size.should == 1
+ add_to_queue.call
+ q.size.should == 2
+ add_to_queue.should raise_error(ThreadError)
+ end
+
+ it "interrupts enqueuing threads with ClosedQueueError when the queue is closed" do
+ q = @object.call(1)
+ q << 1
+
+ t = Thread.new {
+ lambda { q.send(@method, 2) }.should raise_error(ClosedQueueError)
+ }
+
+ Thread.pass until q.num_waiting == 1
+
+ q.close
+
+ t.join
+ q.pop.should == 1
+ end
+end
diff --git a/spec/ruby/shared/sizedqueue/max.rb b/spec/ruby/shared/sizedqueue/max.rb
new file mode 100644
index 0000000000..cd4b47f1c1
--- /dev/null
+++ b/spec/ruby/shared/sizedqueue/max.rb
@@ -0,0 +1,47 @@
+describe :sizedqueue_max, shared: true do
+ it "returns the size of the queue" do
+ q = @object.call(5)
+ q.max.should == 5
+ end
+end
+
+describe :sizedqueue_max=, shared: true do
+ it "sets the size of the queue" do
+ q = @object.call(5)
+ q.max.should == 5
+ q.max = 10
+ q.max.should == 10
+ end
+
+ it "does not remove items already in the queue beyond the maximum" do
+ q = @object.call(5)
+ q.enq 1
+ q.enq 2
+ q.enq 3
+ q.max = 2
+ (q.size > q.max).should be_true
+ q.deq.should == 1
+ q.deq.should == 2
+ q.deq.should == 3
+ end
+
+ it "raises a TypeError when given a non-numeric value" do
+ q = @object.call(5)
+ lambda { q.max = "foo" }.should raise_error(TypeError)
+ lambda { q.max = Object.new }.should raise_error(TypeError)
+ end
+
+ it "raises an argument error when set to zero" do
+ q = @object.call(5)
+ q.max.should == 5
+ lambda { q.max = 0 }.should raise_error(ArgumentError)
+ q.max.should == 5
+ end
+
+ it "raises an argument error when set to a negative number" do
+ q = @object.call(5)
+ q.max.should == 5
+ lambda { q.max = -1 }.should raise_error(ArgumentError)
+ q.max.should == 5
+ end
+end
diff --git a/spec/ruby/shared/sizedqueue/new.rb b/spec/ruby/shared/sizedqueue/new.rb
new file mode 100644
index 0000000000..4439f2a9c6
--- /dev/null
+++ b/spec/ruby/shared/sizedqueue/new.rb
@@ -0,0 +1,18 @@
+describe :sizedqueue_new, shared: true do
+ it "raises a TypeError when the given argument is not Numeric" do
+ lambda { @object.call("foo") }.should raise_error(TypeError)
+ lambda { @object.call(Object.new) }.should raise_error(TypeError)
+ end
+
+ it "raises an argument error when no argument is given" do
+ lambda { @object.call }.should raise_error(ArgumentError)
+ end
+
+ it "raises an argument error when the given argument is zero" do
+ lambda { @object.call(0) }.should raise_error(ArgumentError)
+ end
+
+ it "raises an argument error when the given argument is negative" do
+ lambda { @object.call(-1) }.should raise_error(ArgumentError)
+ end
+end
diff --git a/spec/ruby/shared/sizedqueue/num_waiting.rb b/spec/ruby/shared/sizedqueue/num_waiting.rb
new file mode 100644
index 0000000000..8c31e48ca5
--- /dev/null
+++ b/spec/ruby/shared/sizedqueue/num_waiting.rb
@@ -0,0 +1,12 @@
+describe :sizedqueue_num_waiting, shared: true do
+ it "reports the number of threads waiting to push" do
+ q = @object.call(1)
+ q.push(1)
+ t = Thread.new { q.push(2) }
+ sleep 0.05 until t.stop?
+ q.num_waiting.should == 1
+
+ q.pop
+ t.join
+ end
+end