Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specs for Fiber.scheduler and Fiber.set_scheduler #1064

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/fiber/scheduler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative '../../spec_helper'
require_relative 'shared/scheduler'

require "fiber"

describe "Fiber.scheduler" do
it_behaves_like :scheduler, :scheduler
end
8 changes: 8 additions & 0 deletions core/fiber/set_scheduler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative '../../spec_helper'
require_relative 'shared/scheduler'

require "fiber"

describe "Fiber.scheduler" do
it_behaves_like :scheduler, :set_scheduler
end
49 changes: 49 additions & 0 deletions core/fiber/shared/scheduler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
describe :scheduler, shared: true do
it "validates the scheduler for required methods" do
required_methods = [:block, :unblock, :kernel_sleep, :io_wait]
required_methods.each do |missing_method|
scheduler = Object.new
required_methods.difference([missing_method]).each do |method|
scheduler.define_singleton_method(method) {}
end
-> { Fiber.set_scheduler(scheduler) }.should raise_error(ArgumentError, /Scheduler must implement ##{missing_method}/)
end
end

it "can set and get the scheduler" do
required_methods = [:block, :unblock, :kernel_sleep, :io_wait]
scheduler = Object.new
required_methods.each do |method|
scheduler.define_singleton_method(method) {}
end
Fiber.set_scheduler(scheduler)
Fiber.scheduler.should == scheduler
end

it "returns the scheduler after setting it" do
required_methods = [:block, :unblock, :kernel_sleep, :io_wait]
scheduler = Object.new
required_methods.each do |method|
scheduler.define_singleton_method(method) {}
end
result = Fiber.set_scheduler(scheduler)
result.should == scheduler
end

it "can remove the scheduler" do
required_methods = [:block, :unblock, :kernel_sleep, :io_wait]
scheduler = Object.new
required_methods.each do |method|
scheduler.define_singleton_method(method) {}
end
Fiber.set_scheduler(scheduler)
Fiber.set_scheduler(nil)
Fiber.scheduler.should be_nil
end

it "can assign a nil scheduler multiple times" do
Fiber.set_scheduler(nil)
Fiber.set_scheduler(nil)
Fiber.scheduler.should be_nil
end
end