%w(lib ../lib . ..).each{|d| $:.unshift d} require 'session' STDOUT.sync = true STDERR.sync = true # a timeout method which does not stop all threads! class TimeoutError < StandardError; end def timeout n #{{{ ret = nil cid = fork unless cid trap('SIGQUIT'){ exit! } sleep n begin; Process.kill 'SIGUSR1', Process.ppid; rescue Errno::Exception; end exit! else begin handler = trap('SIGUSR1'){throw :timeout, TimeoutError.new} thrown = catch(:timeout){ ret = yield } if TimeoutError === thrown display STDIN.gets raise thrown end ensure begin; Process.kill 'SIGQUIT', cid; rescue Exception; end begin; Process.wait; rescue Exception => e; end trap 'SIGUSR1', handler if defined? handler end end ret #}}} end def display #{{{ puts "$command < #{ $command.inspect }> " puts "$iodat < #{ $iodat.inspect }> " puts "$selecting < #{ $selecting.inspect }> " # puts "$buffer < #{ $buffer.inspect }> " # puts "$err < #{ $err.inspect }> " # puts "$out < #{ $out.inspect }> " # puts "$iodat_name < #{ $iodat_name.inspect }> " # puts "$reading < #{ $reading.inspect }> " # puts "$buf < #{ $buf.inspect }> " # puts "$lines < #{ $lines.inspect }> " # puts "$line < #{ $line.inspect }> " # puts "$getting_status < #{ $getting_status.inspect }> " self #}}} end require "test/unit" module Session class Test < Test::Unit::TestCase def test_0 #{{{ sh = nil assert_nothing_raised { sh = Shell.new } #}}} end def test_1 #{{{ assert_nothing_raised { timeout(16) { sh = nil assert_nothing_raised { sh = Shell.new } sh.execute 'ls' } } #}}} end def test_3 #{{{ assert_nothing_raised { timeout(64) { sh = nil assert_nothing_raised { sh = Shell.new } 128.times { sh.execute 'echo 42' } } } #}}} end def test_4 #{{{ cmds = ['ls', 'echo 42', 'printf "foobar"', 'printf "foobar\n"'] assert_nothing_raised { timeout(64) { sh = nil assert_nothing_raised { sh = Shell.new } 128.times { cmds.each{|cmd| sh.execute cmd;sh.execute "#{cmd} 1>&2"} } } } #}}} end def test_5 #{{{ assert_nothing_raised { timeout(16) { sh = nil assert_nothing_raised { sh = Shell.new } out, err = sh.execute 'echo 42' assert_equal '42', out.strip out, err = sh.execute 'echo "forty-two" 1>&2' assert_equal 'forty-two', err.strip } } #}}} end def test_6 #{{{ out = '' err = '' assert_nothing_raised { timeout(16) { sh = nil assert_nothing_raised { sh = Shell.new } sh.execute 'echo 42', :stdout => out, :stderr => err assert_equal '42', out.strip sh.execute 'echo "forty-two" 1>&2', :stdout => out, :stderr => err assert_equal 'forty-two', err.strip } } #}}} end def test_7 #{{{ #$DEBUG = true assert_nothing_raised { timeout(16) { sh = nil assert_nothing_raised { sh = Shell.new } sh.execute('echo 42') do |out, err| if out assert_equal '42', out.strip end end sh.execute('echo "forty-two" 1>&2') do |out, err| if err assert_equal 'forty-two', err.strip end end } } #ensure #$DEBUG = true #}}} end def test_8 #{{{ assert_nothing_raised { timeout(16) { idl = nil assert_nothing_raised { idl = IDL.new } out = ''; err = '' idl.execute 'printf, -1, 42', :stdout => out, :stderr => err assert_equal '42', out.strip out = ''; err = '' idl.execute 'printf, -2, \'forty-two\'', :stdout => out, :stderr => err assert_equal 'forty-two', err.strip out = ''; err = '' idl.execute 'foo', :stdout => out, :stderr => err assert_match %r/undefined procedure/io, err } } #}}} end end end