Skip to content

Commit

Permalink
Add support for CompositeReadIO#close. (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Feb 7, 2024
1 parent 062e35d commit 751ad80
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/multipart/post/composite_read_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,26 @@ def initialize(*ios)
@index = 0
end

# Close all the underyling IOs.
def close
@ios.each do |io|
io.close if io.respond_to?(:close)
end

@ios = nil
@index = 0
end

def closed?
@ios.nil?
end

# Read from IOs in order until `length` bytes have been received.
def read(length = nil, outbuf = nil)
if @ios.nil?
raise IOError, "CompositeReadIO is closed!"
end

got_result = false
outbuf = outbuf ? outbuf.replace("") : String.new

Expand Down
9 changes: 9 additions & 0 deletions spec/multipart/post/composite_read_io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
MULTIBYTE = File.dirname(__FILE__) + '/../../fixtures/multibyte.txt'

RSpec.shared_context "composite io" do
it "can close all the ios" do
subject.close
expect(subject.closed?).to be_truthy

expect do
subject.read
end.to raise_error(IOError)
end

it "test_full_read_from_several_ios" do
expect(subject.read).to be == 'the quick brown fox'
end
Expand Down

0 comments on commit 751ad80

Please sign in to comment.