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/library/fiber/transfer_spec.rb
eregon 1d15d5f080 Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
  [Misc #13792] [ruby-core:82287]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-20 20:18:52 +00:00

51 lines
1.7 KiB
Ruby

require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../../../shared/fiber/resume', __FILE__)
with_feature :fiber_library do
require 'fiber'
describe "Fiber#transfer" do
it_behaves_like :fiber_resume, :transfer
end
describe "Fiber#transfer" do
it "transfers control from one Fiber to another when called from a Fiber" do
fiber1 = Fiber.new { :fiber1 }
fiber2 = Fiber.new { fiber1.transfer; :fiber2 }
fiber2.resume.should == :fiber1
end
it "returns to the root Fiber when finished" do
f1 = Fiber.new { :fiber_1 }
f2 = Fiber.new { f1.transfer; :fiber_2 }
f2.transfer.should == :fiber_1
f2.transfer.should == :fiber_2
end
it "can be invoked from the same Fiber it transfers control to" do
states = []
fiber = Fiber.new { states << :start; fiber.transfer; states << :end }
fiber.transfer
states.should == [:start, :end]
states = []
fiber = Fiber.new { states << :start; fiber.transfer; states << :end }
fiber.resume
states.should == [:start, :end]
end
it "can transfer control to a Fiber that has transfered to another Fiber" do
states = []
fiber1 = Fiber.new { states << :fiber1 }
fiber2 = Fiber.new { states << :fiber2_start; fiber1.transfer; states << :fiber2_end}
fiber2.resume.should == [:fiber2_start, :fiber1]
fiber2.transfer.should == [:fiber2_start, :fiber1, :fiber2_end]
end
it "raises a FiberError when transferring to a Fiber which resumes itself" do
fiber = Fiber.new { fiber.resume }
lambda { fiber.transfer }.should raise_error(FiberError)
end
end
end