Skip to content

Add specs for Fiber.blocking? #886

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

Merged
merged 3 commits into from
Oct 28, 2021
Merged
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
62 changes: 62 additions & 0 deletions core/fiber/blocking_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require_relative '../../spec_helper'
require_relative 'shared/blocking'

ruby_version_is "3.0" do
require "fiber"

describe "Fiber.blocking?" do
it_behaves_like :non_blocking_fiber, -> { Fiber.blocking? }

context "when fiber is blocking" do
context "root Fiber of the main thread" do
it "returns 1 for blocking: true" do
fiber = Fiber.new(blocking: true) { Fiber.blocking? }
blocking = fiber.resume

blocking.should == 1
end
end

context "root Fiber of a new thread" do
it "returns 1 for blocking: true" do
thread = Thread.new do
fiber = Fiber.new(blocking: true) { Fiber.blocking? }
blocking = fiber.resume

blocking.should == 1
end

thread.join
end
end
end
end

describe "Fiber#blocking?" do
it_behaves_like :non_blocking_fiber, -> { Fiber.current.blocking? }

context "when fiber is blocking" do
context "root Fiber of the main thread" do
it "returns true for blocking: true" do
fiber = Fiber.new(blocking: true) { Fiber.current.blocking? }
blocking = fiber.resume

blocking.should == true
end
end

context "root Fiber of a new thread" do
it "returns true for blocking: true" do
thread = Thread.new do
fiber = Fiber.new(blocking: true) { Fiber.current.blocking? }
blocking = fiber.resume

blocking.should == true
end

thread.join
end
end
end
end
end
41 changes: 41 additions & 0 deletions core/fiber/shared/blocking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe :non_blocking_fiber, shared: true do
context "root Fiber of the main thread" do
it "returns false" do
fiber = Fiber.new { @method.call }
blocking = fiber.resume

blocking.should == false
end

it "returns false for blocking: false" do
fiber = Fiber.new(blocking: false) { @method.call }
blocking = fiber.resume

blocking.should == false
end
end

context "root Fiber of a new thread" do
it "returns false" do
thread = Thread.new do
fiber = Fiber.new { @method.call }
blocking = fiber.resume

blocking.should == false
end

thread.join
end

it "returns false for blocking: false" do
thread = Thread.new do
fiber = Fiber.new(blocking: false) { @method.call }
blocking = fiber.resume

blocking.should == false
end

thread.join
end
end
end