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 tests for Kernel.sleep calling the Fiber scheduler hook #1094

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
30 changes: 30 additions & 0 deletions core/fiber/fixtures/scheduler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module FiberSpecs

class LoggingScheduler
attr_reader :events
def initialize
@events = []
end

def block(*args)
@events << { event: :block, fiber: Fiber.current, args: args }
Fiber.yield
end

def io_wait(*args)
@events << { event: :io_wait, fiber: Fiber.current, args: args }
Fiber.yield
end

def kernel_sleep(*args)
@events << { event: :kernel_sleep, fiber: Fiber.current, args: args }
Fiber.yield
end

def unblock(*args)
@events << { event: :unblock, fiber: Fiber.current, args: args }
Fiber.yield
end
end

end
36 changes: 36 additions & 0 deletions core/kernel/sleep_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require_relative '../../spec_helper'
require_relative '../fiber/fixtures/scheduler'
require 'fiber'

describe "Kernel#sleep" do
it "is a private method" do
Expand Down Expand Up @@ -73,6 +75,40 @@ def o.divmod(*); [0, 0.001]; end
t.value.should == 5
end
end

context "Kernel.sleep with Fiber scheduler" do
before :each do
Fiber.set_scheduler(FiberSpecs::LoggingScheduler.new)
end

after :each do
Fiber.set_scheduler(nil)
end

it "calls the scheduler without arguments when no duration is given" do
sleeper = Fiber.new(blocking: false) do
sleep
end
sleeper.resume
Fiber.scheduler.events.should == [{ event: :kernel_sleep, fiber: sleeper, args: [] }]
end

it "calls the scheduler with the given duration" do
sleeper = Fiber.new(blocking: false) do
sleep(0.01)
end
sleeper.resume
Fiber.scheduler.events.should == [{ event: :kernel_sleep, fiber: sleeper, args: [0.01] }]
end

it "does not call the scheduler if the fiber is blocking" do
sleeper = Fiber.new(blocking: true) do
sleep(0.01)
end
sleeper.resume
Fiber.scheduler.events.should == []
end
end
end

describe "Kernel.sleep" do
Expand Down