aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/process/spawn_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/process/spawn_spec.rb')
-rw-r--r--spec/ruby/core/process/spawn_spec.rb88
1 files changed, 64 insertions, 24 deletions
diff --git a/spec/ruby/core/process/spawn_spec.rb b/spec/ruby/core/process/spawn_spec.rb
index 1bd1dfac83..43e389ad7f 100644
--- a/spec/ruby/core/process/spawn_spec.rb
+++ b/spec/ruby/core/process/spawn_spec.rb
@@ -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