aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authornormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-27 07:23:02 +0000
committernormal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-27 07:23:02 +0000
commit0b5ef5f37d9a1258f9a039864f442a4d30e6e1df (patch)
tree49334554915e589e13839d94b877507f15e32fbd /test
parentba4d0899cf134abf0927d1ebe11d9ac033baf9d4 (diff)
downloadruby-0b5ef5f37d9a1258f9a039864f442a4d30e6e1df.tar.gz
test for IO.copy_stream CPU usage (r58534)
I'm likely to make similar mistakes in the future when working on Fiber auto-scheduling. Start adding assertions for existing code, first. * test/ruby/test_io.rb (test_copy_stream_no_busy_wait): added * test/lib/test/unit/assertions.rb (assert_cpu_usage_low): added git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58924 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/lib/test/unit/assertions.rb7
-rw-r--r--test/ruby/test_io.rb12
2 files changed, 19 insertions, 0 deletions
diff --git a/test/lib/test/unit/assertions.rb b/test/lib/test/unit/assertions.rb
index 976a6fd04a..92f754064a 100644
--- a/test/lib/test/unit/assertions.rb
+++ b/test/lib/test/unit/assertions.rb
@@ -715,6 +715,13 @@ eom
skip
end
+ def assert_cpu_usage_low(msg = nil, pct: 0.005)
+ require 'benchmark'
+ tms = Benchmark.measure(msg || '') { yield }
+ max = pct * tms.real
+ assert_operator tms.total, :<=, max, msg
+ end
+
def assert_is_minus_zero(f)
assert(1.0/f == -Float::INFINITY, "#{f} is not -0.0")
end
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 2b82470b8c..5d14aae601 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -532,6 +532,18 @@ class TestIO < Test::Unit::TestCase
end
if have_nonblock?
+ def test_copy_stream_no_busy_wait
+ IO.pipe do |r,w|
+ r.nonblock = true
+ assert_cpu_usage_low('r58534 [ruby-core:80969] [Backport #13533]') do
+ th = Thread.new { IO.copy_stream(r, IO::NULL) }
+ sleep 0.1
+ w.close
+ th.join
+ end
+ end
+ end
+
def test_copy_stream_pipe_nonblock
mkcdtmpdir {
with_read_pipe("abc") {|r1|