ruby-changes:36142
From: akr <ko1@a...>
Date: Sat, 1 Nov 2014 19:26:10 +0900 (JST)
Subject: [ruby-changes:36142] akr:r48223 (trunk): * test/ruby/envutil.rb (assert_join_threads): New assertion to
akr 2014-11-01 19:25:57 +0900 (Sat, 01 Nov 2014) New Revision: 48223 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48223 Log: * test/ruby/envutil.rb (assert_join_threads): New assertion to join multiple threads without exceptions. Modified files: trunk/ChangeLog trunk/test/ruby/envutil.rb trunk/test/ruby/test_io.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 48222) +++ ChangeLog (revision 48223) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Nov 1 19:24:59 2014 Tanaka Akira <akr@f...> + + * test/ruby/envutil.rb (assert_join_threads): New assertion to + join multiple threads without exceptions. + Sat Nov 1 17:09:32 2014 Tanaka Akira <akr@f...> * bignum.c (bary_mul_balance_with_mulfunc): Fix free work area Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 48222) +++ test/ruby/test_io.rb (revision 48223) @@ -584,11 +584,13 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L584 end t1 = Thread.new { w1 << megacontent; w1.close } t2 = Thread.new { r2.read } - ret = IO.copy_stream(r1, w2) - assert_equal(megacontent.bytesize, ret) - w2.close - t1.join - assert_equal(megacontent, t2.value) + t3 = Thread.new { + ret = IO.copy_stream(r1, w2) + assert_equal(megacontent.bytesize, ret) + w2.close + } + _, t2_value, _ = assert_join_threads([t1, t2, t3]) + assert_equal(megacontent, t2_value) } } } @@ -601,11 +603,13 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L603 with_pipe {|r2, w2| t1 = Thread.new { w1 << megacontent; w1.close } t2 = Thread.new { r2.read } - ret = IO.copy_stream(r1, w2) - assert_equal(megacontent.bytesize, ret) - w2.close - t1.join - assert_equal(megacontent, t2.value) + t3 = Thread.new { + ret = IO.copy_stream(r1, w2) + assert_equal(megacontent.bytesize, ret) + w2.close + } + _, t2_value, _ = assert_join_threads([t1, t2, t3]) + assert_equal(megacontent, t2_value) } } } @@ -614,11 +618,14 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L618 def test_copy_stream_megacontent_file_to_pipe with_megasrc {|megasrc, megacontent| with_pipe {|r, w| - t = Thread.new { r.read } - ret = IO.copy_stream(megasrc, w) - assert_equal(megacontent.bytesize, ret) - w.close - assert_equal(megacontent, t.value) + t1 = Thread.new { r.read } + t2 = Thread.new { + ret = IO.copy_stream(megasrc, w) + assert_equal(megacontent.bytesize, ret) + w.close + } + t1_value, _ = assert_join_threads([t1, t2]) + assert_equal(megacontent, t1_value) } } end @@ -666,11 +673,13 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L673 def test_copy_stream_socket2 with_bigsrc {|bigsrc, bigcontent| with_socketpair {|s1, s2| - t = Thread.new { s2.read } - ret = IO.copy_stream(bigsrc, s1) - assert_equal(bigcontent.bytesize, ret) - s1.close - result = t.value + t1 = Thread.new { s2.read } + t2 = Thread.new { + ret = IO.copy_stream(bigsrc, s1) + assert_equal(bigcontent.bytesize, ret) + s1.close + } + result, _ = assert_join_threads([t1, t2]) assert_equal(bigcontent, result) } } @@ -679,11 +688,13 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L688 def test_copy_stream_socket3 with_bigsrc {|bigsrc, bigcontent| with_socketpair {|s1, s2| - t = Thread.new { s2.read } - ret = IO.copy_stream(bigsrc, s1, 10000) - assert_equal(10000, ret) - s1.close - result = t.value + t1 = Thread.new { s2.read } + t2 = Thread.new { + ret = IO.copy_stream(bigsrc, s1, 10000) + assert_equal(10000, ret) + s1.close + } + result, _ = assert_join_threads([t1, t2]) assert_equal(bigcontent[0,10000], result) } } @@ -694,12 +705,14 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L705 File.open(bigsrc) {|f| assert_equal(0, f.pos) with_socketpair {|s1, s2| - t = Thread.new { s2.read } - ret = IO.copy_stream(f, s1, nil, 100) - assert_equal(bigcontent.bytesize-100, ret) - assert_equal(0, f.pos) - s1.close - result = t.value + t1 = Thread.new { s2.read } + t2 = Thread.new { + ret = IO.copy_stream(f, s1, nil, 100) + assert_equal(bigcontent.bytesize-100, ret) + assert_equal(0, f.pos) + s1.close + } + result, _ = assert_join_threads([t1, t2]) assert_equal(bigcontent[100..-1], result) } } @@ -712,12 +725,14 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L725 assert_equal(bigcontent[0,100], f.read(100)) assert_equal(100, f.pos) with_socketpair {|s1, s2| - t = Thread.new { s2.read } - ret = IO.copy_stream(f, s1) - assert_equal(bigcontent.bytesize-100, ret) - assert_equal(bigcontent.length, f.pos) - s1.close - result = t.value + t1 = Thread.new { s2.read } + t2 = Thread.new { + ret = IO.copy_stream(f, s1) + assert_equal(bigcontent.bytesize-100, ret) + assert_equal(bigcontent.length, f.pos) + s1.close + } + result, _ = assert_join_threads([t1, t2]) assert_equal(bigcontent[100..-1], result) } } @@ -735,11 +750,13 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L750 rescue Errno::EBADF skip "nonblocking IO for pipe is not implemented" end - t = Thread.new { s2.read } - ret = IO.copy_stream("megasrc", s1) - assert_equal(megacontent.bytesize, ret) - s1.close - result = t.value + t1 = Thread.new { s2.read } + t2 = Thread.new { + ret = IO.copy_stream("megasrc", s1) + assert_equal(megacontent.bytesize, ret) + s1.close + } + result, _ = assert_join_threads([t1, t2]) assert_equal(megacontent, result) } } @@ -967,11 +984,12 @@ class TestIO < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L984 w.write "zz" src = StringIO.new("abcd") IO.copy_stream(src, w) - t = Thread.new { + t1 = Thread.new { w.close } - assert_equal("zzabcd", r.read) - t.join + t2 = Thread.new { r.read } + _, result = assert_join_threads([t1, t2]) + assert_equal("zzabcd", result) } end @@ -2747,7 +2765,7 @@ End https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io.rb#L2765 threads << Thread.new {write_file.print(i)} threads << Thread.new {read_file.read} end - threads.each {|t| t.join} + assert_join_threads(threads) assert(true, "[ruby-core:37197]") ensure read_file.close Index: test/ruby/envutil.rb =================================================================== --- test/ruby/envutil.rb (revision 48222) +++ test/ruby/envutil.rb (revision 48223) @@ -520,6 +520,36 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/envutil.rb#L520 end end + # threads should respond to shift method. + # Array and Queue can be used. + def assert_join_threads(threads, message = nil) + errs = [] + values = [] + while th = threads.shift + begin + values << th.value + rescue Exception + errs << $! + end + end + if !errs.empty? + msg = errs.map {|err| + err.backtrace.map.with_index {|line, i| + if i == 0 + "#{line}: #{err.message} (#{err.class})" + else + "\tfrom #{line}" + end + }.join("\n") + }.join("\n---\n") + if message + msg = "#{message}\n#{msg}" + end + raise MiniTest::Assertion, msg + end + values + end + class << (AssertFile = Struct.new(:failure_message).new) include Assertions def assert_file_predicate(predicate, *args) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/