Skip to content

Commit 28d71cd

Browse files
committed
Improve Thread.handle_interrupt specs
* Related: https://bugs.ruby-lang.org/issues/17568
1 parent c507495 commit 28d71cd

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

spec/ruby/core/thread/handle_interrupt_spec.rb

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,62 @@ def make_handle_interrupt_thread(interrupt_config, blocking = true)
5858
make_handle_interrupt_thread(RuntimeError => :immediate).should == [:interrupted]
5959
end
6060

61-
it "with :immediate immediately runs pending interrupts" do
61+
it "with :immediate immediately runs pending interrupts, before the block" do
6262
Thread.handle_interrupt(RuntimeError => :never) do
6363
current = Thread.current
6464
Thread.new {
65-
current.raise "interrupt"
65+
current.raise "interrupt immediate"
6666
}.join
6767

68+
Thread.pending_interrupt?.should == true
6869
-> {
6970
Thread.handle_interrupt(RuntimeError => :immediate) {
7071
flunk "not reached"
7172
}
72-
}.should raise_error(RuntimeError, "interrupt")
73+
}.should raise_error(RuntimeError, "interrupt immediate")
74+
Thread.pending_interrupt?.should == false
7375
end
7476
end
7577

78+
it "also works with suspended Fibers and does not duplicate interrupts" do
79+
fiber = Fiber.new { Fiber.yield }
80+
fiber.resume
81+
82+
Thread.handle_interrupt(RuntimeError => :never) do
83+
current = Thread.current
84+
Thread.new {
85+
current.raise "interrupt with fibers"
86+
}.join
87+
88+
Thread.pending_interrupt?.should == true
89+
-> {
90+
Thread.handle_interrupt(RuntimeError => :immediate) {
91+
flunk "not reached"
92+
}
93+
}.should raise_error(RuntimeError, "interrupt with fibers")
94+
Thread.pending_interrupt?.should == false
95+
end
96+
97+
fiber.resume
98+
end
99+
100+
it "runs pending interrupts at the end of the block, even if there was an exception raised in the block" do
101+
executed = false
102+
-> {
103+
Thread.handle_interrupt(RuntimeError => :never) do
104+
current = Thread.current
105+
Thread.new {
106+
current.raise "interrupt exception"
107+
}.join
108+
109+
Thread.pending_interrupt?.should == true
110+
executed = true
111+
raise "regular exception"
112+
end
113+
}.should raise_error(RuntimeError, "interrupt exception")
114+
executed.should == true
115+
end
116+
76117
it "supports multiple pairs in the Hash" do
77118
make_handle_interrupt_thread(ArgumentError => :never, RuntimeError => :never).should == [:deferred]
78119
end

0 commit comments

Comments
 (0)