mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
don't use fork method.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d7e28fd0fe
commit
5adabb4010
1 changed files with 82 additions and 123 deletions
|
@ -5,6 +5,20 @@ require_relative 'envutil'
|
||||||
class TestProcess < Test::Unit::TestCase
|
class TestProcess < Test::Unit::TestCase
|
||||||
RUBY = EnvUtil.rubybin
|
RUBY = EnvUtil.rubybin
|
||||||
|
|
||||||
|
def write_file(filename, content)
|
||||||
|
File.open(filename, "w") {|f|
|
||||||
|
f << content
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_tmpchdir
|
||||||
|
Dir.mktmpdir {|d|
|
||||||
|
Dir.chdir(d) {
|
||||||
|
yield d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def test_rlimit_availability
|
def test_rlimit_availability
|
||||||
begin
|
begin
|
||||||
Process.getrlimit(nil)
|
Process.getrlimit(nil)
|
||||||
|
@ -25,26 +39,29 @@ class TestProcess < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_rlimit_nofile
|
def test_rlimit_nofile
|
||||||
return unless rlimit_exist?
|
return unless rlimit_exist?
|
||||||
pid = fork {
|
with_tmpchdir {
|
||||||
cur_nofile, max_nofile = Process.getrlimit(Process::RLIMIT_NOFILE)
|
write_file 's', <<-"End"
|
||||||
result = 1
|
cur_nofile, max_nofile = Process.getrlimit(Process::RLIMIT_NOFILE)
|
||||||
begin
|
result = 1
|
||||||
Process.setrlimit(Process::RLIMIT_NOFILE, 0, max_nofile)
|
begin
|
||||||
rescue Errno::EINVAL
|
Process.setrlimit(Process::RLIMIT_NOFILE, 0, max_nofile)
|
||||||
result = 0
|
rescue Errno::EINVAL
|
||||||
end
|
result = 0
|
||||||
if result == 1
|
end
|
||||||
begin
|
if result == 1
|
||||||
IO.pipe
|
begin
|
||||||
rescue Errno::EMFILE
|
IO.pipe
|
||||||
result = 0
|
rescue Errno::EMFILE
|
||||||
end
|
result = 0
|
||||||
end
|
end
|
||||||
Process.setrlimit(Process::RLIMIT_NOFILE, cur_nofile, max_nofile)
|
end
|
||||||
exit result
|
Process.setrlimit(Process::RLIMIT_NOFILE, cur_nofile, max_nofile)
|
||||||
|
exit result
|
||||||
|
End
|
||||||
|
pid = spawn RUBY, "s"
|
||||||
|
Process.wait pid
|
||||||
|
assert_equal(0, $?.to_i, "#{$?}")
|
||||||
}
|
}
|
||||||
Process.wait pid
|
|
||||||
assert_equal(0, $?.to_i, "#{$?}")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_rlimit_name
|
def test_rlimit_name
|
||||||
|
@ -79,14 +96,6 @@ class TestProcess < Test::Unit::TestCase
|
||||||
assert_raise(Errno::EPERM) { Process.setrlimit(:NOFILE, "INFINITY") }
|
assert_raise(Errno::EPERM) { Process.setrlimit(:NOFILE, "INFINITY") }
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_tmpchdir
|
|
||||||
Dir.mktmpdir {|d|
|
|
||||||
Dir.chdir(d) {
|
|
||||||
yield d
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
TRUECOMMAND = [RUBY, '-e', '']
|
TRUECOMMAND = [RUBY, '-e', '']
|
||||||
|
|
||||||
def test_execopts_opts
|
def test_execopts_opts
|
||||||
|
@ -113,7 +122,7 @@ class TestProcess < Test::Unit::TestCase
|
||||||
io.close
|
io.close
|
||||||
|
|
||||||
assert_raise(ArgumentError) { system(*TRUECOMMAND, :pgroup=>-1) }
|
assert_raise(ArgumentError) { system(*TRUECOMMAND, :pgroup=>-1) }
|
||||||
assert_raise(Errno::EPERM) { Process.wait spawn(*TRUECOMMAND, :pgroup=>1) }
|
assert_raise(Errno::EPERM) { Process.wait spawn(*TRUECOMMAND, :pgroup=>2) }
|
||||||
|
|
||||||
io1 = IO.popen([RUBY, "-e", "print Process.getpgrp", :pgroup=>true])
|
io1 = IO.popen([RUBY, "-e", "print Process.getpgrp", :pgroup=>true])
|
||||||
io2 = IO.popen([RUBY, "-e", "print Process.getpgrp", :pgroup=>io1.pid])
|
io2 = IO.popen([RUBY, "-e", "print Process.getpgrp", :pgroup=>io1.pid])
|
||||||
|
@ -305,7 +314,7 @@ class TestProcess < Test::Unit::TestCase
|
||||||
assert_equal("ggg\nhhh\n", File.read("out2"))
|
assert_equal("ggg\nhhh\n", File.read("out2"))
|
||||||
|
|
||||||
assert_raise(Errno::ENOENT) {
|
assert_raise(Errno::ENOENT) {
|
||||||
Process.wait Process.spawn("non-existing-command", (3..100).to_a=>["err", File::WRONLY|File::CREAT])
|
Process.wait Process.spawn("non-existing-command", (3..60).to_a=>["err", File::WRONLY|File::CREAT])
|
||||||
}
|
}
|
||||||
assert_equal("", File.read("err"))
|
assert_equal("", File.read("err"))
|
||||||
|
|
||||||
|
@ -386,9 +395,8 @@ class TestProcess < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_execopts_exec
|
def test_execopts_exec
|
||||||
with_tmpchdir {|d|
|
with_tmpchdir {|d|
|
||||||
pid = fork {
|
write_file("s", 'exec "echo aaa", STDOUT=>"foo"')
|
||||||
exec "echo aaa", STDOUT=>"foo"
|
pid = spawn RUBY, 's'
|
||||||
}
|
|
||||||
Process.wait pid
|
Process.wait pid
|
||||||
assert_equal("aaa\n", File.read("foo"))
|
assert_equal("aaa\n", File.read("foo"))
|
||||||
}
|
}
|
||||||
|
@ -447,13 +455,16 @@ class TestProcess < Test::Unit::TestCase
|
||||||
assert_equal("", r.read)
|
assert_equal("", r.read)
|
||||||
}
|
}
|
||||||
with_pipe {|r, w|
|
with_pipe {|r, w|
|
||||||
Process.wait fork {
|
with_tmpchdir {|d|
|
||||||
exec(RUBY, '-e',
|
write_file("s", <<-"End")
|
||||||
'IO.new(ARGV[0].to_i).puts("bu") rescue nil',
|
exec(#{RUBY.dump}, '-e',
|
||||||
w.fileno.to_s)
|
'IO.new(ARGV[0].to_i).puts("bu") rescue nil',
|
||||||
|
#{w.fileno.to_s.dump})
|
||||||
|
End
|
||||||
|
Process.wait spawn(RUBY, "s", :close_others=>false)
|
||||||
|
w.close
|
||||||
|
assert_equal("bu\n", r.read)
|
||||||
}
|
}
|
||||||
w.close
|
|
||||||
assert_equal("bu\n", r.read)
|
|
||||||
}
|
}
|
||||||
with_pipe {|r, w|
|
with_pipe {|r, w|
|
||||||
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}).puts('me')"])
|
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}).puts('me')"])
|
||||||
|
@ -492,7 +503,13 @@ class TestProcess < Test::Unit::TestCase
|
||||||
assert_equal("bi\n", r.read)
|
assert_equal("bi\n", r.read)
|
||||||
}
|
}
|
||||||
with_pipe {|r, w|
|
with_pipe {|r, w|
|
||||||
Process.wait fork { exec(RUBY, '-e', 'STDERR.reopen("err", "w"); IO.new(ARGV[0].to_i).puts("mu")', w.fileno.to_s, :close_others=>true) }
|
write_file("s", <<-"End")
|
||||||
|
exec(#{RUBY.dump}, '-e',
|
||||||
|
'STDERR.reopen("err", "w"); IO.new(ARGV[0].to_i).puts("mu")',
|
||||||
|
#{w.fileno.to_s.dump},
|
||||||
|
:close_others=>true)
|
||||||
|
End
|
||||||
|
Process.wait spawn(RUBY, "s", :close_others=>false)
|
||||||
w.close
|
w.close
|
||||||
assert_equal("", r.read)
|
assert_equal("", r.read)
|
||||||
assert_not_equal("", File.read("err"))
|
assert_not_equal("", File.read("err"))
|
||||||
|
@ -523,24 +540,6 @@ class TestProcess < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_execopts_redirect_self
|
|
||||||
with_pipe {|r, w|
|
|
||||||
w << "haha\n"
|
|
||||||
w.close
|
|
||||||
r.close_on_exec = true
|
|
||||||
IO.popen([RUBY, "-e", "print IO.new(#{r.fileno}).read", r.fileno=>r.fileno, :close_others=>false]) {|io|
|
|
||||||
assert_equal("haha\n", io.read)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_execopts_duplex_io
|
|
||||||
IO.popen("#{RUBY} -e ''", "r+") {|duplex|
|
|
||||||
assert_raise(ArgumentError) { system("#{RUBY} -e ''", duplex=>STDOUT) }
|
|
||||||
assert_raise(ArgumentError) { system("#{RUBY} -e ''", STDOUT=>duplex) }
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_execopts_modification
|
def test_execopts_modification
|
||||||
h = {}
|
h = {}
|
||||||
Process.wait spawn(*TRUECOMMAND, h)
|
Process.wait spawn(*TRUECOMMAND, h)
|
||||||
|
@ -572,25 +571,23 @@ class TestProcess < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exec_noshell
|
def test_exec_noshell
|
||||||
str = "echo non existing command name which contains spaces"
|
with_tmpchdir {|d|
|
||||||
with_pipe {|r, w|
|
with_pipe {|r, w|
|
||||||
pid = fork {
|
write_file("s", <<-"End")
|
||||||
STDOUT.reopen(w)
|
str = "echo non existing command name which contains spaces"
|
||||||
STDERR.reopen(w)
|
w = IO.new(#{w.fileno})
|
||||||
begin
|
STDOUT.reopen(w)
|
||||||
exec [str, str]
|
STDERR.reopen(w)
|
||||||
rescue Errno::ENOENT
|
begin
|
||||||
w.write "Errno::ENOENT success"
|
exec [str, str]
|
||||||
end
|
rescue Errno::ENOENT
|
||||||
|
w.write "Errno::ENOENT success"
|
||||||
|
end
|
||||||
|
End
|
||||||
|
system(RUBY, "s", :close_others=>false)
|
||||||
|
w.close
|
||||||
|
assert_equal("Errno::ENOENT success", r.read)
|
||||||
}
|
}
|
||||||
w.close
|
|
||||||
assert_equal("Errno::ENOENT success", r.read)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def write_file(filename, content)
|
|
||||||
File.open(filename, "w") {|f|
|
|
||||||
f << content
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -652,10 +649,11 @@ class TestProcess < Test::Unit::TestCase
|
||||||
File.open("result", "w") {|t| t << "hehe pid=#{$$} ppid=#{Process.ppid}" }
|
File.open("result", "w") {|t| t << "hehe pid=#{$$} ppid=#{Process.ppid}" }
|
||||||
exit 6
|
exit 6
|
||||||
End
|
End
|
||||||
str = "#{RUBY} script"
|
write_file("s", <<-"End")
|
||||||
pid = fork {
|
ruby = #{RUBY.dump}
|
||||||
exec str
|
exec "\#{ruby} script"
|
||||||
}
|
End
|
||||||
|
pid = spawn(RUBY, "s")
|
||||||
Process.wait pid
|
Process.wait pid
|
||||||
status = $?
|
status = $?
|
||||||
assert_equal(pid, status.pid)
|
assert_equal(pid, status.pid)
|
||||||
|
@ -741,9 +739,11 @@ class TestProcess < Test::Unit::TestCase
|
||||||
File.open("result2", "w") {|t| t << "tiku pid=#{$$} ppid=#{Process.ppid}" }
|
File.open("result2", "w") {|t| t << "tiku pid=#{$$} ppid=#{Process.ppid}" }
|
||||||
exit 8
|
exit 8
|
||||||
End
|
End
|
||||||
pid = fork {
|
write_file("s", <<-"End")
|
||||||
exec("#{RUBY} script1; #{RUBY} script2")
|
ruby = #{RUBY.dump}
|
||||||
}
|
exec("\#{ruby} script1; \#{ruby} script2")
|
||||||
|
End
|
||||||
|
pid = spawn RUBY, "s"
|
||||||
Process.wait pid
|
Process.wait pid
|
||||||
status = $?
|
status = $?
|
||||||
assert(status.exited?)
|
assert(status.exited?)
|
||||||
|
@ -756,45 +756,4 @@ class TestProcess < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_argv0
|
|
||||||
assert_equal(false, system([RUBY, "asdfg"], "-e", "exit false"))
|
|
||||||
assert_equal(true, system([RUBY, "zxcvb"], "-e", "exit true"))
|
|
||||||
|
|
||||||
Process.wait spawn([RUBY, "poiu"], "-e", "exit 4")
|
|
||||||
assert_equal(4, $?.exitstatus)
|
|
||||||
|
|
||||||
assert_equal("1", IO.popen([[RUBY, "qwerty"], "-e", "print 1"]).read)
|
|
||||||
|
|
||||||
pid = fork {
|
|
||||||
exec([RUBY, "lkjh"], "-e", "exit 5")
|
|
||||||
}
|
|
||||||
Process.wait pid
|
|
||||||
assert_equal(5, $?.exitstatus)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_argv0_noarg
|
|
||||||
with_tmpchdir {|d|
|
|
||||||
open("t", "w") {|f| f.print "exit true" }
|
|
||||||
open("f", "w") {|f| f.print "exit false" }
|
|
||||||
|
|
||||||
assert_equal(true, system([RUBY, "qaz"], STDIN=>"t"))
|
|
||||||
assert_equal(false, system([RUBY, "wsx"], STDIN=>"f"))
|
|
||||||
|
|
||||||
Process.wait spawn([RUBY, "edc"], STDIN=>"t")
|
|
||||||
assert($?.success?)
|
|
||||||
Process.wait spawn([RUBY, "rfv"], STDIN=>"f")
|
|
||||||
assert(!$?.success?)
|
|
||||||
|
|
||||||
IO.popen([[RUBY, "tgb"], STDIN=>"t"]) {|io| assert_equal("", io.read) }
|
|
||||||
assert($?.success?)
|
|
||||||
IO.popen([[RUBY, "yhn"], STDIN=>"f"]) {|io| assert_equal("", io.read) }
|
|
||||||
assert(!$?.success?)
|
|
||||||
|
|
||||||
Process.wait fork { exec([RUBY, "ujm"], STDIN=>"t") }
|
|
||||||
assert($?.success?)
|
|
||||||
Process.wait fork { exec([RUBY, "ik,"], STDIN=>"f") }
|
|
||||||
assert(!$?.success?)
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue