diff options
Diffstat (limited to 'spec/ruby/core/io/close_spec.rb')
-rw-r--r-- | spec/ruby/core/io/close_spec.rb | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/spec/ruby/core/io/close_spec.rb b/spec/ruby/core/io/close_spec.rb index dda82b34de..eb560eaf67 100644 --- a/spec/ruby/core/io/close_spec.rb +++ b/spec/ruby/core/io/close_spec.rb @@ -45,19 +45,36 @@ describe "IO#close" do end it 'raises an IOError with a clear message' do - read_io, write_io = IO.pipe - going_to_read = false - thread = Thread.new do - -> do - going_to_read = true - read_io.read - end.should raise_error(IOError, 'stream closed in another thread') - end - - Thread.pass until going_to_read && thread.stop? - read_io.close - thread.join - write_io.close + 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_error(IOError, IOSpecs::THREAD_CLOSE_ERROR_MESSAGE) end end |