Moved tests to just semaphore, and fixed thread joining

This commit is contained in:
Marc Siegel 2013-12-26 13:28:40 -05:00
parent e2e70591a1
commit d3d51fef91
2 changed files with 37 additions and 26 deletions

View File

@ -55,26 +55,5 @@ module ProcessShared
mem.get_char(0).must_equal(0) mem.get_char(0).must_equal(0)
end end
def test_allows_other_threads_within_a_process_to_continue_while_locked
was_set = false
@lock.synchronize do
t1 = Thread.new do
# give t2 a chance to wait on the lock, then set the flag
sleep 0.01
was_set = true
end
t2 = Thread.new do
@lock.synchronize { }
end
# t1 should set the flag and die while t2 is still waiting on the lock
t1.join
end
was_set.must_equal(true)
end
end end
end end

View File

@ -91,6 +91,32 @@ module ProcessShared
end end
end end
end end
it 'allows other threads in a process to continue while waiting' do
sem = Semaphore.new
was_set = false
t2 = nil
sem.synchronize do
t1 = Thread.new do
# give t2 a chance to wait on the lock, then set the flag
sleep 0.01
was_set = true
end
t2 = Thread.new do
sem.synchronize { }
end
# t1 should set the flag and die while t2 is still waiting on the lock
t1.join
end
was_set.must_equal true
t2.join
end
end end
describe '#try_wait' do describe '#try_wait' do
@ -133,13 +159,12 @@ module ProcessShared
end end
it 'allows other threads in a process to continue while waiting' do it 'allows other threads in a process to continue while waiting' do
# NOTE: A similar test in LockBehavior tests Semaphore#wait,
# Mutex#lock, etc. Necessary only to test #try_wait here.
start = Time.now.to_f start = Time.now.to_f
sem = Semaphore.new
was_set = false was_set = false
t2 = nil
Semaphore.open(0) do |sem| sem.synchronize do
t1 = Thread.new do t1 = Thread.new do
# give t2 a chance to wait on the lock, then set the flag # give t2 a chance to wait on the lock, then set the flag
sleep 0.01 sleep 0.01
@ -147,14 +172,21 @@ module ProcessShared
end end
t2 = Thread.new do t2 = Thread.new do
sem.try_wait(10.0) begin
sem.try_wait(10.0)
rescue Errno::ETIMEDOUT
# success
end
end end
# t1 should set the flag and die while t2 is still waiting on the lock
t1.join t1.join
end end
was_set.must_equal true was_set.must_equal true
(Time.now.to_f - start).must be_lt(0.1) (Time.now.to_f - start).must be_lt(0.1)
t2.join
end end
end end
end end