aboutsummaryrefslogtreecommitdiffstats
path: root/spec/rubyspec/library/thread/shared/queue
diff options
context:
space:
mode:
Diffstat (limited to 'spec/rubyspec/library/thread/shared/queue')
-rw-r--r--spec/rubyspec/library/thread/shared/queue/clear.rb10
-rw-r--r--spec/rubyspec/library/thread/shared/queue/close.rb26
-rw-r--r--spec/rubyspec/library/thread/shared/queue/closed.rb12
-rw-r--r--spec/rubyspec/library/thread/shared/queue/deque.rb37
-rw-r--r--spec/rubyspec/library/thread/shared/queue/empty.rb12
-rw-r--r--spec/rubyspec/library/thread/shared/queue/enque.rb10
-rw-r--r--spec/rubyspec/library/thread/shared/queue/length.rb9
-rw-r--r--spec/rubyspec/library/thread/shared/queue/num_waiting.rb16
8 files changed, 132 insertions, 0 deletions
diff --git a/spec/rubyspec/library/thread/shared/queue/clear.rb b/spec/rubyspec/library/thread/shared/queue/clear.rb
new file mode 100644
index 0000000000..59ea37d615
--- /dev/null
+++ b/spec/rubyspec/library/thread/shared/queue/clear.rb
@@ -0,0 +1,10 @@
+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
+end
diff --git a/spec/rubyspec/library/thread/shared/queue/close.rb b/spec/rubyspec/library/thread/shared/queue/close.rb
new file mode 100644
index 0000000000..4457f3ae8b
--- /dev/null
+++ b/spec/rubyspec/library/thread/shared/queue/close.rb
@@ -0,0 +1,26 @@
+describe :queue_close, shared: true do
+ it "closes the queue and returns nil for further #pop" do
+ q = @object.call
+ q << 1
+ q.close
+ q.pop.should == 1
+ q.pop.should == nil
+ q.pop.should == nil
+ end
+
+ it "prevents further #push" do
+ q = @object.call
+ q.close
+ lambda {
+ q << 1
+ }.should raise_error(ClosedQueueError)
+ end
+
+ 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
+end
diff --git a/spec/rubyspec/library/thread/shared/queue/closed.rb b/spec/rubyspec/library/thread/shared/queue/closed.rb
new file mode 100644
index 0000000000..b3cea0c524
--- /dev/null
+++ b/spec/rubyspec/library/thread/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/rubyspec/library/thread/shared/queue/deque.rb b/spec/rubyspec/library/thread/shared/queue/deque.rb
new file mode 100644
index 0000000000..1b06dffa2c
--- /dev/null
+++ b/spec/rubyspec/library/thread/shared/queue/deque.rb
@@ -0,0 +1,37 @@
+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 "raises a ThreadError if Queue is empty" do
+ q = @object.call
+ lambda { q.send(@method,true) }.should raise_error(ThreadError)
+ end
+end
diff --git a/spec/rubyspec/library/thread/shared/queue/empty.rb b/spec/rubyspec/library/thread/shared/queue/empty.rb
new file mode 100644
index 0000000000..4acd831d48
--- /dev/null
+++ b/spec/rubyspec/library/thread/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/rubyspec/library/thread/shared/queue/enque.rb b/spec/rubyspec/library/thread/shared/queue/enque.rb
new file mode 100644
index 0000000000..36b98d3a07
--- /dev/null
+++ b/spec/rubyspec/library/thread/shared/queue/enque.rb
@@ -0,0 +1,10 @@
+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
+end
diff --git a/spec/rubyspec/library/thread/shared/queue/length.rb b/spec/rubyspec/library/thread/shared/queue/length.rb
new file mode 100644
index 0000000000..a0143a4e19
--- /dev/null
+++ b/spec/rubyspec/library/thread/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/rubyspec/library/thread/shared/queue/num_waiting.rb b/spec/rubyspec/library/thread/shared/queue/num_waiting.rb
new file mode 100644
index 0000000000..b054951e45
--- /dev/null
+++ b/spec/rubyspec/library/thread/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