summaryrefslogtreecommitdiff
path: root/spec/ruby/library/monitor
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/monitor')
-rw-r--r--spec/ruby/library/monitor/enter_spec.rb28
-rw-r--r--spec/ruby/library/monitor/new_cond_spec.rb88
-rw-r--r--spec/ruby/library/monitor/synchronize_spec.rb19
-rw-r--r--spec/ruby/library/monitor/try_enter_spec.rb39
4 files changed, 170 insertions, 4 deletions
diff --git a/spec/ruby/library/monitor/enter_spec.rb b/spec/ruby/library/monitor/enter_spec.rb
new file mode 100644
index 0000000000..f523c42087
--- /dev/null
+++ b/spec/ruby/library/monitor/enter_spec.rb
@@ -0,0 +1,28 @@
+require_relative '../../spec_helper'
+require 'monitor'
+
+describe "Monitor#enter" do
+ it "acquires the monitor" do
+ monitor = Monitor.new
+ 10.times do
+ wait_q = Queue.new
+ continue_q = Queue.new
+
+ thread = Thread.new do
+ begin
+ monitor.enter
+ wait_q << true
+ continue_q.pop
+ ensure
+ monitor.exit
+ end
+ end
+
+ wait_q.pop
+ monitor.mon_locked?.should == true
+ continue_q << true
+ thread.join
+ monitor.mon_locked?.should == false
+ end
+ end
+end
diff --git a/spec/ruby/library/monitor/new_cond_spec.rb b/spec/ruby/library/monitor/new_cond_spec.rb
new file mode 100644
index 0000000000..ec25d3f8a2
--- /dev/null
+++ b/spec/ruby/library/monitor/new_cond_spec.rb
@@ -0,0 +1,88 @@
+require_relative '../../spec_helper'
+require 'monitor'
+
+describe "Monitor#new_cond" do
+ it "creates a MonitorMixin::ConditionVariable" do
+ m = Monitor.new
+ c = m.new_cond
+ c.class.should == MonitorMixin::ConditionVariable
+ end
+
+ it 'returns a condition variable which can be waited on by a thread holding the monitor' do
+ m = Monitor.new
+ c = m.new_cond
+
+ 10.times do
+
+ wait_q = Queue.new
+ thread = Thread.new do
+ m.synchronize do
+ wait_q << true
+ c.wait
+ end
+ :done
+ end
+
+ wait_q.pop
+
+ # Synchronize can't happen until the other thread is waiting.
+ m.synchronize { c.signal }
+
+ thread.join
+ thread.value.should == :done
+ end
+ end
+
+ it 'returns a condition variable which can be waited on by a thread holding the monitor inside multiple synchronize blocks' do
+ m = Monitor.new
+ c = m.new_cond
+
+ 10.times do
+
+ wait_q = Queue.new
+ thread = Thread.new do
+ m.synchronize do
+ m.synchronize do
+ wait_q << true
+ c.wait
+ end
+ end
+ :done
+ end
+
+ wait_q.pop
+
+ #No need to wait here as we cannot synchronize until the other thread is waiting.
+ m.synchronize { c.signal }
+
+ thread.join
+ thread.value.should == :done
+ end
+ end
+
+ it 'returns a condition variable which can be signalled by a thread holding the monitor inside multiple synchronize blocks' do
+ m = Monitor.new
+ c = m.new_cond
+
+ 10.times do
+
+ wait_q = Queue.new
+ thread = Thread.new do
+ m.synchronize do
+ wait_q << true
+ c.wait
+ end
+ :done
+ end
+
+ wait_q.pop
+
+ # Synchronize can't happen until the other thread is waiting.
+ m.synchronize { m.synchronize { c.signal } }
+
+ thread.join
+ thread.value.should == :done
+ end
+ end
+
+end
diff --git a/spec/ruby/library/monitor/synchronize_spec.rb b/spec/ruby/library/monitor/synchronize_spec.rb
index d851b98d6c..d78393eb3a 100644
--- a/spec/ruby/library/monitor/synchronize_spec.rb
+++ b/spec/ruby/library/monitor/synchronize_spec.rb
@@ -7,24 +7,35 @@ describe "Monitor#synchronize" do
monitor = Monitor.new
10.times do
- locked = false
+ wait_q = Queue.new
+ continue_q = Queue.new
thread = Thread.new do
begin
monitor.synchronize do
- locked = true
+ wait_q << true
# Do not wait here, we are trying to interrupt the ensure part of #synchronize
end
- sleep # wait for exception if it did not happen yet
+ continue_q.pop
rescue exc_class
monitor.should_not.mon_locked?
:ok
end
end
- Thread.pass until locked
+ wait_q.pop
thread.raise exc_class, "interrupt"
+ continue_q << true
thread.value.should == :ok
end
end
+
+ it "raises a LocalJumpError if not passed a block" do
+ -> { Monitor.new.synchronize }.should raise_error(LocalJumpError)
+ end
+
+ it "raises a thread error if the monitor is not owned on exiting the block" do
+ monitor = Monitor.new
+ -> { monitor.synchronize { monitor.exit } }.should raise_error(ThreadError)
+ end
end
diff --git a/spec/ruby/library/monitor/try_enter_spec.rb b/spec/ruby/library/monitor/try_enter_spec.rb
new file mode 100644
index 0000000000..04b878f720
--- /dev/null
+++ b/spec/ruby/library/monitor/try_enter_spec.rb
@@ -0,0 +1,39 @@
+require_relative '../../spec_helper'
+require 'monitor'
+
+describe "Monitor#try_enter" do
+ it "will acquire a monitor not held by another thread" do
+ monitor = Monitor.new
+ 10.times do
+
+ thread = Thread.new do
+ val = monitor.try_enter
+ monitor.exit if val
+ val
+ end
+
+ thread.join
+ thread.value.should == true
+ end
+ end
+
+ it "will not acquire a monitor already held by another thread" do
+ monitor = Monitor.new
+ 10.times do
+ monitor.enter
+ begin
+ thread = Thread.new do
+ val = monitor.try_enter
+ monitor.exit if val
+ val
+ end
+
+ thread.join
+ thread.value.should == false
+ ensure
+ monitor.exit
+ end
+ monitor.mon_locked?.should == false
+ end
+ end
+end