[前][次][番号順一覧][スレッド一覧]

ruby-changes:56083

From: Nobuyoshi <ko1@a...>
Date: Tue, 11 Jun 2019 23:20:55 +0900 (JST)
Subject: [ruby-changes:56083] Nobuyoshi Nakada: c4cbaef216 (trunk): assert_cpu_usage_low with timeout scale

https://git.ruby-lang.org/ruby.git/commit/?id=c4cbaef216

From c4cbaef216ffcc9bda70cc328a805ad679ccaa8c Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Tue, 11 Jun 2019 11:13:26 +0900
Subject: assert_cpu_usage_low with timeout scale

* test/lib/test/unit/assertions.rb (assert_cpu_usage_low): apply
  the timeout scale to measuring period.  this assertion is very
  runtime environment dependent.

diff --git a/test/lib/test/unit/assertions.rb b/test/lib/test/unit/assertions.rb
index 4431657..da552e3 100644
--- a/test/lib/test/unit/assertions.rb
+++ b/test/lib/test/unit/assertions.rb
@@ -761,15 +761,27 @@ eom https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit/assertions.rb#L761
       MIN_HZ = MiniTest::Unit::TestCase.windows? ? 67 : 100
       MIN_MEASURABLE = 1.0 / MIN_HZ
 
-      def assert_cpu_usage_low(msg = nil, pct: 0.05)
+      def assert_cpu_usage_low(msg = nil, pct: 0.05, wait: 0.1, stop: nil)
         require 'benchmark'
 
-        tms = Benchmark.measure(msg || '') { yield }
-        max = pct * tms.real
-        if tms.real < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c
+        wait = EnvUtil.apply_timeout_scale(wait)
+        if wait < 0.1 # TIME_QUANTUM_USEC in thread_pthread.c
           warn "test #{msg || 'assert_cpu_usage_low'} too short to be accurate"
         end
+        tms = Benchmark.measure(msg || '') do
+          if stop
+            th = Thread.start {sleep wait; stop.call}
+            yield
+            th.join
+          else
+            begin
+              Timeout.timeout(wait) {yield}
+            rescue Timeout::Error
+            end
+          end
+        end
 
+        max = pct * tms.real
         min_measurable = MIN_MEASURABLE
         min_measurable *= 1.30 # add a little (30%) to account for misc. overheads
         if max < min_measurable
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 7f40764..4f66685 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -567,11 +567,8 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L567
       msg = 'r58534 [ruby-core:80969] [Backport #13533]'
       IO.pipe do |r,w|
         r.nonblock = true
-        assert_cpu_usage_low(msg) do
-          th = Thread.new { IO.copy_stream(r, IO::NULL) }
-          sleep 0.1
-          w.close
-          th.join
+        assert_cpu_usage_low(msg, stop: ->{w.close}) do
+          IO.copy_stream(r, IO::NULL)
         end
       end
     end
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]