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

Extend tests for Fiber storage #1058

Merged
merged 4 commits into from
Aug 28, 2023
Merged
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
43 changes: 43 additions & 0 deletions core/fiber/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@
it "cannot create a fiber with non-hash storage" do
-> { Fiber.new(storage: 42) {} }.should raise_error(TypeError)
end

it "cannot create a fiber with a frozen hash as storage" do
-> { Fiber.new(storage: {life: 43}.freeze) {} }.should raise_error(FrozenError)
end

it "cannot create a fiber with a storage hash with non-symbol keys" do
-> { Fiber.new(storage: {life: 43, Object.new => 44}) {} }.should raise_error(TypeError)
end
end
end

describe "Fiber#storage" do
ruby_version_is "3.2" do
herwinw marked this conversation as resolved.
Show resolved Hide resolved
it "cannot be accessed from a different fiber" do
f = Fiber.new(storage: {life: 42}) { nil }
-> {
f.storage
}.should raise_error(ArgumentError, /Fiber storage can only be accessed from the Fiber it belongs to/)
end
end
end

Expand Down Expand Up @@ -73,6 +92,17 @@
it "returns nil if the current fiber has no storage" do
Fiber.new { Fiber[:life] }.resume.should be_nil
end

it "can access the storage of the parent fiber" do
f = Fiber.new(storage: {life: 42}) do
Fiber.new { Fiber[:life] }.resume
end
f.resume.should == 42
end

it "can't access the storage of the fiber with non-symbol keys" do
-> { Fiber[Object.new] }.should raise_error(TypeError)
end
end
end

Expand All @@ -89,6 +119,19 @@
it "sets the value of the given key in the storage of the current fiber" do
Fiber.new { Fiber[:life] = 43; Fiber[:life] }.resume.should == 43
end

it "does not overwrite the storage of the parent fiber" do
f = Fiber.new(storage: {life: 42}) do
Fiber.yield Fiber.new { Fiber[:life] = 43; Fiber[:life] }.resume
Fiber[:life]
end
f.resume.should == 43 # Value of the inner fiber
f.resume.should == 42 # Value of the outer fiber
end

it "can't access the storage of the fiber with non-symbol keys" do
-> { Fiber[Object.new] = 44 }.should raise_error(TypeError)
end
end

ruby_version_is "3.3" do
Expand Down