@@ -58,21 +58,62 @@ def make_handle_interrupt_thread(interrupt_config, blocking = true)
58
58
make_handle_interrupt_thread ( RuntimeError => :immediate ) . should == [ :interrupted ]
59
59
end
60
60
61
- it "with :immediate immediately runs pending interrupts" do
61
+ it "with :immediate immediately runs pending interrupts, before the block " do
62
62
Thread . handle_interrupt ( RuntimeError => :never ) do
63
63
current = Thread . current
64
64
Thread . new {
65
- current . raise "interrupt"
65
+ current . raise "interrupt immediate "
66
66
} . join
67
67
68
+ Thread . pending_interrupt? . should == true
68
69
-> {
69
70
Thread . handle_interrupt ( RuntimeError => :immediate ) {
70
71
flunk "not reached"
71
72
}
72
- } . should raise_error ( RuntimeError , "interrupt" )
73
+ } . should raise_error ( RuntimeError , "interrupt immediate" )
74
+ Thread . pending_interrupt? . should == false
73
75
end
74
76
end
75
77
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
+
76
117
it "supports multiple pairs in the Hash" do
77
118
make_handle_interrupt_thread ( ArgumentError => :never , RuntimeError => :never ) . should == [ :deferred ]
78
119
end
0 commit comments