Skip to content

Commit acdc04e

Browse files
committed
Back to 100% coverage.
1 parent dd250ba commit acdc04e

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

lib/protocol/http/body/streamable.rb

+3-10
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ def initialize(input, block)
4747
@fiber = Fiber.new do |from|
4848
@from = from
4949
block.call(stream)
50+
rescue => error
51+
# Ignore.
5052
ensure
5153
@fiber = nil
52-
53-
# No more chunks will be generated:
54-
if from = @from
55-
@from = nil
56-
from.transfer(nil)
57-
end
54+
self.close(error)
5855
end
5956
end
6057

@@ -182,10 +179,6 @@ def stream(input)
182179
@input&.write(chunk)
183180
end
184181
@input&.close_write
185-
rescue => error
186-
raise
187-
ensure
188-
self.close(error)
189182
end
190183
end
191184
end

test/protocol/http/body/streamable.rb

+27-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@
127127

128128
it "closes the stream if an error occurs" do
129129
stream = StringIO.new
130-
expect(body).to receive(:close)
131130

132131
expect do
133132
body.call(stream)
@@ -136,6 +135,7 @@
136135
expect(stream.string).to be == "Hello"
137136

138137
body.stream(nil)
138+
body.close
139139
end
140140
end
141141
end
@@ -149,7 +149,10 @@
149149

150150
it "can raise an error on the block" do
151151
expect(body.read).to be == "Hello"
152-
body.close(RuntimeError.new("Oh no!"))
152+
153+
expect do
154+
body.close(RuntimeError.new("Oh no!"))
155+
end.to raise_exception(RuntimeError, message: be =~ /Oh no!/)
153156
end
154157
end
155158

@@ -202,4 +205,26 @@
202205
end
203206
end
204207
end
208+
209+
with "#stream" do
210+
let(:block) do
211+
proc do |stream|
212+
while chunk = stream.read_partial
213+
stream.write(chunk)
214+
end
215+
end
216+
end
217+
218+
it "can stream to output" do
219+
input = Protocol::HTTP::Body::Buffered.new(["Hello", " ", "World"])
220+
221+
body.stream(input)
222+
223+
expect(body.read).to be == "Hello"
224+
expect(body.read).to be == " "
225+
expect(body.read).to be == "World"
226+
227+
body.close
228+
end
229+
end
205230
end

0 commit comments

Comments
 (0)