Skip to content

Commit

Permalink
Use @callback to track the state of the invocation.
Browse files Browse the repository at this point in the history
Setting `@body = nil` can cause other operations to fail.
  • Loading branch information
ioquatix committed Apr 9, 2024
1 parent 2d4e6ef commit dd71918
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/protocol/http/body/completable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,20 @@ def initialize(body, callback)
end

def finish
if @body
result = super

@callback.call

@body = nil

return result
super.tap do
if @callback
@callback.call
@callback = nil
end
end
end

def close(error = nil)
if @body
super

@callback.call(error)

@body = nil
super.tap do
if @callback
@callback.call(error)
@callback = nil
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/protocol/http/body/completable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'protocol/http/body/completable'
require 'protocol/http/body/buffered'
require 'protocol/http/request'

describe Protocol::HTTP::Body::Completable do
let(:body) {Protocol::HTTP::Body::Buffered.new}
Expand Down Expand Up @@ -76,5 +77,10 @@
completable.finish
end
end

it "doesn't break #read after finishing" do
completable.finish
expect(completable.read).to be_nil
end
end
end

0 comments on commit dd71918

Please sign in to comment.