2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2004-11-29 08:00:46 +03:00
|
|
|
require 'test/unit'
|
2007-12-29 21:39:43 +03:00
|
|
|
require 'timeout'
|
2004-11-29 09:22:42 +03:00
|
|
|
begin
|
|
|
|
require 'io/nonblock'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2004-11-29 08:00:46 +03:00
|
|
|
|
|
|
|
class TestIONonblock < Test::Unit::TestCase
|
2007-11-18 10:18:56 +03:00
|
|
|
def test_flush
|
2014-05-29 16: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 08:05:20 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def flush_test(r, w)
|
|
|
|
begin
|
|
|
|
w.nonblock = true
|
|
|
|
rescue Errno::EBADF
|
|
|
|
return false
|
|
|
|
end
|
2004-11-29 08:00:46 +03:00
|
|
|
w.sync = false
|
|
|
|
w << "b"
|
|
|
|
w.flush
|
|
|
|
w << "a" * 4096
|
2004-11-29 08:21:47 +03:00
|
|
|
result = ""
|
2015-07-13 13:07:01 +03:00
|
|
|
Timeout.timeout(10) {
|
2014-06-05 13:16:57 +04:00
|
|
|
t0 = Thread.new {
|
2007-12-30 15:42:20 +03:00
|
|
|
Thread.pass
|
|
|
|
w.close
|
|
|
|
}
|
|
|
|
t = Thread.new {
|
|
|
|
while (Thread.pass; s = r.read(4096))
|
|
|
|
result << s
|
|
|
|
end
|
|
|
|
}
|
2008-08-05 13: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 21:39:43 +03:00
|
|
|
assert_nothing_raised {t.join}
|
2014-06-05 13:16:57 +04:00
|
|
|
t0.join
|
2007-12-29 21:39:43 +03:00
|
|
|
}
|
2004-12-07 21:26:12 +03:00
|
|
|
assert_equal(4097, result.size)
|
2011-02-13 03:56:48 +03:00
|
|
|
true
|
2004-11-29 08:00:46 +03:00
|
|
|
end
|
2017-09-04 16:25:01 +03:00
|
|
|
|
|
|
|
def test_nonblock
|
|
|
|
IO.pipe {|r, w|
|
|
|
|
assert_equal(false, w.nonblock?)
|
|
|
|
w.nonblock do
|
|
|
|
assert_equal(true, w.nonblock?)
|
|
|
|
w.nonblock(false) do
|
|
|
|
assert_equal(false, w.nonblock?)
|
|
|
|
w.nonblock(false) do
|
|
|
|
assert_equal(false, w.nonblock?)
|
|
|
|
end
|
|
|
|
assert_equal(false, w.nonblock?)
|
|
|
|
end
|
|
|
|
assert_equal(true, w.nonblock?)
|
|
|
|
end
|
|
|
|
assert_equal(false, w.nonblock?)
|
|
|
|
}
|
|
|
|
end
|
2004-11-29 09:22:42 +03:00
|
|
|
end if IO.method_defined?(:nonblock)
|