ruby-changes:53033
From: kazu <ko1@a...>
Date: Sat, 20 Oct 2018 22:14:07 +0900 (JST)
Subject: [ruby-changes:53033] kazu:r65247 (trunk): Run background threads while testing drb
kazu 2018-10-20 22:14:02 +0900 (Sat, 20 Oct 2018) New Revision: 65247 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65247 Log: Run background threads while testing drb Do not start background thread on load test/drb/drbtest.rb, and stop threads on each test. Modified files: trunk/test/drb/drbtest.rb trunk/test/drb/test_drb.rb trunk/test/drb/test_drbssl.rb trunk/test/drb/test_drbunix.rb Index: test/drb/test_drbssl.rb =================================================================== --- test/drb/test_drbssl.rb (revision 65246) +++ test/drb/test_drbssl.rb (revision 65247) @@ -15,37 +15,35 @@ class DRbSSLService < DRbService https://github.com/ruby/ruby/blob/trunk/test/drb/test_drbssl.rb#L15 %w(ut_drb_drbssl.rb ut_array_drbssl.rb).each do |nm| add_service_command(nm) end - config = Hash.new - config[:SSLVerifyMode] = OpenSSL::SSL::VERIFY_PEER - config[:SSLVerifyCallback] = lambda{ |ok,x509_store| - true - } - begin - data = open("sample.key"){|io| io.read } - config[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(data) - data = open("sample.crt"){|io| io.read } - config[:SSLCertificate] = OpenSSL::X509::Certificate.new(data) - rescue - # $stderr.puts "Switching to use self-signed certificate" - config[:SSLCertName] = - [ ["C","JP"], ["O","Foo.DRuby.Org"], ["CN", "Sample"] ] - end + def start + config = Hash.new + + config[:SSLVerifyMode] = OpenSSL::SSL::VERIFY_PEER + config[:SSLVerifyCallback] = lambda{ |ok,x509_store| + true + } + begin + data = open("sample.key"){|io| io.read } + config[:SSLPrivateKey] = OpenSSL::PKey::RSA.new(data) + data = open("sample.crt"){|io| io.read } + config[:SSLCertificate] = OpenSSL::X509::Certificate.new(data) + rescue + # $stderr.puts "Switching to use self-signed certificate" + config[:SSLCertName] = + [ ["C","JP"], ["O","Foo.DRuby.Org"], ["CN", "Sample"] ] + end - uri = ARGV.shift if $0 == __FILE__ - @server = DRb::DRbServer.new(uri || 'drbssl://:0', self.manager, config) + @server = DRb::DRbServer.new('drbssl://:0', manager, config) + end end class TestDRbSSLCore < Test::Unit::TestCase include DRbCore def setup - setup_service 'ut_drb_drbssl.rb' - super - end - - def teardown + @drb_service = DRbSSLService.new super - DRbService.finish + setup_service 'ut_drb_drbssl.rb' end def test_02_unknown @@ -61,13 +59,9 @@ end https://github.com/ruby/ruby/blob/trunk/test/drb/test_drbssl.rb#L59 class TestDRbSSLAry < Test::Unit::TestCase include DRbAry def setup - setup_service 'ut_array_drbssl.rb' - super - end - - def teardown + @drb_service = DRbSSLService.new super - DRbService.finish + setup_service 'ut_array_drbssl.rb' end end Index: test/drb/test_drb.rb =================================================================== --- test/drb/test_drb.rb (revision 65246) +++ test/drb/test_drb.rb (revision 65247) @@ -7,29 +7,14 @@ class TestDRbCore < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L7 include DRbCore def setup - setup_service 'ut_drb.rb' - super - end - - def teardown super - DRbService.finish + setup_service 'ut_drb.rb' end end -class TestDRbYield < Test::Unit::TestCase +module DRbYield include DRbBase - def setup - setup_service 'ut_drb.rb' - super - end - - def teardown - super - DRbService.finish - end - def test_01_one @there.echo_yield_1([]) {|one| assert_equal([], one) @@ -129,7 +114,23 @@ class TestDRbYield < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L114 end end -class TestDRbRubyYield < TestDRbYield +class TestDRbYield < Test::Unit::TestCase + include DRbYield + + def setup + super + setup_service 'ut_drb.rb' + end +end + +class TestDRbRubyYield < Test::Unit::TestCase + include DRbYield + + def setup + @there = self + super + end + def echo_yield(*arg) yield(*arg) end @@ -153,15 +154,11 @@ class TestDRbRubyYield < TestDRbYield https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L154 end end - def setup - @there = self - end - - def teardown - end end -class TestDRbRuby18Yield < TestDRbRubyYield +class TestDRbRuby18Yield < Test::Unit::TestCase + include DRbYield + class YieldTest18 def echo_yield(*arg, &proc) proc.call(*arg) @@ -188,6 +185,7 @@ class TestDRbRuby18Yield < TestDRbRubyYi https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L185 def setup @there = YieldTest18.new + super end end @@ -195,13 +193,8 @@ class TestDRbAry < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L193 include DRbAry def setup - setup_service 'ut_array.rb' - super - end - - def teardown super - DRbService.finish + setup_service 'ut_array.rb' end end @@ -209,8 +202,8 @@ class TestDRbMServer < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L202 include DRbBase def setup - setup_service 'ut_drb.rb' super + setup_service 'ut_drb.rb' @server = (1..3).collect do |n| DRb::DRbServer.new("druby://localhost:0", Onecky.new(n.to_s)) end @@ -221,7 +214,6 @@ class TestDRbMServer < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L214 s.stop_service end super - DRbService.finish end def test_01 @@ -229,14 +221,11 @@ class TestDRbMServer < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L221 end end -class TestDRbSafe1 < TestDRbAry +class TestDRbSafe1 < Test::Unit::TestCase + include DRbAry def setup - setup_service 'ut_safe1.rb' - end - - def teardown super - DRbService.finish + setup_service 'ut_safe1.rb' end end @@ -244,13 +233,8 @@ class TestDRbLarge < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L233 include DRbBase def setup - setup_service 'ut_large.rb' - super - end - - def teardown super - DRbService.finish + setup_service 'ut_large.rb' end def test_01_large_ary @@ -333,13 +317,8 @@ class TestBug4409 < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L317 include DRbBase def setup - setup_service 'ut_eq.rb' - super - end - - def teardown super - DRbService.finish + setup_service 'ut_eq.rb' end def test_bug4409 Index: test/drb/drbtest.rb =================================================================== --- test/drb/drbtest.rb (revision 65246) +++ test/drb/drbtest.rb (revision 65247) @@ -7,7 +7,6 @@ require 'timeout' https://github.com/ruby/ruby/blob/trunk/test/drb/drbtest.rb#L7 module DRbTests class DRbService - @@manager = DRb::ExtServManager.new @@ruby = [EnvUtil.rubybin] @@ruby << "-d" if $DEBUG def self.add_service_command(nm) @@ -18,21 +17,31 @@ class DRbService https://github.com/ruby/ruby/blob/trunk/test/drb/drbtest.rb#L17 %w(ut_drb.rb ut_array.rb ut_port.rb ut_large.rb ut_safe1.rb ut_eq.rb).each do |nm| add_service_command(nm) end - @server = @@server = DRb::DRbServer.new('druby://localhost:0', @@manager, {}) - @@manager.uri = @@server.uri - def self.manager - @@manager + + def initialize + @manager = DRb::ExtServManager.new + start + @manager.uri = server.uri end - def self.server - @server || @@server + + def start + @server = DRb::DRbServer.new('druby://localhost:0', manager, {}) end - def self.ext_service(name) + + attr_reader :manager + attr_reader :server + + def ext_service(name) Timeout.timeout(100, RuntimeError) do manager.service(name) end end - def self.finish - @server.instance_variable_get(:@grp).list.each {|th| th.join } + + def finish + server.instance_variable_get(:@grp).list.each {|th| th.join } + server.stop_service + manager.instance_variable_get(:@queue)&.push(nil) + manager.instance_variable_get(:@thread)&.join end end @@ -68,35 +77,42 @@ class XArray < Array https://github.com/ruby/ruby/blob/trunk/test/drb/drbtest.rb#L77 end module DRbBase + def setup + @drb_service ||= DRbService.new + end + def setup_service(service_name) @service_name = service_name - @ext = DRbService.ext_service(@service_name) + @ext = @drb_service.ext_service(@service_name) @there = @ext.front end def teardown @ext.stop_service if defined?(@ext) && @ext - DRbService.manager.unregist(@service_name) - while (@there&&@there.to_s rescue nil) - # nop - end - signal = /mswin|mingw/ =~ RUBY_PLATFORM ? :KILL : :TERM - Thread.list.each {|th| - if th.respond_to?(:pid) && th[:drb_service] == @service_name - 10.times do - begin - Process.kill signal, th.pid - break - rescue Errno::ESRCH - break - rescue Errno::EPERM # on Windows - sleep 0.1 - retry + if defined?(@service_name) && @service_name + @drb_service.manager.unregist(@service_name) + while (@there&&@there.to_s rescue nil) + # nop + end + signal = /mswin|mingw/ =~ RUBY_PLATFORM ? :KILL : :TERM + Thread.list.each {|th| + if th.respond_to?(:pid) && th[:drb_service] == @service_name + 10.times do + begin + Process.kill signal, th.pid + break + rescue Errno::ESRCH + break + rescue Errno::EPERM # on Windows + sleep 0.1 + retry + end end + th.join end - th.join - end - } + } + end + @drb_service.finish end end Index: test/drb/test_drbunix.rb =================================================================== --- test/drb/test_drbunix.rb (revision 65246) +++ test/drb/test_drbunix.rb (revision 65247) @@ -16,20 +16,17 @@ class DRbUNIXService < DRbService https://github.com/ruby/ruby/blob/trunk/test/drb/test_drbunix.rb#L16 add_service_command(nm) end - uri = ARGV.shift if $0 == __FILE__ - @server = DRb::DRbServer.new(uri || 'drbunix:', self.manager, {}) + def start + @server = DRb::DRbServer.new('drbunix:', manager, {}) + end end class TestDRbUNIXCore < Test::Unit::TestCase include DRbCore def setup - setup_service 'ut_drb_drbunix.rb' - super - end - - def teardown + @drb_service = DRbUNIXService.new super - DRbService.finish + setup_service 'ut_drb_drbunix.rb' end def test_02_unknown @@ -51,12 +48,9 @@ end https://github.com/ruby/ruby/blob/trunk/test/drb/test_drbunix.rb#L48 class TestDRbUNIXAry < Test::Unit::TestCase include DRbAry def setup - setup_service 'ut_array_drbunix.rb' + @drb_service = DRbUNIXService.new super - end - def teardown - super - DRbService.finish + setup_service 'ut_array_drbunix.rb' end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/