summaryrefslogtreecommitdiff
path: root/test/lib
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-06-11 11:13:26 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-06-11 23:15:46 +0900
commitc4cbaef216ffcc9bda70cc328a805ad679ccaa8c (patch)
treec9070910a461abca183c4d0fceea7cc922e149d2 /test/lib
parent42f0a8fd6f7e1f4afcb17aeb34e9f8ddf8d66b9b (diff)
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.
Diffstat (limited to 'test/lib')
-rw-r--r--test/lib/test/unit/assertions.rb20
1 files changed, 16 insertions, 4 deletions
diff --git a/test/lib/test/unit/assertions.rb b/test/lib/test/unit/assertions.rb
index 44316579b4..da552e3b0e 100644
--- a/test/lib/test/unit/assertions.rb
+++ b/test/lib/test/unit/assertions.rb
@@ -761,15 +761,27 @@ eom
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