diff options
Diffstat (limited to 'spec/ruby/core/io/close_spec.rb')
| -rw-r--r-- | spec/ruby/core/io/close_spec.rb | 70 |
1 files changed, 53 insertions, 17 deletions
diff --git a/spec/ruby/core/io/close_spec.rb b/spec/ruby/core/io/close_spec.rb index 0e51ec23d2..afd84ba101 100644 --- a/spec/ruby/core/io/close_spec.rb +++ b/spec/ruby/core/io/close_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe "IO#close" do before :each do @@ -14,7 +14,7 @@ describe "IO#close" do it "closes the stream" do @io.close - @io.closed?.should == true + @io.should.closed? end it "returns nil" do @@ -23,27 +23,64 @@ describe "IO#close" do it "raises an IOError reading from a closed IO" do @io.close - lambda { @io.read }.should raise_error(IOError) + -> { @io.read }.should.raise(IOError) end it "raises an IOError writing to a closed IO" do @io.close - lambda { @io.write "data" }.should raise_error(IOError) + -> { @io.write "data" }.should.raise(IOError) end - ruby_version_is ''...'2.3' do - it "raises an IOError if closed" do - @io.close - lambda { @io.close }.should raise_error(IOError) - end + it 'does not close the stream if autoclose is false' do + other_io = IO.new(@io.fileno) + other_io.autoclose = false + other_io.close + -> { @io.write "data" }.should_not.raise(IOError) end - ruby_version_is "2.3" do - it "does nothing if already closed" do - @io.close + it "does nothing if already closed" do + @io.close + + @io.close.should == nil + end - @io.close.should be_nil - end + it "does not call the #flush method but flushes the stream internally" do + @io.should_not_receive(:flush) + @io.close + @io.should.closed? + end + + it 'raises an IOError with a clear message' do + matching_exception = nil + + -> do + IOSpecs::THREAD_CLOSE_RETRIES.times do + read_io, write_io = IO.pipe + going_to_read = false + + thread = Thread.new do + begin + going_to_read = true + read_io.read + rescue IOError => ioe + if ioe.message == IOSpecs::THREAD_CLOSE_ERROR_MESSAGE + matching_exception = ioe + end + # try again + end + end + + # best attempt to ensure the thread is actually blocked on read + Thread.pass until going_to_read && thread.stop? + sleep(0.001) + + read_io.close + thread.join + write_io.close + + matching_exception&.tap {|ex| raise ex} + end + end.should.raise(IOError, IOSpecs::THREAD_CLOSE_ERROR_MESSAGE) end end @@ -56,7 +93,7 @@ describe "IO#close on an IO.popen stream" do io.close - lambda { io.pid }.should raise_error(IOError) + -> { io.pid }.should.raise(IOError) end it "sets $?" do @@ -79,4 +116,3 @@ describe "IO#close on an IO.popen stream" do end end - |
