Skip to content

Commit

Permalink
Refined uncommon device type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Mar 5, 2021
1 parent 12ae3df commit 0c73ebc
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions test/io/wait/test_io_wait_uncommon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
# We may optimize IO#wait_*able for non-Linux kernels in the future
class TestIOWaitUncommon < Test::Unit::TestCase
def test_tty_wait
begin
tty = File.open('/dev/tty', 'w+')
rescue Errno::ENOENT, Errno::ENXIO => e
skip "/dev/tty: #{e.message} (#{e.class})"
check_dev('/dev/tty', mode: 'w+') do |tty|
assert_include [ nil, tty ], tty.wait_readable(0)
assert_equal tty, tty.wait_writable(1), 'portability test'
end
assert_include [ nil, tty ], tty.wait_readable(0)
assert_equal tty, tty.wait_writable(1), 'portability test'
ensure
tty&.close
end

def test_fifo_wait
Expand Down Expand Up @@ -44,36 +39,40 @@ def test_fifo_wait

# used to find portability problems because some ppoll implementations
# 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
fp = File.open("/dev/#{dev}", m == :wait_readable ? 'r' : 'w')
fp = File.open(dev, mode)
rescue Errno::ENOENT
return # Ignore silently
rescue SystemCallError => e
skip "#{dev} could not be opened #{e.message} (#{e.class})"
end
assert_same fp, fp.__send__(m)
if block
yield fp
else
assert_same fp, fp.__send__(m)
end
ensure
fp&.close
end

def test_wait_readable_urandom
check_dev 'urandom'
check_dev('/dev/urandom')
end

def test_wait_readable_random
File.open('/dev/random') do |fp|
check_dev('/dev/random') do |fp|
assert_nothing_raised do
fp.wait_readable(0)
end
end
rescue SystemCallError => e
skip "/dev/random could not be opened #{e.message} (#{e.class})"
end

def test_wait_readable_zero
check_dev 'zero'
check_dev('/dev/zero')
end

def test_wait_writable_null
check_dev 'null', :wait_writable
check_dev(IO::NULL, :wait_writable)
end
end

0 comments on commit 0c73ebc

Please sign in to comment.