Skip to content
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
1 change: 1 addition & 0 deletions lib/async/safe/violation_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ViolationError < StandardError
# @parameter current [Fiber] The fiber that attempted to access the object.
def initialize(message = nil, target:, method:, owner:, current:)
@target = target
@object_class = target.class
@method = method
@owner = owner
@current = current
Expand Down
29 changes: 22 additions & 7 deletions test/async/safe/violation_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,35 @@
require "async/safe"

describe Async::Safe::ViolationError do
it "can be serialized to JSON" do
owner_fiber = Fiber.current
current_fiber = Fiber.new{}.tap(&:resume)
error = Async::Safe::ViolationError.new(
let(:owner_fiber) {Fiber.current}
let(:current_fiber) {Fiber.new{}.tap(&:resume)}

let(:error) do
Async::Safe::ViolationError.new(
target: "test_object",
method: :test_method,
owner: owner_fiber,
current: current_fiber
)

end

it "exposes object_class as the class of the target" do
expect(error.object_class).to be == String
end

it "exposes the method name" do
expect(error.method).to be == :test_method
end

it "exposes owner and current fibers" do
expect(error.owner).to be == owner_fiber
expect(error.current).to be == current_fiber
end

it "serializes object_class correctly in as_json" do
json = error.as_json

expect(json[:object_class]).to be == nil # Not set
expect(json[:object_class]).to be == String
expect(json[:method]).to be == :test_method
expect(json[:owner]).to be_a(Hash)
expect(json[:current]).to be_a(Hash)
Expand Down
Loading