2004-11-29 00:00:46 -05:00
|
|
|
require 'test/unit'
|
2007-12-29 13:39:43 -05:00
|
|
|
require 'timeout'
|
2004-11-29 01:22:42 -05:00
|
|
|
begin
|
|
|
|
require 'io/nonblock'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2004-11-29 00:00:46 -05:00
|
|
|
|
|
|
|
class TestIONonblock < Test::Unit::TestCase
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_flush
|
2010-06-03 00:05:20 -04:00
|
|
|
flush_test(*IO.pipe) or
|
|
|
|
(require 'socket'; flush_test(*Socket.pair(:INET, :STREAM))) or
|
|
|
|
skip "nonblocking IO did not work"
|
|
|
|
end
|
|
|
|
|
|
|
|
def flush_test(r, w)
|
|
|
|
begin
|
|
|
|
w.nonblock = true
|
|
|
|
rescue Errno::EBADF
|
|
|
|
return false
|
|
|
|
end
|
2004-11-29 00:00:46 -05:00
|
|
|
w.sync = false
|
|
|
|
w << "b"
|
|
|
|
w.flush
|
|
|
|
w << "a" * 4096
|
2004-11-29 00:21:47 -05:00
|
|
|
result = ""
|
2007-12-29 13:39:43 -05:00
|
|
|
timeout(10) {
|
2007-12-30 07:42:20 -05:00
|
|
|
Thread.new {
|
|
|
|
Thread.pass
|
|
|
|
w.close
|
|
|
|
}
|
|
|
|
t = Thread.new {
|
|
|
|
while (Thread.pass; s = r.read(4096))
|
|
|
|
result << s
|
|
|
|
end
|
|
|
|
}
|
2008-08-05 05:04:13 -04:00
|
|
|
begin
|
|
|
|
w.flush # assert_raise(IOError, "[ruby-dev:24985]") {w.flush}
|
|
|
|
rescue Errno::EBADF, IOError
|
|
|
|
# ignore [ruby-dev:35638]
|
|
|
|
end
|
2007-12-29 13:39:43 -05:00
|
|
|
assert_nothing_raised {t.join}
|
|
|
|
}
|
2004-12-07 13:26:12 -05:00
|
|
|
assert_equal(4097, result.size)
|
2004-11-29 00:00:46 -05:00
|
|
|
end
|
2004-11-29 01:22:42 -05:00
|
|
|
end if IO.method_defined?(:nonblock)
|