Skip to content

Commit 476cced

Browse files
authored
Use @callback to track the state of the invocation. (#52)
Setting `@body = nil` can cause other operations to fail.
1 parent 2d4e6ef commit 476cced

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

lib/protocol/http/body/completable.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,20 @@ def initialize(body, callback)
2525
end
2626

2727
def finish
28-
if @body
29-
result = super
30-
31-
@callback.call
32-
33-
@body = nil
34-
35-
return result
28+
super.tap do
29+
if @callback
30+
@callback.call
31+
@callback = nil
32+
end
3633
end
3734
end
3835

3936
def close(error = nil)
40-
if @body
41-
super
42-
43-
@callback.call(error)
44-
45-
@body = nil
37+
super.tap do
38+
if @callback
39+
@callback.call(error)
40+
@callback = nil
41+
end
4642
end
4743
end
4844
end

test/protocol/http/body/completable.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
require 'protocol/http/body/completable'
77
require 'protocol/http/body/buffered'
8+
require 'protocol/http/request'
89

910
describe Protocol::HTTP::Body::Completable do
1011
let(:body) {Protocol::HTTP::Body::Buffered.new}
@@ -76,5 +77,10 @@
7677
completable.finish
7778
end
7879
end
80+
81+
it "doesn't break #read after finishing" do
82+
completable.finish
83+
expect(completable.read).to be_nil
84+
end
7985
end
8086
end

0 commit comments

Comments
 (0)