| 1 |
# frozen_string_literal: false |
| 2 |
require 'test/unit' |
| 3 |
require 'timeout' |
| 4 |
begin |
| 5 |
require 'io/nonblock' |
| 6 |
rescue LoadError |
| 7 |
end |
| 8 |
|
| 9 |
class TestIONonblock < Test::Unit::TestCase |
| 10 |
def test_flush |
| 11 |
IO.pipe {|r, w| |
| 12 |
return if flush_test(r, w) |
| 13 |
} |
| 14 |
require 'socket'; |
| 15 |
Socket.pair(:INET, :STREAM) {|s1, s2| |
| 16 |
return if flush_test(s1, s2) |
| 17 |
} |
| 18 |
omit "nonblocking IO did not work" |
| 19 |
end |
| 20 |
|
| 21 |
def flush_test(r, w) |
| 22 |
begin |
| 23 |
w.nonblock = true |
| 24 |
rescue Errno::EBADF |
| 25 |
return false |
| 26 |
end |
| 27 |
w.sync = false |
| 28 |
w << "b" |
| 29 |
w.flush |
| 30 |
w << "a" * 4096 |
| 31 |
result = "" |
| 32 |
Timeout.timeout(10) { |
| 33 |
t0 = Thread.new { |
| 34 |
Thread.pass |
| 35 |
w.close |
| 36 |
} |
| 37 |
t = Thread.new { |
| 38 |
while (Thread.pass; s = r.read(4096)) |
| 39 |
result << s |
| 40 |
end |
| 41 |
} |
| 42 |
begin |
| 43 |
w.flush # assert_raise(IOError, "[ruby-dev:24985]") {w.flush} |
| 44 |
rescue Errno::EBADF, IOError |
| 45 |
# ignore [ruby-dev:35638] |
| 46 |
end |
| 47 |
assert_nothing_raised {t.join} |
| 48 |
t0.join |
| 49 |
} |
| 50 |
assert_equal(4097, result.size) |
| 51 |
true |
| 52 |
end |
| 53 |
|
| 54 |
def test_nonblock |
| 55 |
IO.pipe {|r, w| |
| 56 |
w.nonblock = false |
| 57 |
assert_equal(false, w.nonblock?) |
| 58 |
w.nonblock do |
| 59 |
assert_equal(true, w.nonblock?) |
| 60 |
w.nonblock(false) do |
| 61 |
assert_equal(false, w.nonblock?) |
| 62 |
w.nonblock(false) do |
| 63 |
assert_equal(false, w.nonblock?) |
| 64 |
end |
| 65 |
assert_equal(false, w.nonblock?) |
| 66 |
end |
| 67 |
assert_equal(true, w.nonblock?) |
| 68 |
end |
| 69 |
assert_equal(false, w.nonblock?) |
| 70 |
} |
| 71 |
rescue NotImplementedError |
| 72 |
end |
| 73 |
end if IO.method_defined?(:nonblock) |