diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/test/io |
Fresh start
Diffstat (limited to 'jni/ruby/test/io')
-rw-r--r-- | jni/ruby/test/io/console/test_io_console.rb | 332 | ||||
-rw-r--r-- | jni/ruby/test/io/nonblock/test_flush.rb | 52 | ||||
-rw-r--r-- | jni/ruby/test/io/wait/test_io_wait.rb | 112 |
3 files changed, 496 insertions, 0 deletions
diff --git a/jni/ruby/test/io/console/test_io_console.rb b/jni/ruby/test/io/console/test_io_console.rb new file mode 100644 index 0000000..3481a2b --- /dev/null +++ b/jni/ruby/test/io/console/test_io_console.rb @@ -0,0 +1,332 @@ +begin + require 'io/console' + require 'test/unit' + require 'pty' +rescue LoadError +end + +class TestIO_Console < Test::Unit::TestCase + Bug6116 = '[ruby-dev:45309]' + + def test_raw + helper {|m, s| + s.print "abc\n" + assert_equal("abc\r\n", m.gets) + assert_send([s, :echo?]) + s.raw { + assert_not_send([s, :echo?], Bug6116) + s.print "def\n" + assert_equal("def\n", m.gets) + } + assert_send([s, :echo?]) + s.print "ghi\n" + assert_equal("ghi\r\n", m.gets) + } + end + + def test_raw_minchar + helper {|m, s| + len = 0 + assert_equal([nil, 0], [s.getch(min: 0), len]) + main = Thread.current + go = false + th = Thread.start { + len += 1 + m.print("a") + m.flush + sleep 0.01 until go and main.stop? + len += 10 + m.print("1234567890") + m.flush + } + begin + assert_equal(["a", 1], [s.getch(min: 1), len]) + go = true + assert_equal(["1", 11], [s.getch, len]) + ensure + th.join + end + } + end + + def test_raw_timeout + helper {|m, s| + len = 0 + assert_equal([nil, 0], [s.getch(min: 0, time: 0.1), len]) + main = Thread.current + th = Thread.start { + sleep 0.01 until main.stop? + len += 2 + m.print("ab") + } + begin + assert_equal(["a", 2], [s.getch(min: 1, time: 1), len]) + assert_equal(["b", 2], [s.getch(time: 1), len]) + ensure + th.join + end + } + end + + def test_cooked + helper {|m, s| + assert_send([s, :echo?]) + s.raw { + s.print "abc\n" + assert_equal("abc\n", m.gets) + assert_not_send([s, :echo?], Bug6116) + s.cooked { + assert_send([s, :echo?]) + s.print "def\n" + assert_equal("def\r\n", m.gets) + } + assert_not_send([s, :echo?], Bug6116) + } + assert_send([s, :echo?]) + s.print "ghi\n" + assert_equal("ghi\r\n", m.gets) + } + end + + def test_echo + helper {|m, s| + assert_send([s, :echo?]) + m.print "a" + assert_equal("a", m.readpartial(10)) + } + end + + def test_noecho + helper {|m, s| + s.noecho { + assert_not_send([s, :echo?]) + m.print "a" + sleep 0.1 + } + m.print "b" + assert_equal("b", m.readpartial(10)) + } + end + + def test_noecho2 + helper {|m, s| + assert_send([s, :echo?]) + m.print "a\n" + sleep 0.1 + s.print "b\n" + sleep 0.1 + assert_equal("a\r\nb\r\n", m.readpartial(10)) + assert_equal("a\n", s.readpartial(10)) + s.noecho { + assert_not_send([s, :echo?]) + m.print "a\n" + s.print "b\n" + assert_equal("b\r\n", m.readpartial(10)) + assert_equal("a\n", s.readpartial(10)) + } + assert_send([s, :echo?]) + m.print "a\n" + sleep 0.1 + s.print "b\n" + sleep 0.1 + assert_equal("a\r\nb\r\n", m.readpartial(10)) + assert_equal("a\n", s.readpartial(10)) + } + end + + def test_setecho + helper {|m, s| + assert_send([s, :echo?]) + s.echo = false + m.print "a" + sleep 0.1 + s.echo = true + m.print "b" + assert_equal("b", m.readpartial(10)) + } + end + + def test_setecho2 + helper {|m, s| + assert_send([s, :echo?]) + m.print "a\n" + sleep 0.1 + s.print "b\n" + sleep 0.1 + assert_equal("a\r\nb\r\n", m.readpartial(10)) + assert_equal("a\n", s.readpartial(10)) + s.echo = false + assert_not_send([s, :echo?]) + m.print "a\n" + s.print "b\n" + assert_equal("b\r\n", m.readpartial(10)) + assert_equal("a\n", s.readpartial(10)) + s.echo = true + assert_send([s, :echo?]) + m.print "a\n" + sleep 0.1 + s.print "b\n" + sleep 0.1 + assert_equal("a\r\nb\r\n", m.readpartial(10)) + assert_equal("a\n", s.readpartial(10)) + } + end + + def test_iflush + helper {|m, s| + m.print "a" + s.iflush + m.print "b\n" + assert_equal("b\n", s.readpartial(10)) + } + end + + def test_oflush + helper {|m, s| + s.print "a" + s.oflush # oflush may be issued after "a" is already sent. + s.print "b" + assert_include(["b", "ab"], m.readpartial(10)) + } + end + + def test_ioflush + helper {|m, s| + m.print "a" + s.ioflush + m.print "b\n" + assert_equal("b\n", s.readpartial(10)) + } + end + + def test_ioflush2 + helper {|m, s| + s.print "a" + s.ioflush # ioflush may be issued after "a" is already sent. + s.print "b" + assert_include(["b", "ab"], m.readpartial(10)) + } + end + + def test_winsize + helper {|m, s| + begin + assert_equal([0, 0], s.winsize) + rescue Errno::EINVAL # OpenSolaris 2009.06 TIOCGWINSZ causes Errno::EINVAL before TIOCSWINSZ. + end + } + end + + if IO.console + def test_close + IO.console.close + assert_kind_of(IO, IO.console) + assert_nothing_raised(IOError) {IO.console.fileno} + + IO.console(:close) + assert(IO.console(:tty?)) + ensure + IO.console(:close) + end + + def test_sync + assert(IO.console.sync, "console should be unbuffered") + ensure + IO.console(:close) + end + else + def test_close + assert_equal(["true"], run_pty("IO.console.close; p IO.console.fileno >= 0")) + assert_equal(["true"], run_pty("IO.console(:close); p IO.console(:tty?)")) + end + + def test_sync + assert_equal(["true"], run_pty("p IO.console.sync")) + end + end + + private + def helper + m, s = PTY.open + rescue RuntimeError + skip $! + else + yield m, s + ensure + m.close if m + s.close if s + end + + def run_pty(src, n = 1) + r, w, pid = PTY.spawn(EnvUtil.rubybin, "-rio/console", "-e", src) + rescue RuntimeError + skip $! + else + result = [] + n.times {result << r.gets.chomp} + Process.wait(pid) + if block_given? + yield result + else + result + end + ensure + r.close if r + w.close if w + end +end if defined?(PTY) and defined?(IO::console) + +class TestIO_Console < Test::Unit::TestCase + case + when Process.respond_to?(:daemon) + noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"] + when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty? + dir, base = File.split(EnvUtil.rubybin) + noctty = [File.join(dir, base.sub(/ruby/, rubyw))] + end + + if noctty + require 'tempfile' + NOCTTY = noctty + def test_noctty + t = Tempfile.new("console") + t.close + t2 = Tempfile.new("console") + t2.close + cmd = [*NOCTTY[1..-1], + '-e', 'open(ARGV[0], "w") {|f|', + '-e', 'STDOUT.reopen(f)', + '-e', 'STDERR.reopen(f)', + '-e', 'require "io/console"', + '-e', 'f.puts IO.console.inspect', + '-e', 'f.flush', + '-e', 'File.unlink(ARGV[1])', + '-e', '}', + '--', t.path, t2.path] + assert_ruby_status(cmd, rubybin: NOCTTY[0]) + 30.times do + break unless File.exist?(t2.path) + sleep 0.1 + end + t.open + assert_equal("nil", t.gets(nil).chomp) + ensure + t.close! if t and !t.closed? + t2.close! + end + end +end if defined?(IO.console) + +class TestIO_Console < Test::Unit::TestCase + def test_stringio_getch + assert_separately %w"--disable=gems -rstringio -rio/console", %q{ + assert_operator(StringIO, :method_defined?, :getch) + } + assert_separately %w"--disable=gems -rio/console -rstringio", %q{ + assert_operator(StringIO, :method_defined?, :getch) + } + assert_separately %w"--disable=gems -rstringio", %q{ + assert_not_operator(StringIO, :method_defined?, :getch) + } + end +end diff --git a/jni/ruby/test/io/nonblock/test_flush.rb b/jni/ruby/test/io/nonblock/test_flush.rb new file mode 100644 index 0000000..ab54205 --- /dev/null +++ b/jni/ruby/test/io/nonblock/test_flush.rb @@ -0,0 +1,52 @@ +require 'test/unit' +require 'timeout' +begin + require 'io/nonblock' +rescue LoadError +end + +class TestIONonblock < Test::Unit::TestCase + def test_flush + IO.pipe {|r, w| + return if flush_test(r, w) + } + require 'socket'; + Socket.pair(:INET, :STREAM) {|s1, s2| + return if flush_test(s1, s2) + } + skip "nonblocking IO did not work" + end + + def flush_test(r, w) + begin + w.nonblock = true + rescue Errno::EBADF + return false + end + w.sync = false + w << "b" + w.flush + w << "a" * 4096 + result = "" + timeout(10) { + t0 = Thread.new { + Thread.pass + w.close + } + t = Thread.new { + while (Thread.pass; s = r.read(4096)) + result << s + end + } + begin + w.flush # assert_raise(IOError, "[ruby-dev:24985]") {w.flush} + rescue Errno::EBADF, IOError + # ignore [ruby-dev:35638] + end + assert_nothing_raised {t.join} + t0.join + } + assert_equal(4097, result.size) + true + end +end if IO.method_defined?(:nonblock) diff --git a/jni/ruby/test/io/wait/test_io_wait.rb b/jni/ruby/test/io/wait/test_io_wait.rb new file mode 100644 index 0000000..7729d45 --- /dev/null +++ b/jni/ruby/test/io/wait/test_io_wait.rb @@ -0,0 +1,112 @@ +# -*- coding: us-ascii -*- +require 'test/unit' +require 'timeout' +require 'socket' +begin + require 'io/wait' +rescue LoadError +end + +class TestIOWait < Test::Unit::TestCase + + def setup + if /mswin|mingw/ =~ RUBY_PLATFORM + @r, @w = Socket.pair(Socket::AF_INET, Socket::SOCK_STREAM, 0) + else + @r, @w = IO.pipe + end + end + + def teardown + @r.close unless @r.closed? + @w.close unless @w.closed? + end + + def test_nread + assert_equal 0, @r.nread + @w.syswrite "." + sleep 0.1 + assert_equal 1, @r.nread + end + + def test_nread_buffered + @w.syswrite ".\n!" + assert_equal ".\n", @r.gets + assert_equal 1, @r.nread + end + + def test_ready? + refute @r.ready?, "shouldn't ready, but ready" + @w.syswrite "." + sleep 0.1 + assert @r.ready?, "should ready, but not" + 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 "." + sleep 0.1 + 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 + th = Thread.new { sleep 0.01; @w.syswrite "." } + assert_equal @r, @r.wait + ensure + th.join + end + + def test_wait_eof + th = Thread.new { sleep 0.01; @w.close } + assert_nil @r.wait + ensure + th.join + end + + def test_wait_writable + assert_equal @w, @w.wait_writable + end + + def test_wait_writable_timeout + assert_equal @w, @w.wait_writable(0.01) + written = fill_pipe + assert_nil @w.wait_writable(0.01) + @r.read(written) + assert_equal @w, @w.wait_writable(0.01) + 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) + rescue Errno::EAGAIN, Errno::EWOULDBLOCK + return written + end while true + end +end if IO.method_defined?(:wait) |