Skip to content

Commit

Permalink
Back to 100% coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 8, 2024
1 parent dd250ba commit acdc04e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
13 changes: 3 additions & 10 deletions lib/protocol/http/body/streamable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -182,10 +179,6 @@ def stream(input)
@input&.write(chunk)
end
@input&.close_write
rescue => error
raise
ensure
self.close(error)
end
end
end
Expand Down
29 changes: 27 additions & 2 deletions test/protocol/http/body/streamable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -136,6 +135,7 @@
expect(stream.string).to be == "Hello"

body.stream(nil)
body.close
end
end
end
Expand All @@ -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

Expand Down Expand Up @@ -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

0 comments on commit acdc04e

Please sign in to comment.