Created
October 25, 2012 12:21
-
-
Save pochi/3952276 to your computer and use it in GitHub Desktop.
Fiber sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'rspec' | |
require 'fiber' | |
describe 'FiberTest' do | |
let(:fiber) do | |
Fiber.new do |name| | |
last_name = Fiber.yield("#{name}") | |
puts "#{name} #{last_name}" | |
end | |
end | |
describe '.current()' do | |
subject { Fiber.current() } | |
it { should be_instance_of(Fiber) } | |
end | |
# Can't yield from root fiber | |
describe '.yield()' do | |
subject { Fiber.yield("pochi") } | |
it do | |
expect { subject }.to raise_exception(FiberError) | |
end | |
end | |
describe '#alive?' do | |
describe 'when fiber alive' do | |
context 'never called' do | |
subject { fiber.alive? } | |
it { should be_true } | |
end | |
context 'once called' do | |
before { fiber.resume("pochi") } | |
subject { fiber.alive? } | |
it { should be_true } | |
end | |
end | |
context 'when fiver dead' do | |
before { 2.times { |_| fiber.resume("pochi") } } | |
subject { fiber.alive? } | |
it { should be_false } | |
end | |
end | |
describe '#resume(name)' do | |
context 'when once called' do | |
subject { fiber.resume("pochi") } | |
it { should == "pochi" } | |
end | |
context "when twice called" do | |
before { fiber.resume("pochi") } | |
subject { fiber.resume("kuroda") } | |
it { should = "kuroda pochi pochi" } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment