/
event_spec.rb
177 lines (143 loc) · 4.4 KB
/
event_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module Concurrent
RSpec.describe Event do
subject{ Event.new }
context '#initialize' do
it 'sets the state to unset' do
expect(subject).not_to be_set
end
end
context '#set?' do
it 'returns true when the event has been set' do
subject.set
expect(subject).to be_set
end
it 'returns false if the event is unset' do
expect(subject).not_to be_set
end
end
context '#set' do
it 'triggers the event' do
latch = CountDownLatch.new(1)
t = Thread.new{ subject.wait.tap{ latch.count_down } }
t.join(0.1)
subject.set
expect(latch.wait(1)).to be true
end
it 'sets the state to set' do
subject.set
expect(subject).to be_set
end
end
context '#try?' do
it 'triggers the event if not already set' do
subject.try?
expect(subject).to be_set
end
it 'returns true if not previously set' do
expect(subject.try?).to be true
end
it 'returns false if previously set' do
subject.set
expect(subject.try?).to be false
end
end
context '#reset' do
it 'does not change the state of an unset event' do
subject.reset
expect(subject).not_to be_set
end
it 'does not trigger an unset event' do
latch = CountDownLatch.new(1)
Thread.new{ subject.wait.tap{ latch.count_down } }
subject.reset
expect(latch.wait(0.1)).to be false
end
it 'returns true when called on an unset event' do
expect(subject.reset).to be true
end
it 'sets the state of a set event to unset' do
subject.set
expect(subject).to be_set
subject.reset
expect(subject).not_to be_set
end
it 'returns true when called on a set event' do
subject.set
expect(subject).to be_set
expect(subject.reset).to be true
end
end
context '#wait' do
it 'returns immediately when the event has been set' do
subject.reset
latch = CountDownLatch.new(1)
subject.set
Thread.new{ subject.wait(1000); latch.count_down }
expect(latch.wait(0.1)).to be true
end
it 'returns true once the event is set' do
subject.set
expect(subject.wait).to be true
end
it 'blocks indefinitely when the timer is nil' do
subject.reset
latch = CountDownLatch.new(1)
Thread.new{ subject.wait.tap{ latch.count_down } }
expect(latch.wait(0.1)).to be false
subject.set
expect(latch.wait(0.1)).to be true
end
it 'stops waiting when the timer expires' do
subject.reset
latch = CountDownLatch.new(1)
Thread.new{ subject.wait(0.2); latch.count_down }
expect(latch.wait(0.1)).to be false
expect(latch.wait).to be true
end
it 'returns false when the timer expires' do
subject.reset
expect(subject.wait(1)).to be false
end
it 'triggers multiple waiting threads' do
latch = CountDownLatch.new(5)
subject.reset
5.times{ Thread.new{ subject.wait; latch.count_down } }
subject.set
expect(latch.wait(0.2)).to be true
end
it 'behaves appropriately if wait begins while #set is processing' do
subject.reset
latch = CountDownLatch.new(5)
5.times{ Thread.new{ subject.wait(5) } }
subject.set
5.times{ Thread.new{ subject.wait; latch.count_down } }
expect(latch.wait(0.2)).to be true
end
end
context 'spurious wake ups' do
before(:each) do
def subject.simulate_spurious_wake_up
synchronize do
ns_signal
ns_broadcast
end
end
end
it 'should resist to spurious wake ups without timeout' do
latch = CountDownLatch.new(1)
t = Thread.new{ subject.wait.tap{ latch.count_down } }
t.join(0.1)
subject.simulate_spurious_wake_up
expect(latch.wait(0.1)).to be false
end
it 'should resist spurious wake ups with timeout', buggy: true do
latch = CountDownLatch.new(1)
t = Thread.new{ subject.wait(0.5); latch.count_down }
t.join(0.1)
subject.simulate_spurious_wake_up
expect(latch.wait(0.1)).to be false
expect(latch.wait(1)).to be true
end
end
end
end