From acdc04ee8cc768d6fb8b246cd7accd6ba00c0547 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 8 Sep 2024 23:48:13 +1200 Subject: [PATCH] Back to 100% coverage. --- lib/protocol/http/body/streamable.rb | 13 +++--------- test/protocol/http/body/streamable.rb | 29 +++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/protocol/http/body/streamable.rb b/lib/protocol/http/body/streamable.rb index 8317d9c..2c2d990 100644 --- a/lib/protocol/http/body/streamable.rb +++ b/lib/protocol/http/body/streamable.rb @@ -47,14 +47,11 @@ def initialize(input, block) @fiber = Fiber.new do |from| @from = from block.call(stream) + rescue => error + # Ignore. ensure @fiber = nil - - # No more chunks will be generated: - if from = @from - @from = nil - from.transfer(nil) - end + self.close(error) end end @@ -182,10 +179,6 @@ def stream(input) @input&.write(chunk) end @input&.close_write - rescue => error - raise - ensure - self.close(error) end end end diff --git a/test/protocol/http/body/streamable.rb b/test/protocol/http/body/streamable.rb index 4f4277e..bd35b34 100644 --- a/test/protocol/http/body/streamable.rb +++ b/test/protocol/http/body/streamable.rb @@ -127,7 +127,6 @@ it "closes the stream if an error occurs" do stream = StringIO.new - expect(body).to receive(:close) expect do body.call(stream) @@ -136,6 +135,7 @@ expect(stream.string).to be == "Hello" body.stream(nil) + body.close end end end @@ -149,7 +149,10 @@ it "can raise an error on the block" do expect(body.read).to be == "Hello" - body.close(RuntimeError.new("Oh no!")) + + expect do + body.close(RuntimeError.new("Oh no!")) + end.to raise_exception(RuntimeError, message: be =~ /Oh no!/) end end @@ -202,4 +205,26 @@ end end end + + with "#stream" do + let(:block) do + proc do |stream| + while chunk = stream.read_partial + stream.write(chunk) + end + end + end + + it "can stream to output" do + input = Protocol::HTTP::Body::Buffered.new(["Hello", " ", "World"]) + + body.stream(input) + + expect(body.read).to be == "Hello" + expect(body.read).to be == " " + expect(body.read).to be == "World" + + body.close + end + end end