2012-11-21 17:40:00 +04:00
|
|
|
# -*- coding: us-ascii -*-
|
2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2011-04-30 20:02:11 +04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'timeout'
|
2011-05-07 19:17:22 +04:00
|
|
|
require 'socket'
|
2011-04-30 20:02:11 +04:00
|
|
|
begin
|
|
|
|
require 'io/wait'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestIOWait < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def setup
|
2011-09-21 12:02:03 +04:00
|
|
|
if /mswin|mingw/ =~ RUBY_PLATFORM
|
2011-05-08 00:21:38 +04:00
|
|
|
@r, @w = Socket.pair(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
|
|
|
else
|
|
|
|
@r, @w = IO.pipe
|
|
|
|
end
|
2011-04-30 20: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-31 07:54:58 +04:00
|
|
|
sleep 0.1
|
2011-04-30 20:02:11 +04:00
|
|
|
assert_equal 1, @r.nread
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nread_buffered
|
|
|
|
@w.syswrite ".\n!"
|
2015-03-02 00:03:22 +03:00
|
|
|
assert_equal ".\n", @r.gets
|
2011-04-30 20:02:11 +04:00
|
|
|
assert_equal 1, @r.nread
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ready?
|
2016-02-19 10:45:58 +03:00
|
|
|
assert_not_predicate @r, :ready?, "shouldn't ready, but ready"
|
2011-04-30 20:02:11 +04:00
|
|
|
@w.syswrite "."
|
2011-05-31 07:54:58 +04:00
|
|
|
sleep 0.1
|
2016-02-19 10:45:58 +03:00
|
|
|
assert_predicate @r, :ready?, "should ready, but not"
|
2011-04-30 20:02:11 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_buffered_ready?
|
|
|
|
@w.syswrite ".\n!"
|
|
|
|
assert_equal ".\n", @r.gets
|
2016-02-19 10:45:58 +03:00
|
|
|
assert_predicate @r, :ready?
|
2011-04-30 20:02:11 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait
|
|
|
|
assert_nil @r.wait(0)
|
|
|
|
@w.syswrite "."
|
2011-05-31 07:54:58 +04:00
|
|
|
sleep 0.1
|
2011-04-30 20: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
|
2019-12-24 09:47:59 +03:00
|
|
|
q = Thread::Queue.new
|
|
|
|
th = Thread.new { q.pop; @w.syswrite "." }
|
|
|
|
q.push(true)
|
2011-04-30 20:02:11 +04:00
|
|
|
assert_equal @r, @r.wait
|
2014-05-30 17:48:06 +04:00
|
|
|
ensure
|
|
|
|
th.join
|
2011-04-30 20:02:11 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_eof
|
2019-12-24 09:47:59 +03:00
|
|
|
q = Thread::Queue.new
|
|
|
|
th = Thread.new { q.pop; @w.close }
|
2015-10-23 01:25:19 +03:00
|
|
|
ret = nil
|
2015-04-12 09:46:28 +03:00
|
|
|
assert_nothing_raised(Timeout::Error) do
|
2019-12-24 09:47:59 +03:00
|
|
|
q.push(true)
|
2015-10-23 01:25:19 +03:00
|
|
|
Timeout.timeout(0.1) { ret = @r.wait }
|
2015-04-12 09:46:28 +03:00
|
|
|
end
|
2015-10-23 01:25:19 +03:00
|
|
|
assert_equal @r, ret
|
2014-05-30 17:48:06 +04:00
|
|
|
ensure
|
|
|
|
th.join
|
2011-04-30 20:02:11 +04:00
|
|
|
end
|
2012-11-21 17:40:00 +04:00
|
|
|
|
2016-01-24 08:30:54 +03:00
|
|
|
def test_wait_readable
|
|
|
|
assert_nil @r.wait_readable(0)
|
|
|
|
@w.syswrite "."
|
2018-12-31 15:54:40 +03:00
|
|
|
IO.select([@r])
|
2016-01-24 08:30:54 +03:00
|
|
|
assert_equal @r, @r.wait_readable(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_readable_buffered
|
|
|
|
@w.syswrite ".\n!"
|
|
|
|
assert_equal ".\n", @r.gets
|
|
|
|
assert_equal true, @r.wait_readable(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_readable_forever
|
2019-12-24 09:47:59 +03:00
|
|
|
q = Thread::Queue.new
|
|
|
|
th = Thread.new { q.pop; @w.syswrite "." }
|
|
|
|
q.push(true)
|
2016-01-24 08:30:54 +03:00
|
|
|
assert_equal @r, @r.wait_readable
|
|
|
|
ensure
|
|
|
|
th.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_readable_eof
|
2019-12-24 09:47:59 +03:00
|
|
|
q = Thread::Queue.new
|
|
|
|
th = Thread.new { q.pop; @w.close }
|
2016-01-24 08:30:54 +03:00
|
|
|
ret = nil
|
|
|
|
assert_nothing_raised(Timeout::Error) do
|
2019-12-24 09:47:59 +03:00
|
|
|
q.push(true)
|
2016-01-24 08:30:54 +03:00
|
|
|
Timeout.timeout(0.1) { ret = @r.wait_readable }
|
|
|
|
end
|
|
|
|
assert_equal @r, ret
|
|
|
|
ensure
|
|
|
|
th.join
|
|
|
|
end
|
|
|
|
|
2012-11-21 17:40:00 +04:00
|
|
|
def test_wait_writable
|
|
|
|
assert_equal @w, @w.wait_writable
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_writable_timeout
|
2013-01-11 11:11:54 +04:00
|
|
|
assert_equal @w, @w.wait_writable(0.01)
|
2012-11-21 17:40:00 +04:00
|
|
|
written = fill_pipe
|
2013-01-11 11:11:54 +04:00
|
|
|
assert_nil @w.wait_writable(0.01)
|
2012-11-21 17:40:00 +04:00
|
|
|
@r.read(written)
|
2013-01-11 11:11:54 +04:00
|
|
|
assert_equal @w, @w.wait_writable(0.01)
|
2012-11-21 17:40:00 +04: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
|
2015-06-02 05:18:44 +03:00
|
|
|
assert_raise(IOError) { @w.wait_writable }
|
2012-11-21 17:40:00 +04:00
|
|
|
end
|
|
|
|
|
2016-01-24 10:55:05 +03:00
|
|
|
def test_wait_readwrite
|
|
|
|
assert_equal @r.wait(0, :write), @r.wait(0, :read_write)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_readwrite_timeout
|
|
|
|
assert_equal @w, @w.wait(0.01, :read_write)
|
|
|
|
written = fill_pipe
|
2016-03-09 03:28:41 +03:00
|
|
|
if /aix/ =~ RUBY_PLATFORM
|
|
|
|
# IO#wait internally uses select(2) on AIX.
|
|
|
|
# AIX's select(2) returns "readable" for the write-side fd
|
|
|
|
# of a pipe, so @w.wait(0.01, :read_write) does not return nil.
|
|
|
|
assert_equal @w, @w.wait(0.01, :read_write)
|
|
|
|
else
|
|
|
|
assert_nil @w.wait(0.01, :read_write)
|
|
|
|
end
|
2016-01-24 10:55:05 +03:00
|
|
|
@r.read(written)
|
|
|
|
assert_equal @w, @w.wait(0.01, :read_write)
|
|
|
|
end
|
|
|
|
|
2022-06-25 10:21:37 +03:00
|
|
|
def test_wait_mask_writable
|
|
|
|
omit("Missing IO::WRITABLE!") unless IO.const_defined?(:WRITABLE)
|
|
|
|
assert_equal IO::WRITABLE, @w.wait(IO::WRITABLE, 0)
|
|
|
|
end
|
2022-06-25 10:22:07 +03:00
|
|
|
|
2022-06-25 10:21:37 +03:00
|
|
|
def test_wait_mask_readable
|
|
|
|
omit("Missing IO::READABLE!") unless IO.const_defined?(:READABLE)
|
|
|
|
@w.write("Hello World\n" * 3)
|
|
|
|
assert_equal IO::READABLE, @r.wait(IO::READABLE, 0)
|
2022-06-25 10:22:07 +03:00
|
|
|
|
2022-06-25 10:21:37 +03:00
|
|
|
@r.gets
|
|
|
|
assert_equal IO::READABLE, @r.wait(IO::READABLE, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_mask_zero
|
|
|
|
omit("Missing IO::WRITABLE!") unless IO.const_defined?(:WRITABLE)
|
2022-06-25 11:12:21 +03:00
|
|
|
assert_raise(ArgumentError) do
|
2022-06-25 10:21:37 +03:00
|
|
|
@w.wait(0, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wait_mask_negative
|
|
|
|
omit("Missing IO::WRITABLE!") unless IO.const_defined?(:WRITABLE)
|
2022-06-25 11:12:21 +03:00
|
|
|
assert_raise(ArgumentError) do
|
2022-06-25 10:21:37 +03:00
|
|
|
@w.wait(-6, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-21 17:40:00 +04:00
|
|
|
private
|
|
|
|
|
|
|
|
def fill_pipe
|
|
|
|
written = 0
|
|
|
|
buf = " " * 4096
|
|
|
|
begin
|
|
|
|
written += @w.write_nonblock(buf)
|
2012-11-29 12:11:10 +04:00
|
|
|
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
|
2012-11-21 17:40:00 +04:00
|
|
|
return written
|
|
|
|
end while true
|
|
|
|
end
|
2019-12-24 08:56:17 +03:00
|
|
|
|
|
|
|
def sleep(time)
|
|
|
|
super EnvUtil.apply_timeout_scale(time)
|
|
|
|
end
|
2011-04-30 20:02:11 +04:00
|
|
|
end if IO.method_defined?(:wait)
|