Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions lib/protocol/rack/body/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,33 @@ def call(stream)
# Read the next chunk from the response body.
# Returns nil when there are no more chunks.
#
# The body is enumerated in a fiber of its own, and each chunk is transferred back to the reader. Unlike `Enumerator#next`, which relies on `Fiber.yield`, this also returns chunks the body yields from inside a fiber it created.
#
# @returns [String | Nil] The next chunk or nil if there are no more chunks.
def read
@chunks ||= @body.to_enum(:each)
return nil if @chunks == false

@chunks ||= Fiber.new do |reader|
@body.each do |chunk|
@chunks = Fiber.current
reader = reader.transfer(chunk)
nil
end
rescue Exception => error
# Raised in the reader below.
ensure
# After an error, the next read enumerates the body again.
@chunks = error ? nil : false
reader.transfer(error)
end

result = @chunks.transfer(Fiber.current)

# As with `Enumerator#next`, a `StopIteration` raised by the body ends this read.
return nil if result.is_a?(StopIteration)
raise result if result.is_a?(Exception)

return @chunks.next
rescue StopIteration
return nil
return result
end

# Get a string representation of the body.
Expand Down
4 changes: 4 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Releases

## Unreleased

- Return chunks yielded from nested fibers when reading enumerable response bodies.

## v0.23.0

- Respect the result of rewinding the underlying request body.
Expand Down
119 changes: 119 additions & 0 deletions test/protocol/rack/body/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

require "protocol/rack/body/enumerable"

require "sus/fixtures/async/scheduler_context"

describe Protocol::Rack::Body::Enumerable do
with "empty body" do
let(:body) {subject.new([], nil)}
Expand All @@ -30,6 +32,7 @@
expect(body.read).to be == " "
expect(body.read).to be == "World"
expect(body.read).to be_nil
expect(body.read).to be_nil
end

it "returns nil when exhausted" do
Expand All @@ -38,6 +41,122 @@
body.read
expect(body.read).to be_nil
end

it "returns nil to the body from each yield" do
values = []
body = subject.new(Class.new{define_method(:each){|&block| values << block.call("Hello")}}.new, 5)

expect(body.read).to be == "Hello"
expect(body.read).to be_nil
expect(values).to be == [nil]
end

it "can be read from different fibers" do
expect(body.read).to be == "Hello"
expect(Fiber.new{body.read}.resume).to be == " "
expect(body.read).to be == "World"
end

it "closes the wrapped body when closed partway through" do
closed = false
body = subject.new(Rack::BodyProxy.new(["Hello", " ", "World"]){closed = true}, 11)

expect(body.read).to be == "Hello"
body.close

expect(closed).to be == true
end

with "a body that raises" do
let(:failing) do
Class.new do
def each
yield "Hello"
raise "Bad Enumerable"
end
end
end

let(:body) {subject.new(failing.new, nil)}

include Sus::Fixtures::Async::SchedulerContext

it "raises the error to the reader" do
expect(body.read).to be == "Hello"
expect{body.read}.to raise_exception(RuntimeError, message: be =~ /Bad Enumerable/)
end

it "enumerates the body again after the error" do
body.read
expect{body.read}.to raise_exception(RuntimeError)

expect(body.read).to be == "Hello"
end
end

with "a body that raises StopIteration" do
let(:stopping) do
Class.new do
def each
yield "Hello"
raise StopIteration
end
end
end

let(:body) {subject.new(stopping.new, nil)}

it "treats it as the end of the body" do
expect(body.read).to be == "Hello"
expect(body.read).to be_nil

expect(body.read).to be == "Hello"
end
end

with "a body that raises an exception that is not a StandardError" do
let(:failing) do
Class.new do
def each
yield "Hello"
raise Interrupt
end
end
end

let(:body) {subject.new(failing.new, nil)}

include Sus::Fixtures::Async::SchedulerContext

it "raises the exception to the reader" do
expect(body.read).to be == "Hello"
expect{body.read}.to raise_exception(Interrupt)
end
end

with "a body that yields from a nested fiber" do
let(:nested) do
Class.new do
def each(&block)
Fiber.new do
block.call("Hello")
block.call(" ")
end.resume

block.call("World")
end
end
end

let(:body) {subject.new(nested.new, 11)}

it "returns every chunk" do
expect(body.read).to be == "Hello"
expect(body.read).to be == " "
expect(body.read).to be == "World"
expect(body.read).to be_nil
end
end
end

with "#each" do
Expand Down