1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/io-wait] Refined uncommon device type tests

https://github.com/ruby/io-wait/commit/0c73ebcf5d
This commit is contained in:
Nobuyoshi Nakada 2021-03-05 16:39:31 +09:00
parent 05d118feea
commit ef9bde6516
Notes: git 2021-03-07 09:54:59 +09:00

View file

@ -6,15 +6,10 @@ require 'io/wait'
# We may optimize IO#wait_*able for non-Linux kernels in the future # We may optimize IO#wait_*able for non-Linux kernels in the future
class TestIOWaitUncommon < Test::Unit::TestCase class TestIOWaitUncommon < Test::Unit::TestCase
def test_tty_wait def test_tty_wait
begin check_dev('/dev/tty', mode: 'w+') do |tty|
tty = File.open('/dev/tty', 'w+') assert_include [ nil, tty ], tty.wait_readable(0)
rescue Errno::ENOENT, Errno::ENXIO => e assert_equal tty, tty.wait_writable(1), 'portability test'
skip "/dev/tty: #{e.message} (#{e.class})"
end end
assert_include [ nil, tty ], tty.wait_readable(0)
assert_equal tty, tty.wait_writable(1), 'portability test'
ensure
tty&.close
end end
def test_fifo_wait def test_fifo_wait
@ -44,36 +39,40 @@ class TestIOWaitUncommon < Test::Unit::TestCase
# used to find portability problems because some ppoll implementations # used to find portability problems because some ppoll implementations
# are incomplete and do not work for certain "file" types # are incomplete and do not work for certain "file" types
def check_dev(dev, m = :wait_readable) def check_dev(dev, m = :wait_readable, mode: m == :wait_readable ? 'r' : 'w', &block)
begin begin
fp = File.open("/dev/#{dev}", m == :wait_readable ? 'r' : 'w') fp = File.open(dev, mode)
rescue Errno::ENOENT
return # Ignore silently
rescue SystemCallError => e rescue SystemCallError => e
skip "#{dev} could not be opened #{e.message} (#{e.class})" skip "#{dev} could not be opened #{e.message} (#{e.class})"
end end
assert_same fp, fp.__send__(m) if block
yield fp
else
assert_same fp, fp.__send__(m)
end
ensure ensure
fp&.close fp&.close
end end
def test_wait_readable_urandom def test_wait_readable_urandom
check_dev 'urandom' check_dev('/dev/urandom')
end end
def test_wait_readable_random def test_wait_readable_random
File.open('/dev/random') do |fp| check_dev('/dev/random') do |fp|
assert_nothing_raised do assert_nothing_raised do
fp.wait_readable(0) fp.wait_readable(0)
end end
end end
rescue SystemCallError => e
skip "/dev/random could not be opened #{e.message} (#{e.class})"
end end
def test_wait_readable_zero def test_wait_readable_zero
check_dev 'zero' check_dev('/dev/zero')
end end
def test_wait_writable_null def test_wait_writable_null
check_dev 'null', :wait_writable check_dev(IO::NULL, :wait_writable)
end end
end end