summaryrefslogtreecommitdiff
path: root/spec/ruby/core/process
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2019-11-30 21:26:52 +0100
committerBenoit Daloze <eregontp@gmail.com>2019-11-30 21:26:52 +0100
commit1243255c3a36433041012b6107a5ac48658a0895 (patch)
tree04440f84b48999ff08d4a2a16d066d0ad731400e /spec/ruby/core/process
parentab8345271eb87ff155d8bd5f22f53a4cf2902c26 (diff)
Update to ruby/spec@4eec3dc
Diffstat (limited to 'spec/ruby/core/process')
-rw-r--r--spec/ruby/core/process/clock_getres_spec.rb5
-rw-r--r--spec/ruby/core/process/clock_gettime_spec.rb93
-rw-r--r--spec/ruby/core/process/exec_spec.rb33
-rw-r--r--spec/ruby/core/process/fixtures/common.rb8
-rw-r--r--spec/ruby/core/process/status/equal_value_spec.rb12
-rw-r--r--spec/ruby/core/process/status/exitstatus_spec.rb2
-rw-r--r--spec/ruby/core/process/status/termsig_spec.rb8
-rw-r--r--spec/ruby/core/process/status/to_i_spec.rb10
-rw-r--r--spec/ruby/core/process/times_spec.rb26
-rw-r--r--spec/ruby/core/process/tms/cstime_spec.rb9
-rw-r--r--spec/ruby/core/process/tms/cutime_spec.rb9
-rw-r--r--spec/ruby/core/process/tms/element_reference_spec.rb5
-rw-r--r--spec/ruby/core/process/tms/members_spec.rb5
-rw-r--r--spec/ruby/core/process/tms/new_spec.rb5
-rw-r--r--spec/ruby/core/process/tms/stime_spec.rb9
-rw-r--r--spec/ruby/core/process/tms/utime_spec.rb9
16 files changed, 164 insertions, 84 deletions
diff --git a/spec/ruby/core/process/clock_getres_spec.rb b/spec/ruby/core/process/clock_getres_spec.rb
index 7112b0520a..f1ecb74010 100644
--- a/spec/ruby/core/process/clock_getres_spec.rb
+++ b/spec/ruby/core/process/clock_getres_spec.rb
@@ -8,10 +8,7 @@ describe "Process.clock_getres" do
# NOTE: Look at fixtures/clocks.rb for clock and OS-specific exclusions
ProcessSpecs.clock_constants_for_resolution_checks.each do |name, value|
it "matches the clock in practice for Process::#{name}" do
- times = []
- 10_000.times do
- times << Process.clock_gettime(value, :nanosecond)
- end
+ times = 10_000.times.map { Process.clock_gettime(value, :nanosecond) }
reported = Process.clock_getres(value, :nanosecond)
# The clock should not be more accurate than reported (times should be
diff --git a/spec/ruby/core/process/clock_gettime_spec.rb b/spec/ruby/core/process/clock_gettime_spec.rb
index d3f973d7ae..59e1406e02 100644
--- a/spec/ruby/core/process/clock_gettime_spec.rb
+++ b/spec/ruby/core/process/clock_gettime_spec.rb
@@ -41,4 +41,97 @@ describe "Process.clock_gettime" do
t2.should be_close(t1, TIME_TOLERANCE)
end
end
+
+ describe "supports the platform clocks mentioned in the documentation" do
+ it "CLOCK_REALTIME" do
+ Process.clock_gettime(Process::CLOCK_REALTIME).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_MONOTONIC" do
+ Process.clock_gettime(Process::CLOCK_MONOTONIC).should be_an_instance_of(Float)
+ end
+
+ # These specs need macOS 10.12+ / darwin 16+
+ guard_not -> { platform_is_not(:darwin) or RUBY_PLATFORM[/darwin\d+/].to_i >= 16 } do
+ platform_is :linux, :openbsd, :darwin do
+ it "CLOCK_PROCESS_CPUTIME_ID" do
+ Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID).should be_an_instance_of(Float)
+ end
+ end
+
+ platform_is :linux, :freebsd, :openbsd, :darwin do
+ it "CLOCK_THREAD_CPUTIME_ID" do
+ Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID).should be_an_instance_of(Float)
+ end
+ end
+
+ platform_is :freebsd, :openbsd do
+ it "CLOCK_VIRTUAL" do
+ Process.clock_gettime(Process::CLOCK_VIRTUAL).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_PROF" do
+ Process.clock_gettime(Process::CLOCK_PROF).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_UPTIME" do
+ Process.clock_gettime(Process::CLOCK_UPTIME).should be_an_instance_of(Float)
+ end
+ end
+
+ platform_is :linux, :darwin do
+ it "CLOCK_MONOTONIC_RAW" do
+ Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW).should be_an_instance_of(Float)
+ end
+ end
+
+ platform_is :darwin do
+ it "CLOCK_MONOTONIC_RAW_APPROX" do
+ Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW_APPROX).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_UPTIME_RAW and CLOCK_UPTIME_RAW_APPROX" do
+ Process.clock_gettime(Process::CLOCK_UPTIME_RAW).should be_an_instance_of(Float)
+ Process.clock_gettime(Process::CLOCK_UPTIME_RAW_APPROX).should be_an_instance_of(Float)
+ end
+ end
+
+ platform_is :freebsd do
+ it "CLOCK_REALTIME_FAST and CLOCK_REALTIME_PRECISE" do
+ Process.clock_gettime(Process::CLOCK_REALTIME_FAST).should be_an_instance_of(Float)
+ Process.clock_gettime(Process::CLOCK_REALTIME_PRECISE).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_MONOTONIC_FAST and CLOCK_MONOTONIC_PRECISE" do
+ Process.clock_gettime(Process::CLOCK_MONOTONIC_FAST).should be_an_instance_of(Float)
+ Process.clock_gettime(Process::CLOCK_MONOTONIC_PRECISE).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_UPTIME_FAST and CLOCK_UPTIME_PRECISE" do
+ Process.clock_gettime(Process::CLOCK_UPTIME_FAST).should be_an_instance_of(Float)
+ Process.clock_gettime(Process::CLOCK_UPTIME_PRECISE).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_SECOND" do
+ Process.clock_gettime(Process::CLOCK_SECOND).should be_an_instance_of(Float)
+ end
+ end
+
+ platform_is :linux do
+ it "CLOCK_REALTIME_COARSE and CLOCK_REALTIME_ALARM" do
+ Process.clock_gettime(Process::CLOCK_REALTIME_COARSE).should be_an_instance_of(Float)
+ Process.clock_gettime(Process::CLOCK_REALTIME_ALARM).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_MONOTONIC_COARSE" do
+ Process.clock_gettime(Process::CLOCK_MONOTONIC_COARSE).should be_an_instance_of(Float)
+ end
+
+ it "CLOCK_BOOTTIME and CLOCK_BOOTTIME_ALARM" do
+ Process.clock_gettime(Process::CLOCK_BOOTTIME).should be_an_instance_of(Float)
+ Process.clock_gettime(Process::CLOCK_BOOTTIME_ALARM).should be_an_instance_of(Float)
+ end
+ end
+ end
+ end
end
diff --git a/spec/ruby/core/process/exec_spec.rb b/spec/ruby/core/process/exec_spec.rb
index 848990c6dc..5a6e3fc1a4 100644
--- a/spec/ruby/core/process/exec_spec.rb
+++ b/spec/ruby/core/process/exec_spec.rb
@@ -200,9 +200,9 @@ describe "Process.exec" do
it "maps the key to a file descriptor in the child that inherits the file descriptor from the parent specified by the value" do
map_fd_fixture = fixture __FILE__, "map_fd.rb"
cmd = <<-EOC
- f = File.open("#{@name}", "w+")
+ f = File.open(#{@name.inspect}, "w+")
child_fd = f.fileno + 1
- File.open("#{@child_fd_file}", "w") { |io| io.print child_fd }
+ File.open(#{@child_fd_file.inspect}, "w") { |io| io.print child_fd }
Process.exec "#{ruby_cmd(map_fd_fixture)} \#{child_fd}", { child_fd => f }
EOC
@@ -212,6 +212,35 @@ describe "Process.exec" do
File.read(@name).should == "writing to fd: #{child_fd}"
end
+
+ it "lets the process after exec have specified file descriptor despite close_on_exec" do
+ map_fd_fixture = fixture __FILE__, "map_fd.rb"
+ cmd = <<-EOC
+ f = File.open(#{@name.inspect}, 'w+')
+ puts(f.fileno, f.close_on_exec?)
+ STDOUT.flush
+ Process.exec("#{ruby_cmd(map_fd_fixture)} \#{f.fileno}", f.fileno => f.fileno)
+ EOC
+
+ output = ruby_exe(cmd, escape: true)
+ child_fd, close_on_exec = output.split
+
+ child_fd.to_i.should > STDERR.fileno
+ close_on_exec.should == 'true'
+ File.read(@name).should == "writing to fd: #{child_fd}"
+ end
+
+ it "sets close_on_exec to false on specified fd even when it fails" do
+ cmd = <<-EOC
+ f = File.open(#{__FILE__.inspect}, 'r')
+ puts(f.close_on_exec?)
+ Process.exec('/', f.fileno => f.fileno) rescue nil
+ puts(f.close_on_exec?)
+ EOC
+
+ output = ruby_exe(cmd, escape: true)
+ output.split.should == ['true', 'false']
+ end
end
end
end
diff --git a/spec/ruby/core/process/fixtures/common.rb b/spec/ruby/core/process/fixtures/common.rb
index bdbf1e654b..f49513d262 100644
--- a/spec/ruby/core/process/fixtures/common.rb
+++ b/spec/ruby/core/process/fixtures/common.rb
@@ -3,11 +3,15 @@ module ProcessSpecs
if defined?(MSpecScript::SYSTEM_RUBY)
context.send(:before, :all) do
@ruby = ::RUBY_EXE
- Object.const_set(:RUBY_EXE, MSpecScript::SYSTEM_RUBY)
+ suppress_warning {
+ Object.const_set(:RUBY_EXE, MSpecScript::SYSTEM_RUBY)
+ }
end
context.send(:after, :all) do
- Object.const_set(:RUBY_EXE, @ruby)
+ suppress_warning {
+ Object.const_set(:RUBY_EXE, @ruby)
+ }
end
end
end
diff --git a/spec/ruby/core/process/status/equal_value_spec.rb b/spec/ruby/core/process/status/equal_value_spec.rb
index 9e9a2d0a2b..444ce1775b 100644
--- a/spec/ruby/core/process/status/equal_value_spec.rb
+++ b/spec/ruby/core/process/status/equal_value_spec.rb
@@ -1,5 +1,15 @@
require_relative '../../../spec_helper'
describe "Process::Status#==" do
- it "needs to be reviewed for spec completeness"
+ it "returns true when compared to the integer status of an exited child" do
+ ruby_exe("exit(29)")
+ $?.to_i.should == $?
+ $?.should == $?.to_i
+ end
+
+ it "returns true when compared to the integer status of a terminated child" do
+ ruby_exe("Process.kill(:KILL, $$); exit(29)")
+ $?.to_i.should == $?
+ $?.should == $?.to_i
+ end
end
diff --git a/spec/ruby/core/process/status/exitstatus_spec.rb b/spec/ruby/core/process/status/exitstatus_spec.rb
index cd46b2081f..d6c6965b9e 100644
--- a/spec/ruby/core/process/status/exitstatus_spec.rb
+++ b/spec/ruby/core/process/status/exitstatus_spec.rb
@@ -11,7 +11,7 @@ describe "Process::Status#exitstatus" do
describe "for a child that raised SignalException" do
before :each do
- ruby_exe("raise SignalException, 'SIGTERM'")
+ ruby_exe("Process.kill(:KILL, $$); exit(42)")
end
platform_is_not :windows do
diff --git a/spec/ruby/core/process/status/termsig_spec.rb b/spec/ruby/core/process/status/termsig_spec.rb
index 1482d27146..204708bc1b 100644
--- a/spec/ruby/core/process/status/termsig_spec.rb
+++ b/spec/ruby/core/process/status/termsig_spec.rb
@@ -1,9 +1,7 @@
require_relative '../../../spec_helper'
describe "Process::Status#termsig" do
-
describe "for a child that exited normally" do
-
before :each do
ruby_exe("exit(0)")
end
@@ -26,26 +24,20 @@ describe "Process::Status#termsig" do
end
describe "for a child that was sent a signal" do
-
before :each do
ruby_exe("Process.kill(:KILL, $$); exit(42)")
end
platform_is_not :windows do
-
it "returns the signal" do
$?.termsig.should == Signal.list["KILL"]
end
-
end
platform_is :windows do
-
it "always returns nil" do
$?.termsig.should be_nil
end
-
end
-
end
end
diff --git a/spec/ruby/core/process/status/to_i_spec.rb b/spec/ruby/core/process/status/to_i_spec.rb
index 37b7bdb1e4..a284f64f86 100644
--- a/spec/ruby/core/process/status/to_i_spec.rb
+++ b/spec/ruby/core/process/status/to_i_spec.rb
@@ -1,5 +1,13 @@
require_relative '../../../spec_helper'
describe "Process::Status#to_i" do
- it "needs to be reviewed for spec completeness"
+ it "returns an integer when the child exits" do
+ ruby_exe('exit 48')
+ $?.to_i.should be_an_instance_of(Integer)
+ end
+
+ it "returns an integer when the child is signaled" do
+ ruby_exe('raise SignalException, "TERM"')
+ $?.to_i.should be_an_instance_of(Integer)
+ end
end
diff --git a/spec/ruby/core/process/times_spec.rb b/spec/ruby/core/process/times_spec.rb
index 07b4fa5c9f..f148954262 100644
--- a/spec/ruby/core/process/times_spec.rb
+++ b/spec/ruby/core/process/times_spec.rb
@@ -7,21 +7,19 @@ describe "Process.times" do
it "returns current cpu times" do
t = Process.times
+ user = t.utime
- # Do busy work for a wall-clock interval.
- start = Time.now
- 1 until (Time.now - start) > 0.5
+ 1 until Process.times.utime > user
+ Process.times.utime.should > user
+ end
- # Ensure times is larger. NOTE that there is no
- # guarantee of an upper bound since anything may be
- # happening at the OS level, so we ONLY check that at
- # least an interval has elapsed. Also, we are assuming
- # there is a correlation between wall clock time and
- # process time. In practice, there is an observed
- # discrepancy often 10% or greater. In other words,
- # this is a very fuzzy test.
- t2 = Process.times
- diff = (t2.utime + t2.stime) - (t.utime + t.stime)
- diff.should > 0
+ ruby_version_is "2.5" do
+ platform_is_not :windows do
+ it "uses getrusage when available to improve precision beyond milliseconds" do
+ times = 100.times.map { Process.times }
+ times.count { |t| ((t.utime * 1e6).to_i % 1000) > 0 }.should > 0
+ times.count { |t| ((t.stime * 1e6).to_i % 1000) > 0 }.should > 0
+ end
+ end
end
end
diff --git a/spec/ruby/core/process/tms/cstime_spec.rb b/spec/ruby/core/process/tms/cstime_spec.rb
deleted file mode 100644
index 207d4391c0..0000000000
--- a/spec/ruby/core/process/tms/cstime_spec.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-require_relative '../../../spec_helper'
-
-describe "Process::Tms#cstime" do
- it "needs to be reviewed for spec completeness"
-end
-
-describe "Process::Tms#cstime=" do
- it "needs to be reviewed for spec completeness"
-end
diff --git a/spec/ruby/core/process/tms/cutime_spec.rb b/spec/ruby/core/process/tms/cutime_spec.rb
deleted file mode 100644
index 390280f005..0000000000
--- a/spec/ruby/core/process/tms/cutime_spec.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-require_relative '../../../spec_helper'
-
-describe "Process::Tms#cutime" do
- it "needs to be reviewed for spec completeness"
-end
-
-describe "Process::Tms#cutime=" do
- it "needs to be reviewed for spec completeness"
-end
diff --git a/spec/ruby/core/process/tms/element_reference_spec.rb b/spec/ruby/core/process/tms/element_reference_spec.rb
deleted file mode 100644
index 84a34089ae..0000000000
--- a/spec/ruby/core/process/tms/element_reference_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require_relative '../../../spec_helper'
-
-describe "Process::Tms.[]" do
- it "needs to be reviewed for spec completeness"
-end
diff --git a/spec/ruby/core/process/tms/members_spec.rb b/spec/ruby/core/process/tms/members_spec.rb
deleted file mode 100644
index 005a8baec1..0000000000
--- a/spec/ruby/core/process/tms/members_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require_relative '../../../spec_helper'
-
-describe "Process::Tms.members" do
- it "needs to be reviewed for spec completeness"
-end
diff --git a/spec/ruby/core/process/tms/new_spec.rb b/spec/ruby/core/process/tms/new_spec.rb
deleted file mode 100644
index 9dd1f5a8f2..0000000000
--- a/spec/ruby/core/process/tms/new_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require_relative '../../../spec_helper'
-
-describe "Process::Tms.new" do
- it "needs to be reviewed for spec completeness"
-end
diff --git a/spec/ruby/core/process/tms/stime_spec.rb b/spec/ruby/core/process/tms/stime_spec.rb
deleted file mode 100644
index 4104b625e2..0000000000
--- a/spec/ruby/core/process/tms/stime_spec.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-require_relative '../../../spec_helper'
-
-describe "Process::Tms#stime" do
- it "needs to be reviewed for spec completeness"
-end
-
-describe "Process::Tms#stime=" do
- it "needs to be reviewed for spec completeness"
-end
diff --git a/spec/ruby/core/process/tms/utime_spec.rb b/spec/ruby/core/process/tms/utime_spec.rb
deleted file mode 100644
index 28371590e9..0000000000
--- a/spec/ruby/core/process/tms/utime_spec.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-require_relative '../../../spec_helper'
-
-describe "Process::Tms#utime" do
- it "needs to be reviewed for spec completeness"
-end
-
-describe "Process::Tms#utime=" do
- it "needs to be reviewed for spec completeness"
-end