Skip to content

Commit

Permalink
More tests and 100% code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 16, 2024
1 parent 1d85bf2 commit ab2a293
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
1 change: 0 additions & 1 deletion lib/protocol/http/body/streamable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ def stream(body)
@input.write(chunk)
end
rescue => error
raise
ensure
@input.close_write(error)
end
Expand Down
67 changes: 65 additions & 2 deletions test/protocol/http/body/streamable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
stream.write(chunk)
end
rescue => error
ensure
stream.close(error)
end
end
Expand All @@ -215,12 +216,74 @@
end
end

body.stream(input)

expect do
body.stream(input)
body.read
end.to raise_exception(RuntimeError, message: be =~ /Oh no!/)
end
end

with "streaming in a different task" do
let(:block) do
proc do |stream|
while chunk = stream.read_partial
stream.write(chunk)
end
rescue => error
ensure
stream.close(error)
end
end

let(:input) {Protocol::HTTP::Body::Writable.new}
let(:output) {Protocol::HTTP::Body::Writable.new}

before do
parent = Async::Task.current

@input_task = parent.async do
body.stream(input)
end

@output_task = parent.async do
while chunk = body.read
output.write(chunk)
end
rescue => error
ensure
output.close_write(error)
end
end

after do
@input_task&.wait
@output_task&.wait
end

it "can stream a chunk" do
input.write("Hello")
input.close_write
expect(output.read).to be == "Hello"
end

it "can stream multiple chunks" do
input.write("Hello")
input.write(" ")
input.write("World")
input.close_write

expect(output.read).to be == "Hello"
expect(output.read).to be == " "
expect(output.read).to be == "World"
end

it "can stream an error" do
input.write("Hello")
input.close_write(RuntimeError.new("Oh no!"))

expect do
body.read
output.read
end.to raise_exception(RuntimeError, message: be =~ /Oh no!/)
end
end
Expand Down

0 comments on commit ab2a293

Please sign in to comment.