| 1 |
# frozen_string_literal: true |
| 2 |
require 'test/unit' |
| 3 |
|
| 4 |
# test uncommon device types to check portability problems |
| 5 |
# We may optimize IO#wait_*able for non-Linux kernels in the future |
| 6 |
class TestIOWaitUncommon < Test::Unit::TestCase |
| 7 |
def test_tty_wait |
| 8 |
check_dev('/dev/tty', mode: 'w+') do |tty| |
| 9 |
assert_include [ nil, tty ], tty.wait_readable(0) |
| 10 |
assert_equal tty, tty.wait_writable(1), 'portability test' |
| 11 |
end |
| 12 |
end |
| 13 |
|
| 14 |
def test_fifo_wait |
| 15 |
omit 'no mkfifo' unless File.respond_to?(:mkfifo) && IO.const_defined?(:NONBLOCK) |
| 16 |
require 'tmpdir' |
| 17 |
Dir.mktmpdir('rubytest-fifo') do |dir| |
| 18 |
fifo = "#{dir}/fifo" |
| 19 |
assert_equal 0, File.mkfifo(fifo) |
| 20 |
rd = Thread.new { File.open(fifo, IO::RDONLY|IO::NONBLOCK) } |
| 21 |
begin |
| 22 |
wr = File.open(fifo, IO::WRONLY|IO::NONBLOCK) |
| 23 |
rescue Errno::ENXIO |
| 24 |
Thread.pass |
| 25 |
end until wr |
| 26 |
assert_instance_of File, rd.value |
| 27 |
assert_instance_of File, wr |
| 28 |
rd = rd.value |
| 29 |
assert_nil rd.wait_readable(0) |
| 30 |
assert_same wr, wr.wait_writable(0) |
| 31 |
wr.syswrite 'hi' |
| 32 |
assert_same rd, rd.wait_readable(1) |
| 33 |
wr.close |
| 34 |
assert_equal 'hi', rd.gets |
| 35 |
rd.close |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
# used to find portability problems because some ppoll implementations |
| 40 |
# are incomplete and do not work for certain "file" types |
| 41 |
def check_dev(dev, m = :wait_readable, mode: m == :wait_readable ? 'r' : 'w', &block) |
| 42 |
begin |
| 43 |
fp = File.open(dev, mode) |
| 44 |
rescue Errno::ENOENT |
| 45 |
return # Ignore silently |
| 46 |
rescue SystemCallError => e |
| 47 |
omit "#{dev} could not be opened #{e.message} (#{e.class})" |
| 48 |
end |
| 49 |
if block |
| 50 |
yield fp |
| 51 |
else |
| 52 |
assert_same fp, fp.__send__(m) |
| 53 |
end |
| 54 |
ensure |
| 55 |
fp&.close |
| 56 |
end |
| 57 |
|
| 58 |
def test_wait_readable_urandom |
| 59 |
check_dev('/dev/urandom') |
| 60 |
end |
| 61 |
|
| 62 |
def test_wait_readable_random |
| 63 |
check_dev('/dev/random') do |fp| |
| 64 |
assert_nothing_raised do |
| 65 |
fp.wait_readable(0) |
| 66 |
end |
| 67 |
end |
| 68 |
end |
| 69 |
|
| 70 |
def test_wait_readable_zero |
| 71 |
check_dev('/dev/zero') |
| 72 |
end |
| 73 |
|
| 74 |
def test_wait_writable_null |
| 75 |
check_dev(IO::NULL, :wait_writable) |
| 76 |
end |
| 77 |
end |