Skip to content

Commit

Permalink
Ensure chunks are flushed if required, when streaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 3, 2024
1 parent 36ec7dd commit bdd70fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/protocol/http/body/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def stream?
def call(stream)
while chunk = self.read
stream.write(chunk)

# Flush the stream unless we are immediately expecting more data:
unless self.ready?
stream.flush
end
end
ensure
stream.close
Expand Down
21 changes: 20 additions & 1 deletion test/protocol/http/body/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Released under the MIT License.
# Copyright, 2023-2024, by Samuel Williams.

require 'protocol/http/body/stream'
require 'protocol/http/body/readable'

describe Protocol::HTTP::Body::Readable do
Expand Down Expand Up @@ -30,11 +31,29 @@
let(:output) {Protocol::HTTP::Body::Buffered.new}
let(:stream) {Protocol::HTTP::Body::Stream.new(nil, output)}

it "can stream data" do
it "can stream (empty) data" do
body.call(stream)

expect(output).to be(:empty?)
end

it "flushes the stream if it is not ready" do
chunks = ["Hello World"]

mock(body) do |mock|
mock.replace(:read) do
chunks.pop
end

mock.replace(:ready?) do
false
end
end

expect(stream).to receive(:flush)

body.call(stream)
end
end

with '#join' do
Expand Down

0 comments on commit bdd70fe

Please sign in to comment.