1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/ruby/library/monitor/synchronize_spec.rb

42 lines
1.1 KiB
Ruby
Raw Normal View History

2021-03-27 13:02:41 +01:00
require_relative '../../spec_helper'
require 'monitor'
describe "Monitor#synchronize" do
it "unlocks after return, even if it was interrupted by Thread#raise" do
exc_class = Class.new(RuntimeError)
monitor = Monitor.new
10.times do
2021-07-29 22:11:21 +02:00
wait_q = Queue.new
continue_q = Queue.new
2021-03-27 13:02:41 +01:00
thread = Thread.new do
begin
monitor.synchronize do
2021-07-29 22:11:21 +02:00
wait_q << true
2021-03-27 13:02:41 +01:00
# Do not wait here, we are trying to interrupt the ensure part of #synchronize
end
2021-07-29 22:11:21 +02:00
continue_q.pop
2021-03-27 13:02:41 +01:00
rescue exc_class
monitor.should_not.mon_locked?
:ok
end
end
2021-07-29 22:11:21 +02:00
wait_q.pop
2021-03-27 13:02:41 +01:00
thread.raise exc_class, "interrupt"
2021-07-29 22:11:21 +02:00
continue_q << true
2021-03-27 13:02:41 +01:00
thread.value.should == :ok
end
end
2021-07-29 22:11:21 +02:00
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
2021-03-27 13:02:41 +01:00
end