Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 67 lines (48 sloc) 1.061 kb
9bbe06c @tobi Added locking class
authored
1 require "test/unit"
2
3 require File.dirname(__FILE__) + "/lock"
4
5 class TestLock < Test::Unit::TestCase
6 def setup
7 $redis.flushall
8 end
9
10 def test_lock
11 run = false
12 Lock.acquire 'a' do
13 run = true
14 end
15
16 assert run
17 end
18
19 def test_expired_lock_is_overtaken
20 $redis['a'] = 5.minutes.ago.to_i
21
22 run = false
23 Lock.acquire 'a' do
24 run = true
25 end
26 assert run
27 end
28
29 def test_cannot_get_lock
30
31 $redis['b'] = 5.minutes.from_now.to_i
32
33 assert_raise(Lock::Error) do
34 Lock.acquire 'b' do
35 raise 'should not happen'
36 end
37 end
38 end
39
40 def test_lock_exclusive_access
41
42 run = false
43
44 results = []
45
46 a = Thread.new do
47 Lock.acquire('a', 1.minute) do
48 sleep 5
49 results << 'first'
50 end
51 end
52
53 b = Thread.new do
54 sleep 1
55
56 Lock.acquire('a', 1.minute) do
57 results << 'second'
58 end
59
60 end
61
62
63 a.join; b.join
64
65 assert_equal ['first', 'second'], results
66 end
67 end
Something went wrong with that request. Please try again.