1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/spec/ruby/core/fiber/resume_spec.rb

49 lines
1.4 KiB
Ruby
Raw Normal View History

require_relative '../../spec_helper'
require_relative '../../shared/fiber/resume'
2019-04-28 17:20:11 -04:00
describe "Fiber#resume" do
it_behaves_like :fiber_resume, :resume
end
describe "Fiber#resume" do
it "raises a FiberError if the Fiber tries to resume itself" do
fiber = Fiber.new { fiber.resume }
-> { fiber.resume }.should raise_error(FiberError, /double resume/)
end
it "returns control to the calling Fiber if called from one" do
fiber1 = Fiber.new { :fiber1 }
fiber2 = Fiber.new { fiber1.resume; :fiber2 }
fiber2.resume.should == :fiber2
end
2019-04-28 17:20:11 -04:00
# Redmine #595
it "executes the ensure clause" do
code = <<-RUBY
f = Fiber.new do
begin
Fiber.yield
ensure
puts "ensure executed"
end
2019-04-28 17:20:11 -04:00
end
2019-04-28 17:20:11 -04:00
# The apparent issue is that when Fiber.yield executes, control
# "leaves" the "ensure block" and so the ensure clause should run. But
# control really does NOT leave the ensure block when Fiber.yield
# executes. It merely pauses there. To require ensure to run when a
# Fiber is suspended then makes ensure-in-a-Fiber-context different
# than ensure-in-a-Thread-context and this would be very confusing.
f.resume
2019-04-28 17:20:11 -04:00
# When we execute the second #resume call, the ensure block DOES exit,
# the ensure clause runs.
f.resume
exit 0
RUBY
ruby_exe(code).should == "ensure executed\n"
end
end