thread_safe 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,84 +0,0 @@
1
- require 'thread_safe/synchronized_delegator.rb'
2
- require File.join(File.dirname(__FILE__), "test_helper")
3
-
4
- class TestSynchronizedDelegator < Minitest::Test
5
-
6
- def test_wraps_array
7
- sync_array = SynchronizedDelegator.new(array = [])
8
-
9
- array << 1
10
- assert_equal 1, sync_array[0]
11
-
12
- sync_array << 2
13
- assert_equal 2, array[1]
14
- end
15
-
16
- def test_synchronizes_access
17
- t1_continue, t2_continue = false, false
18
-
19
- hash = Hash.new do |hash, key|
20
- t2_continue = true
21
- unless hash.find { |e| e[1] == key.to_s } # just to do something
22
- hash[key] = key.to_s
23
- Thread.pass until t1_continue
24
- end
25
- end
26
- sync_hash = SynchronizedDelegator.new(hash)
27
- sync_hash[1] = 'egy'
28
-
29
- t1 = Thread.new do
30
- sync_hash[2] = 'dva'
31
- sync_hash[3] # triggers t2_continue
32
- end
33
-
34
- t2 = Thread.new do
35
- Thread.pass until t2_continue
36
- sync_hash[4] = '42'
37
- end
38
-
39
- sleep(0.05) # sleep some to allow threads to boot
40
-
41
- until t2.status == 'sleep' do
42
- Thread.pass
43
- end
44
-
45
- assert_equal 3, hash.keys.size
46
-
47
- t1_continue = true
48
- t1.join; t2.join
49
-
50
- assert_equal 4, sync_hash.size
51
- end
52
-
53
- def test_synchronizes_access_with_block
54
- t1_continue, t2_continue = false, false
55
-
56
- sync_array = SynchronizedDelegator.new(array = [])
57
-
58
- t1 = Thread.new do
59
- sync_array << 1
60
- sync_array.each do
61
- t2_continue = true
62
- Thread.pass until t1_continue
63
- end
64
- end
65
-
66
- t2 = Thread.new do
67
- # sleep(0.01)
68
- Thread.pass until t2_continue
69
- sync_array << 2
70
- end
71
-
72
- until t2.status == 'sleep' || t2.status == false do
73
- Thread.pass
74
- end
75
-
76
- assert_equal 1, array.size
77
-
78
- t1_continue = true
79
- t1.join; t2.join
80
-
81
- assert_equal [1, 2], array
82
- end
83
-
84
- end