2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
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
|
2014-05-29 08:42:14 -04:00
|
|
|
IO.pipe {|r, w|
|
|
|
|
return if flush_test(r, w)
|
|
|
|
}
|
|
|
|
require 'socket';
|
|
|
|
Socket.pair(:INET, :STREAM) {|s1, s2|
|
|
|
|
return if flush_test(s1, s2)
|
|
|
|
}
|
|
|
|
skip "nonblocking IO did not work"
|
2010-06-03 00:05:20 -04:00
|
|
|
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 = ""
|
2015-07-13 06:07:01 -04:00
|
|
|
Timeout.timeout(10) {
|
2014-06-05 05:16:57 -04:00
|
|
|
t0 = Thread.new {
|
2007-12-30 07:42:20 -05:00
|
|
|
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}
|
2014-06-05 05:16:57 -04:00
|
|
|
t0.join
|
2007-12-29 13:39:43 -05:00
|
|
|
}
|
2004-12-07 13:26:12 -05:00
|
|
|
assert_equal(4097, result.size)
|
2011-02-12 19:56:48 -05:00
|
|
|
true
|
2004-11-29 00:00:46 -05:00
|
|
|
end
|
2004-11-29 01:22:42 -05:00
|
|
|
end if IO.method_defined?(:nonblock)
|