Skip to content

Commit bdd70fe

Browse files
committed
Ensure chunks are flushed if required, when streaming.
1 parent 36ec7dd commit bdd70fe

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/protocol/http/body/readable.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def stream?
5757
def call(stream)
5858
while chunk = self.read
5959
stream.write(chunk)
60+
61+
# Flush the stream unless we are immediately expecting more data:
62+
unless self.ready?
63+
stream.flush
64+
end
6065
end
6166
ensure
6267
stream.close

test/protocol/http/body/readable.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Released under the MIT License.
44
# Copyright, 2023-2024, by Samuel Williams.
55

6+
require 'protocol/http/body/stream'
67
require 'protocol/http/body/readable'
78

89
describe Protocol::HTTP::Body::Readable do
@@ -30,11 +31,29 @@
3031
let(:output) {Protocol::HTTP::Body::Buffered.new}
3132
let(:stream) {Protocol::HTTP::Body::Stream.new(nil, output)}
3233

33-
it "can stream data" do
34+
it "can stream (empty) data" do
3435
body.call(stream)
3536

3637
expect(output).to be(:empty?)
3738
end
39+
40+
it "flushes the stream if it is not ready" do
41+
chunks = ["Hello World"]
42+
43+
mock(body) do |mock|
44+
mock.replace(:read) do
45+
chunks.pop
46+
end
47+
48+
mock.replace(:ready?) do
49+
false
50+
end
51+
end
52+
53+
expect(stream).to receive(:flush)
54+
55+
body.call(stream)
56+
end
3857
end
3958

4059
with '#join' do

0 commit comments

Comments
 (0)