From 476ccedcdbaeca85f3b77429dc9f0dabc875dd63 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 9 Apr 2024 18:44:21 +1200 Subject: [PATCH] Use `@callback` to track the state of the invocation. (#52) Setting `@body = nil` can cause other operations to fail. --- lib/protocol/http/body/completable.rb | 24 ++++++++++-------------- test/protocol/http/body/completable.rb | 6 ++++++ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/protocol/http/body/completable.rb b/lib/protocol/http/body/completable.rb index a581501..68e152b 100644 --- a/lib/protocol/http/body/completable.rb +++ b/lib/protocol/http/body/completable.rb @@ -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 diff --git a/test/protocol/http/body/completable.rb b/test/protocol/http/body/completable.rb index fdcba8c..17631b6 100644 --- a/test/protocol/http/body/completable.rb +++ b/test/protocol/http/body/completable.rb @@ -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} @@ -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