blob: f15da360fd1277f430441325e147ecb68a0083dd (
plain)
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
|
describe :thread_exit, shared: true do
before :each do
ScratchPad.clear
end
it "kills sleeping thread" do
sleeping_thread = Thread.new do
sleep
ScratchPad.record :after_sleep
end
Thread.pass while sleeping_thread.status and sleeping_thread.status != "sleep"
sleeping_thread.send(@method)
sleeping_thread.join
ScratchPad.recorded.should == nil
end
it "kills current thread" do
thread = Thread.new do
Thread.current.send(@method)
ScratchPad.record :after_sleep
end
thread.join
ScratchPad.recorded.should == nil
end
it "runs ensure clause" do
thread = ThreadSpecs.dying_thread_ensures(@method) { ScratchPad.record :in_ensure_clause }
thread.join
ScratchPad.recorded.should == :in_ensure_clause
end
it "runs nested ensure clauses" do
ScratchPad.record []
@outer = Thread.new do
begin
@inner = Thread.new do
begin
sleep
ensure
ScratchPad << :inner_ensure_clause
end
end
sleep
ensure
ScratchPad << :outer_ensure_clause
@inner.send(@method)
@inner.join
end
end
Thread.pass while @outer.status and @outer.status != "sleep"
Thread.pass until @inner
Thread.pass while @inner.status and @inner.status != "sleep"
@outer.send(@method)
@outer.join
ScratchPad.recorded.should include(:inner_ensure_clause)
ScratchPad.recorded.should include(:outer_ensure_clause)
end
it "does not set $!" do
thread = ThreadSpecs.dying_thread_ensures(@method) { ScratchPad.record $! }
thread.join
ScratchPad.recorded.should == nil
end
it "cannot be rescued" do
thread = Thread.new do
begin
Thread.current.send(@method)
rescue Exception
ScratchPad.record :in_rescue
end
ScratchPad.record :end_of_thread_block
end
thread.join
ScratchPad.recorded.should == nil
end
with_feature :fiber do
it "kills the entire thread when a fiber is active" do
t = Thread.new do
Fiber.new do
sleep
end.resume
ScratchPad.record :fiber_resumed
end
Thread.pass while t.status and t.status != "sleep"
t.send(@method)
t.join
ScratchPad.recorded.should == nil
end
end
# This spec is a mess. It fails randomly, it hangs on MRI, it needs to be removed
quarantine! do
it "killing dying running does nothing" do
in_ensure_clause = false
exit_loop = true
t = ThreadSpecs.dying_thread_ensures do
in_ensure_clause = true
loop { if exit_loop then break end }
ScratchPad.record :after_stop
end
Thread.pass until in_ensure_clause == true
10.times { t.send(@method); Thread.pass }
exit_loop = true
t.join
ScratchPad.recorded.should == :after_stop
end
end
quarantine! do
it "propogates inner exception to Thread.join if there is an outer ensure clause" do
thread = ThreadSpecs.dying_thread_with_outer_ensure(@method) { }
lambda { thread.join }.should raise_error(RuntimeError, "In dying thread")
end
it "runs all outer ensure clauses even if inner ensure clause raises exception" do
ThreadSpecs.join_dying_thread_with_outer_ensure(@method) { ScratchPad.record :in_outer_ensure_clause }
ScratchPad.recorded.should == :in_outer_ensure_clause
end
it "sets $! in outer ensure clause if inner ensure clause raises exception" do
ThreadSpecs.join_dying_thread_with_outer_ensure(@method) { ScratchPad.record $! }
ScratchPad.recorded.to_s.should == "In dying thread"
end
end
it "can be rescued by outer rescue clause when inner ensure clause raises exception" do
thread = Thread.new do
begin
begin
Thread.current.send(@method)
ensure
raise "In dying thread"
end
rescue Exception
ScratchPad.record $!
end
:end_of_thread_block
end
thread.value.should == :end_of_thread_block
ScratchPad.recorded.to_s.should == "In dying thread"
end
it "is deferred if ensure clause does Thread.stop" do
ThreadSpecs.wakeup_dying_sleeping_thread(@method) { Thread.stop; ScratchPad.record :after_sleep }
ScratchPad.recorded.should == :after_sleep
end
# Hangs on 1.8.6.114 OS X, possibly also on Linux
quarantine! do
it "is deferred if ensure clause sleeps" do
ThreadSpecs.wakeup_dying_sleeping_thread(@method) { sleep; ScratchPad.record :after_sleep }
ScratchPad.recorded.should == :after_sleep
end
end
# This case occurred in JRuby where native threads are used to provide
# the same behavior as MRI green threads. Key to this issue was the fact
# that the thread which called #exit in its block was also being explicitly
# sent #join from outside the thread. The 100.times provides a certain
# probability that the deadlock will occur. It was sufficient to reliably
# reproduce the deadlock in JRuby.
it "does not deadlock when called from within the thread while being joined from without" do
100.times do
t = Thread.new { Thread.stop; Thread.current.send(@method) }
Thread.pass while t.status and t.status != "sleep"
t.wakeup.should == t
t.join.should == t
end
end
end
|