Skip to content

Commit

Permalink
locking scheme seems ok
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Aug 25, 2009
1 parent 2cf97a4 commit 09e4519
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/ut_2_exclusive.rb
Expand Up @@ -13,6 +13,7 @@
class ExclusiveTest < Test::Unit::TestCase

def setup
Ruote::Dm::Lock.all.destroy!
end
def teardown
Ruote::Dm::Lock.all.destroy!
Expand All @@ -38,5 +39,45 @@ def test_two_locks

assert_equal true, l1.locked?
end

def test_wait

time_line = []

job = lambda do |locker, sec|

lock = Ruote::Dm.lock(locker, 'locked')

loop do
time_line << [ locker, :acquired, lock.id ]

sleep(sec)
time_line << [ locker, :woke_up ]

time_line << [ locker, :locked?, lock.locked? ]

if lock.locked?
time_line << [ locker, :job_done ]
lock.destroy
break
end

time_line << [ locker, :passing ]
sleep 0.100
end
end

t0 = Thread.new { job.call('a', 0.1) }
t1 = Thread.new { job.call('b', 0.05) }

sleep 1

assert_equal 0, Ruote::Dm::Lock.all.size

#time_line.each { |e| p e }

assert_equal 1, time_line.select { |e| e == [ 'a', :job_done ] }.size
assert_equal 1, time_line.select { |e| e == [ 'b', :job_done ] }.size
end
end

0 comments on commit 09e4519

Please sign in to comment.