1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66622 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2018-12-29 00:22:52 +00:00
parent 548defb608
commit 2076c2c3c4
34 changed files with 759 additions and 328 deletions

View file

@ -11,22 +11,22 @@ describe :process_spawn_does_not_close_std_streams, shared: true do
it "does not close STDIN" do
code = "STDOUT.puts STDIN.read(0).inspect"
cmd = "Process.wait Process.spawn(#{ruby_cmd(code).inspect}, #{@options.inspect})"
ruby_exe(cmd, args: "> #{@output}")
File.binread(@output).should == %[""#{newline}]
ruby_exe(cmd, args: "> #{@name}")
File.binread(@name).should == %[""#{newline}]
end
it "does not close STDOUT" do
code = "STDOUT.puts 'hello'"
cmd = "Process.wait Process.spawn(#{ruby_cmd(code).inspect}, #{@options.inspect})"
ruby_exe(cmd, args: "> #{@output}")
File.binread(@output).should == "hello#{newline}"
ruby_exe(cmd, args: "> #{@name}")
File.binread(@name).should == "hello#{newline}"
end
it "does not close STDERR" do
code = "STDERR.puts 'hello'"
cmd = "Process.wait Process.spawn(#{ruby_cmd(code).inspect}, #{@options.inspect})"
ruby_exe(cmd, args: "2> #{@output}")
File.binread(@output).should =~ /hello#{newline}/
ruby_exe(cmd, args: "2> #{@name}")
File.binread(@name).should =~ /hello#{newline}/
end
end
end
@ -391,6 +391,50 @@ describe "Process.spawn" do
end
end
# chdir
platform_is :linux do
describe "inside Dir.chdir" do
def child_pids(pid)
`pgrep -P #{pid}`.lines.map { |child| Integer(child) }
end
it "does not create extra process without chdir" do
pid = Process.spawn("sleep 10")
begin
child_pids(pid).size.should == 0
ensure
Process.kill("TERM", pid)
Process.wait(pid)
end
end
it "kills extra chdir processes" do
pid = nil
Dir.chdir("/tmp") do
pid = Process.spawn("sleep 10")
end
children = child_pids(pid)
children.size.should <= 1
Process.kill("TERM", pid)
Process.wait(pid)
if children.size > 0
# wait a bit for children to die
sleep(1)
children.each do |child|
lambda do
Process.kill("TERM", child)
end.should raise_error(Errno::ESRCH)
end
end
end
end
end
# :umask
it "uses the current umask by default" do
@ -490,20 +534,19 @@ describe "Process.spawn" do
context "when passed close_others: true" do
before :each do
@output = tmp("spawn_close_others_true")
@options = { close_others: true }
end
after :each do
rm_r @output
end
it "closes file descriptors >= 3 in the child process" do
it "closes file descriptors >= 3 in the child process even if fds are set close_on_exec=false" do
touch @name
IO.pipe do |r, w|
r.close_on_exec = false
w.close_on_exec = false
begin
pid = Process.spawn(ruby_cmd("while File.exist? '#{@name}'; sleep 0.1; end"), @options)
w.close
lambda { r.read_nonblock(1) }.should raise_error(EOFError)
r.read(1).should == nil
ensure
rm_r @name
Process.wait(pid) if pid
@ -516,20 +559,16 @@ describe "Process.spawn" do
context "when passed close_others: false" do
before :each do
@output = tmp("spawn_close_others_false")
@options = { close_others: false }
end
after :each do
rm_r @output
end
it "closes file descriptors >= 3 in the child process because they are set close_on_exec by default" do
touch @name
IO.pipe do |r, w|
begin
pid = Process.spawn(ruby_cmd("while File.exist? '#{@name}'; sleep 0.1; end"), @options)
w.close
lambda { r.read_nonblock(1) }.should raise_error(EOFError)
r.read(1).should == nil
ensure
rm_r @name
Process.wait(pid) if pid
@ -542,13 +581,14 @@ describe "Process.spawn" do
IO.pipe do |r, w|
r.close_on_exec = false
w.close_on_exec = false
code = "fd = IO.for_fd(#{w.fileno}); fd.write 'abc'; fd.close"
pid = Process.spawn(ruby_cmd(code), @options)
begin
pid = Process.spawn(ruby_cmd("while File.exist? '#{@name}'; sleep 0.1; end"), @options)
w.close
lambda { r.read_nonblock(1) }.should raise_error(Errno::EAGAIN)
r.read.should == 'abc'
ensure
rm_r @name
Process.wait(pid) if pid
Process.wait(pid)
end
end
end
@ -623,7 +663,7 @@ describe "Process.spawn" do
end
it "maps the key to a file descriptor in the child that inherits the file descriptor from the parent specified by the value" do
child_fd = @io.fileno + 1
child_fd = find_unused_fd
args = ruby_cmd(fixture(__FILE__, "map_fd.rb"), args: [child_fd.to_s])
pid = Process.spawn(*args, { child_fd => @io })
Process.waitpid pid