blob: ea7e81cb989ce21ceef17c0e7727dcf8229309d1 (
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
|
require_relative '../../spec_helper'
describe "Thread.handle_interrupt" do
def make_handle_interrupt_thread(interrupt_config, blocking = true)
interrupt_class = Class.new(RuntimeError)
ScratchPad.record []
in_handle_interrupt = Queue.new
can_continue = Queue.new
thread = Thread.new do
begin
Thread.handle_interrupt(interrupt_config) do
begin
in_handle_interrupt << true
if blocking
Thread.pass # Make it clearer the other thread needs to wait for this one to be in #pop
can_continue.pop
else
begin
can_continue.pop(true)
rescue ThreadError
Thread.pass
retry
end
end
rescue interrupt_class
ScratchPad << :interrupted
end
end
rescue interrupt_class
ScratchPad << :deferred
end
end
in_handle_interrupt.pop
if blocking
# Ensure the thread is inside Thread#pop, as if thread.raise is done before it would be deferred
Thread.pass until thread.stop?
end
thread.raise interrupt_class, "interrupt"
can_continue << true
thread.join
ScratchPad.recorded
end
before :each do
Thread.pending_interrupt?.should == false # sanity check
end
it "with :never defers interrupts until exiting the handle_interrupt block" do
make_handle_interrupt_thread(RuntimeError => :never).should == [:deferred]
end
it "with :on_blocking defers interrupts until the next blocking call" do
make_handle_interrupt_thread(RuntimeError => :on_blocking).should == [:interrupted]
make_handle_interrupt_thread({ RuntimeError => :on_blocking }, false).should == [:deferred]
end
it "with :immediate handles interrupts immediately" do
make_handle_interrupt_thread(RuntimeError => :immediate).should == [:interrupted]
end
it "with :immediate immediately runs pending interrupts, before the block" do
Thread.handle_interrupt(RuntimeError => :never) do
current = Thread.current
Thread.new {
current.raise "interrupt immediate"
}.join
Thread.pending_interrupt?.should == true
-> {
Thread.handle_interrupt(RuntimeError => :immediate) {
flunk "not reached"
}
}.should raise_error(RuntimeError, "interrupt immediate")
Thread.pending_interrupt?.should == false
end
end
it "also works with suspended Fibers and does not duplicate interrupts" do
fiber = Fiber.new { Fiber.yield }
fiber.resume
Thread.handle_interrupt(RuntimeError => :never) do
current = Thread.current
Thread.new {
current.raise "interrupt with fibers"
}.join
Thread.pending_interrupt?.should == true
-> {
Thread.handle_interrupt(RuntimeError => :immediate) {
flunk "not reached"
}
}.should raise_error(RuntimeError, "interrupt with fibers")
Thread.pending_interrupt?.should == false
end
fiber.resume
end
it "runs pending interrupts at the end of the block, even if there was an exception raised in the block" do
executed = false
-> {
Thread.handle_interrupt(RuntimeError => :never) do
current = Thread.current
Thread.new {
current.raise "interrupt exception"
}.join
Thread.pending_interrupt?.should == true
executed = true
raise "regular exception"
end
}.should raise_error(RuntimeError, "interrupt exception")
executed.should == true
end
it "supports multiple pairs in the Hash" do
make_handle_interrupt_thread(ArgumentError => :never, RuntimeError => :never).should == [:deferred]
end
end
|