aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/process/times_spec.rb
blob: 510ea899b99e4a5509053f557659cd57d0cf48c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require_relative '../../spec_helper'

describe "Process.times" do
  it "returns a Process::Tms" do
    Process.times.should be_kind_of(Process::Tms)
  end

  it "returns current cpu times" do
    t = Process.times
    user = t.utime

    1 until Process.times.utime > user
    Process.times.utime.should > user
  end

  ruby_version_is "2.5" do
    platform_is_not :windows do
      it "uses getrusage when available to improve precision beyond milliseconds" do
        gettimes = 100.times.map { Process.clock_gettime(:GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) }
        if gettimes.count { |t| ((t * 1e6).to_i % 1000) > 0 } == 0
          skip "getrusage is not supported on this environment"
        end

        times = 100.times.map { Process.times }

        # Creating custom matcher to show a debug message to investigate random failure
        # on Debian ci.rvm.jp like: https://gist.github.com/ko1/346983a66ba66cf288249383ca30f15a
        larger_than_0 = Class.new do
          def initialize(expected, times:, gettimes:)
            @expected = expected
            @times = times
            @gettimes = gettimes
          end

          def matches?(actual)
            @actual = actual
            @actual > @expected
          end

          def failure_message
            ["Expected #{@actual} > #{@expected}",
             "to be truthy but was false. (times: #{pp(@times)}, gettimes: #{pp(@gettimes)})"]
          end

          alias :negative_failure_message :failure_message

          private def pp(obj)
            require 'pp'
            PP.pp(obj, '')
          end
        end.new(0, times: times, gettimes: gettimes)

        times.count { |t| ((t.utime * 1e6).to_i % 1000) > 0 }.should(larger_than_0)
        times.count { |t| ((t.stime * 1e6).to_i % 1000) > 0 }.should(larger_than_0)
      end
    end
  end
end