2012-11-21 08:40:00 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2011-04-30 12:02:11 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'timeout'
|
2011-05-07 11:17:22 -04:00
|
|
|
require 'socket'
|
2011-04-30 12:02:11 -04:00
|
|
|
begin
|
|
|
|
require 'io/wait'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestIOWait < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def setup
|
2011-09-21 04:02:03 -04:00
|
|
|
if /mswin|mingw/ =~ RUBY_PLATFORM
|
2011-05-07 16:21:38 -04:00
|
|
|
@r, @w = Socket.pair(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
|
|
else
|
|
|
|
@r, @w = IO.pipe
|
|
|
|
end
|
2011-04-30 12:02:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
@r.close unless @r.closed?
|
|
|
|
@w.close unless @w.closed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nread
|
|
|
|
assert_equal 0, @r.nread
|
|
|
|
@w.syswrite "."
|
2011-05-30 23:54:58 -04:00
|
|
|
sleep 0.1
|
2011-04-30 12:02:11 -04:00
|
|
|
assert_equal 1, @r.nread
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nread_buffered
|
|
|
|
@w.syswrite ".\n!"
|
|
|
|
assert_equal ".\n", @r.read(2)
|
|
|
|
assert_equal 1, @r.nread
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ready?
|
2011-05-30 23:54:58 -04:00
|
|
|
refute @r.ready?, "shouldn't ready, but ready"
|
2011-04-30 12:02:11 -04:00
|
|
|
@w.syswrite "."
|
2011-05-30 23:54:58 -04:00
|
|
|
sleep 0.1
|
|
|
|
assert @r.ready?, "should ready, but not"
|
2011-04-30 12:02:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_buffered_ready?
|
|
|
|
@w.syswrite ".\n!"
|
|
|
|
assert_equal ".\n", @r.gets
|
|
|
|
assert @r.ready?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait
|
|
|
|
assert_nil @r.wait(0)
|
|
|
|
@w.syswrite "."
|
2011-05-30 23:54:58 -04:00
|
|
|
sleep 0.1
|
2011-04-30 12:02:11 -04:00
|
|
|
assert_equal @r, @r.wait(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_buffered
|
|
|
|
@w.syswrite ".\n!"
|
|
|
|
assert_equal ".\n", @r.gets
|
|
|
|
assert_equal true, @r.wait(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_forever
|
|
|
|
Thread.new { sleep 0.01; @w.syswrite "." }
|
|
|
|
assert_equal @r, @r.wait
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_eof
|
|
|
|
Thread.new { sleep 0.01; @w.close }
|
|
|
|
assert_nil @r.wait
|
|
|
|
end
|
2012-11-21 08:40:00 -05:00
|
|
|
|
|
|
|
def test_wait_writable
|
|
|
|
assert_equal @w, @w.wait_writable
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_writable_timeout
|
2013-01-11 02:11:54 -05:00
|
|
|
assert_equal @w, @w.wait_writable(0.01)
|
2012-11-21 08:40:00 -05:00
|
|
|
written = fill_pipe
|
2013-01-11 02:11:54 -05:00
|
|
|
assert_nil @w.wait_writable(0.01)
|
2012-11-21 08:40:00 -05:00
|
|
|
@r.read(written)
|
2013-01-11 02:11:54 -05:00
|
|
|
assert_equal @w, @w.wait_writable(0.01)
|
2012-11-21 08:40:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_writable_EPIPE
|
|
|
|
fill_pipe
|
|
|
|
@r.close
|
|
|
|
assert_equal @w, @w.wait_writable
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_writable_closed
|
|
|
|
@w.close
|
|
|
|
assert_raises(IOError) { @w.wait_writable }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fill_pipe
|
|
|
|
written = 0
|
|
|
|
buf = " " * 4096
|
|
|
|
begin
|
|
|
|
written += @w.write_nonblock(buf)
|
2012-11-29 03:11:10 -05:00
|
|
|
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
|
2012-11-21 08:40:00 -05:00
|
|
|
return written
|
|
|
|
end while true
|
|
|
|
end
|
2011-04-30 12:02:11 -04:00
|
|
|
end if IO.method_defined?(:wait)
|