################################################################## # tc_wait3.rb # # Test suite for the Ruby proc-wait3 package. ################################################################## base = File.basename(Dir.pwd) if base == "test" || base =~ /proc-wait3/ require "ftools" ext = ".so" ext = ".sl" if RUBY_PLATFORM =~ /hpux/i ext = ".bundle" if RUBY_PLATFORM =~ /darwin|powerpc/i file = "wait3" + ext Dir.chdir("..") if base == "test" Dir.mkdir("proc") rescue nil File.copy(file,"proc") $LOAD_PATH.unshift(Dir.pwd) Dir.chdir("test") rescue nil end require "proc/wait3" require "test/unit" class TC_Wait3 < Test::Unit::TestCase def setup @proc_stat = nil @proc_stat_members = ["pid", "status", "utime", "stime", "maxrss", "ixrss", "idrss", "isrss", "minflt", "majflt", "nswap", "inblock", "oublock", "msgsnd", "msgrcv", "nsignals", "nvcsw", "nivcsw", "stopped", "signaled","exited","success","coredump","exitstatus", "termsig", "stopsig"] end def test_wait3_version assert_equal('1.5.3', Process::WAIT3_VERSION) end def test_wait3_basic assert_respond_to(Process, :wait3) end def test_wait3_no_args pid = fork{ sleep 1 } assert_nothing_raised{ Process.wait3 } end def test_wait3_procstat_members pid = fork{ sleep 1 } assert_nothing_raised{ @proc_stat = Process.wait3 } assert_equal(@proc_stat_members, @proc_stat.members) end def test_wait3_nohang pid = fork{ sleep 1 } assert_nothing_raised{ Process.wait3(Process::WNOHANG) } end if RUBY_PLATFORM =~ /solaris|sunos/i def test_getdtablesize assert_respond_to(Process, :getdtablesize) assert_kind_of(Fixnum, Process.getdtablesize) assert(Process.getdtablesize > 0) end end unless RUBY_PLATFORM =~ /hpux/i def test_wait4_basic assert_respond_to(Process,:wait4) assert_raises(ArgumentError){ Process.wait4 } # Must have at least 1 arg end def test_wait4_in_action pid = fork{ sleep 1 } assert_nothing_raised{ @proc_stat = Process.wait4(pid) } assert_kind_of(Struct::ProcStat, @proc_stat) end def test_waitid_basic assert_respond_to(Process, :waitid) end def test_waitid_in_action pid = fork{ sleep 1 } assert_nothing_raised{ Process.waitid(Process::P_PID, pid, Process::WEXITED) } assert_raises(TypeError){ Process.waitid("foo", pid, Process::WEXITED) } assert_raises(TypeError){ Process.waitid(Process::P_PID, pid, "foo") } assert_raises(TypeError){ Process.waitid(Process::P_PID, "foo", Process::WEXITED) } if RUBY_PLATFORM.match("linux") assert_raises(Errno::ECHILD){ Process.waitid(Process::P_PID, 99999999, Process::WEXITED) } else assert_raises(Errno::EINVAL){ Process.waitid(Process::P_PID, 99999999, Process::WEXITED) } end end def test_sigsend_basic if RUBY_PLATFORM.match("linux") puts "test_sigsend_basic skipped on this platform" else assert_respond_to(Process,:send) end end def test_sigsend_in_action if RUBY_PLATFORM.match("linux") puts "test_sigsend_in_action skipped on this platform" else pid = fork{ sleep 1 } assert_nothing_raised{ Process.sigsend(Process::P_PID,pid,0) } end end end def test_getrusage_basic assert_respond_to(Process,:getrusage) end def test_getrusage_in_action pid = fork{ sleep 1 } assert_nothing_raised{ Process.getrusage } assert_nothing_raised{ Process.getrusage(true) } assert_kind_of(Struct::RUsage, Process.getrusage) assert_kind_of(Float, Process.getrusage.stime) assert_kind_of(Float, Process.getrusage.utime) end def test_pause assert_respond_to(Process, :pause) end def test_wait_constants msg = "Don't panic. It simply appears that this constant is not defined" msg += "on your particular platform. Ignore" assert_not_nil(Process::WCONTINUED, msg) assert_not_nil(Process::WEXITED, msg) assert_not_nil(Process::WNOWAIT, msg) assert_not_nil(Process::WSTOPPED, msg) assert_not_nil(Process::WTRAPPED, msg) unless RUBY_PLATFORM.match("linux") end # Skip these tests on Ruby 1.8.5 or later # if RUBY_VERSION.split('.').last.to_i <= 4 def test_getrlimit assert_respond_to(Process, :getrlimit) assert_nothing_raised{ Process.getrlimit(Process::RLIMIT_CPU) } assert_kind_of(Array, Process.getrlimit(Process::RLIMIT_CPU)) assert_equal(2, Process.getrlimit(Process::RLIMIT_CPU).length) end def test_setrlimit assert_respond_to(Process, :setrlimit) assert_nothing_raised{ Process.setrlimit( Process::RLIMIT_CPU, Process::RLIM_SAVED_CUR, Process::RLIM_SAVED_MAX ) } assert_nothing_raised{ Process.setrlimit(Process::RLIMIT_CPU, Process::RLIM_SAVED_CUR) } end else STDOUT.puts('Process.getrlimit test skipped for Ruby 1.8.5 or later') STDOUT.puts('Process.setrlimit test skipped for Ruby 1.8.5 or later') end # Test to ensure that the various rlimit constants are defined. Note that # as of Ruby 1.8.5 these are defined by Ruby itself, except for the # RLIMIT_VMEM constant, which is platform dependent. # def test_rlimit_constants assert_not_nil(Process::RLIMIT_AS) assert_not_nil(Process::RLIMIT_CORE) assert_not_nil(Process::RLIMIT_CPU) assert_not_nil(Process::RLIMIT_DATA) assert_not_nil(Process::RLIMIT_FSIZE) assert_not_nil(Process::RLIMIT_NOFILE) assert_not_nil(Process::RLIMIT_STACK) assert_not_nil(Process::RLIM_INFINITY) assert_not_nil(Process::RLIM_SAVED_MAX) assert_not_nil(Process::RLIM_SAVED_CUR) end # This test was added to ensure that these three constants are being # defined properly after an issue appeared on Linux with regards to # the value accidentally being assigned a negative value. # def test_rlimit_constants_valid assert(Process::RLIM_INFINITY > 0) assert(Process::RLIM_SAVED_MAX > 0) assert(Process::RLIM_SAVED_CUR > 0) end def test_process_type_flags unless RUBY_PLATFORM.match("linux") assert_not_nil(Process::P_ALL) assert_not_nil(Process::P_CID) assert_not_nil(Process::P_GID) assert_not_nil(Process::P_MYID) assert_not_nil(Process::P_PGID) assert_not_nil(Process::P_PID) assert_not_nil(Process::P_SID) assert_not_nil(Process::P_UID) end if RUBY_PLATFORM =~ /sunos|solaris/i assert_not_nil(Process::P_TASKID) assert_not_nil(Process::P_PROJID) end end def teardown @proc_stat = nil @proc_stat_members = nil end end