Skip to content

Commit ab2a293

Browse files
committed
More tests and 100% code coverage.
1 parent 1d85bf2 commit ab2a293

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

lib/protocol/http/body/streamable.rb

-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ def stream(body)
140140
@input.write(chunk)
141141
end
142142
rescue => error
143-
raise
144143
ensure
145144
@input.close_write(error)
146145
end

test/protocol/http/body/streamable.rb

+65-2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
stream.write(chunk)
191191
end
192192
rescue => error
193+
ensure
193194
stream.close(error)
194195
end
195196
end
@@ -215,12 +216,74 @@
215216
end
216217
end
217218

219+
body.stream(input)
220+
218221
expect do
219-
body.stream(input)
222+
body.read
220223
end.to raise_exception(RuntimeError, message: be =~ /Oh no!/)
224+
end
225+
end
226+
227+
with "streaming in a different task" do
228+
let(:block) do
229+
proc do |stream|
230+
while chunk = stream.read_partial
231+
stream.write(chunk)
232+
end
233+
rescue => error
234+
ensure
235+
stream.close(error)
236+
end
237+
end
238+
239+
let(:input) {Protocol::HTTP::Body::Writable.new}
240+
let(:output) {Protocol::HTTP::Body::Writable.new}
241+
242+
before do
243+
parent = Async::Task.current
244+
245+
@input_task = parent.async do
246+
body.stream(input)
247+
end
248+
249+
@output_task = parent.async do
250+
while chunk = body.read
251+
output.write(chunk)
252+
end
253+
rescue => error
254+
ensure
255+
output.close_write(error)
256+
end
257+
end
258+
259+
after do
260+
@input_task&.wait
261+
@output_task&.wait
262+
end
263+
264+
it "can stream a chunk" do
265+
input.write("Hello")
266+
input.close_write
267+
expect(output.read).to be == "Hello"
268+
end
269+
270+
it "can stream multiple chunks" do
271+
input.write("Hello")
272+
input.write(" ")
273+
input.write("World")
274+
input.close_write
275+
276+
expect(output.read).to be == "Hello"
277+
expect(output.read).to be == " "
278+
expect(output.read).to be == "World"
279+
end
280+
281+
it "can stream an error" do
282+
input.write("Hello")
283+
input.close_write(RuntimeError.new("Oh no!"))
221284

222285
expect do
223-
body.read
286+
output.read
224287
end.to raise_exception(RuntimeError, message: be =~ /Oh no!/)
225288
end
226289
end

0 commit comments

Comments
 (0)