2018-03-04 15:09:32 +00:00
|
|
|
require_relative '../../spec_helper'
|
2017-05-07 12:04:49 +00:00
|
|
|
|
2020-09-15 21:54:31 +02:00
|
|
|
ruby_version_is ''...'3.0' do
|
2020-07-28 10:58:37 +09:00
|
|
|
describe "Thread.exclusive" do
|
|
|
|
before :each do
|
|
|
|
ScratchPad.clear
|
|
|
|
$VERBOSE, @verbose = nil, $VERBOSE
|
|
|
|
end
|
2019-10-14 15:42:27 +09:00
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
after :each do
|
|
|
|
$VERBOSE = @verbose
|
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
it "yields to the block" do
|
|
|
|
Thread.exclusive { ScratchPad.record true }
|
|
|
|
ScratchPad.recorded.should == true
|
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
it "returns the result of yielding" do
|
|
|
|
Thread.exclusive { :result }.should == :result
|
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
it "blocks the caller if another thread is also in an exclusive block" do
|
|
|
|
m = Mutex.new
|
|
|
|
q1 = Queue.new
|
|
|
|
q2 = Queue.new
|
2018-11-27 20:38:57 +00:00
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
t = Thread.new {
|
|
|
|
Thread.exclusive {
|
|
|
|
q1.push :ready
|
|
|
|
q2.pop
|
|
|
|
}
|
2018-11-27 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
q1.pop.should == :ready
|
2018-11-27 20:38:57 +00:00
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
-> { Thread.exclusive { } }.should block_caller
|
2018-11-27 20:38:57 +00:00
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
q2.push :done
|
|
|
|
t.join
|
|
|
|
end
|
2018-11-27 20:38:57 +00:00
|
|
|
|
2020-07-28 10:58:37 +09:00
|
|
|
it "is not recursive" do
|
|
|
|
Thread.exclusive do
|
|
|
|
-> { Thread.exclusive { } }.should raise_error(ThreadError)
|
|
|
|
end
|
2018-11-27 20:38:57 +00:00
|
|
|
end
|
|
|
|
end
|
2017-05-07 12:04:49 +00:00
|
|
|
end
|