ruby-changes:61077
From: David <ko1@a...>
Date: Fri, 8 May 2020 07:39:44 +0900 (JST)
Subject: [ruby-changes:61077] 1ede0c15a1 (master): [rubygems/rubygems] Move stuff to the instance level
https://git.ruby-lang.org/ruby.git/commit/?id=1ede0c15a1 From 1ede0c15a1adda6ec523a7a3b0c58bcd0603cef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= <deivid.rodriguez@r...> Date: Fri, 10 Apr 2020 21:59:13 +0200 Subject: [rubygems/rubygems] Move stuff to the instance level I don't know why it was living at the class level. At first I thought it would be to reuse some server instances between tests, but they seem to be cleaned up after every test anyways, so I don't think any of it is needed. https://github.com/rubygems/rubygems/commit/04bee71216 diff --git a/test/rubygems/test_gem_remote_fetcher.rb b/test/rubygems/test_gem_remote_fetcher.rb index 2385772..0e12007 100644 --- a/test/rubygems/test_gem_remote_fetcher.rb +++ b/test/rubygems/test_gem_remote_fetcher.rb @@ -100,12 +100,12 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L100 @proxies.each {|k| ENV[k] = nil } super - self.class.start_servers - self.class.enable_yaml = true - self.class.enable_zip = false + start_servers + self.enable_yaml = true + self.enable_zip = false - base_server_uri = "http://localhost:#{self.class.normal_server_port}" - @proxy_uri = "http://localhost:#{self.class.proxy_server_port}" + base_server_uri = "http://localhost:#{normal_server_port}" + @proxy_uri = "http://localhost:#{proxy_server_port}" @server_uri = base_server_uri + "/yaml" @server_z_uri = base_server_uri + "/yaml.Z" @@ -129,7 +129,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L129 def teardown @fetcher.close_all - self.class.stop_servers + stop_servers super Gem.configuration[:http_proxy] = nil @proxies.each_with_index {|k, i| ENV[k] = @old_proxies[i] } @@ -881,7 +881,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L881 def test_yaml_error_on_size use_ui @stub_ui do - self.class.enable_yaml = false + self.enable_yaml = false fetcher = Gem::RemoteFetcher.new nil @fetcher = fetcher assert_error { fetcher.size } @@ -889,7 +889,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L889 end def test_ssl_connection - ssl_server = self.class.start_ssl_server + ssl_server = start_ssl_server temp_ca_cert = File.join(DIR, 'ca_cert.pem') with_configured_fetcher(":ssl_ca_cert: #{temp_ca_cert}") do |fetcher| fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/yaml") @@ -897,7 +897,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L897 end def test_ssl_client_cert_auth_connection - ssl_server = self.class.start_ssl_server({ + ssl_server = start_ssl_server({ :SSLVerifyClient => OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT}) @@ -912,7 +912,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L912 end def test_do_not_allow_invalid_client_cert_auth_connection - ssl_server = self.class.start_ssl_server({ + ssl_server = start_ssl_server({ :SSLVerifyClient => OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT}) @@ -929,7 +929,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L929 end def test_do_not_allow_insecure_ssl_connection_by_default - ssl_server = self.class.start_ssl_server + ssl_server = start_ssl_server with_configured_fetcher do |fetcher| assert_raises Gem::RemoteFetcher::FetchError do fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/yaml") @@ -938,14 +938,14 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L938 end def test_ssl_connection_allow_verify_none - ssl_server = self.class.start_ssl_server + ssl_server = start_ssl_server with_configured_fetcher(":ssl_verify_mode: 0") do |fetcher| fetcher.fetch_path("https://localhost:#{ssl_server.config[:Port]}/yaml") end end def test_do_not_follow_insecure_redirect - ssl_server = self.class.start_ssl_server + ssl_server = start_ssl_server temp_ca_cert = File.join(DIR, 'ca_cert.pem') expected_error_message = "redirecting to non-https resource: #{@server_uri} (https://localhost:#{ssl_server.config[:Port]}/insecure_redirect?to=#{@server_uri})" @@ -960,7 +960,7 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L960 end def test_nil_ca_cert - ssl_server = self.class.start_ssl_server + ssl_server = start_ssl_server temp_ca_cert = nil with_configured_fetcher(":ssl_ca_cert: #{temp_ca_cert}") do |fetcher| @@ -1012,148 +1012,144 @@ PeIQQkFng2VVot/WAQbv3ePqWq07g1BBcwIBAg== https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_remote_fetcher.rb#L1012 end - class << self + private - attr_reader :normal_server, :proxy_server - attr_accessor :enable_zip, :enable_yaml + attr_reader :normal_server, :proxy_server + attr_accessor :enable_zip, :enable_yaml - def start_servers - @normal_server ||= start_server(SERVER_DATA) - @proxy_server ||= start_server(PROXY_DATA) - @enable_yaml = true - @enable_zip = false + def start_servers + @normal_server ||= start_server(SERVER_DATA) + @proxy_server ||= start_server(PROXY_DATA) + @enable_yaml = true + @enable_zip = false + @ssl_server = nil + @ssl_server_thread = nil + end + + def stop_servers + if @normal_server + @normal_server.kill.join + @normal_server = nil + end + if @proxy_server + @proxy_server.kill.join + @proxy_server = nil + end + if @ssl_server + @ssl_server.stop @ssl_server = nil - @ssl_server_thread = nil end - - def stop_servers - if @normal_server - @normal_server.kill.join - @normal_server = nil - end - if @proxy_server - @proxy_server.kill.join - @proxy_server = nil - end - if @ssl_server - @ssl_server.stop - @ssl_server = nil - end - if @ssl_server_thread - @ssl_server_thread.kill.join - @ssl_server_thread = nil - end - utils = WEBrick::Utils # TimeoutHandler is since 1.9 - utils::TimeoutHandler.terminate if defined?(utils::TimeoutHandler.terminate) + if @ssl_server_thread + @ssl_server_thread.kill.join + @ssl_server_thread = nil end + utils = WEBrick::Utils # TimeoutHandler is since 1.9 + utils::TimeoutHandler.terminate if defined?(utils::TimeoutHandler.terminate) + end - def normal_server_port - @normal_server[:server].config[:Port] - end + def normal_server_port + @normal_server[:server].config[:Port] + end - def proxy_server_port - @proxy_server[:server].config[:Port] - end + def proxy_server_port + @proxy_server[:server].config[:Port] + end - DIR = File.expand_path(File.dirname(__FILE__)) + DIR = File.expand_path(File.dirname(__FILE__)) - def start_ssl_server(config = {}) - null_logger = NilLog.new - server = WEBrick::HTTPServer.new({ - :Port => 0, - :Logger => null_logger, - :AccessLog => [], - :SSLEnable => true, - :SSLCACertificateFile => File.join(DIR, 'ca_cert.pem'), - :SSLCertificate => cert('ssl_cert.pem'), - :SSLPrivateKey => key('ssl_key.pem'), - :SSLVerifyClient => nil, - :SSLCertName => nil - }.merge(config)) - server.mount_proc("/yaml") do |req, res| - res.body = "--- true\n" - end - server.mount_proc("/insecure_redirect") do |req, res| - res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, req.query['to']) + def start_ssl_server(config = {}) + null_logger = NilLog.new + server = WEBrick::HTTPServer.new({ + :Port => 0, + :Logger => null_logger, + :AccessLog => [], + :SSLEnable => true, + :SSLCACertificateFile => File.join(DIR, 'ca_cert.pem'), + :SSLCertificate => cert('ssl_cert.pem'), + :SSLPrivateKey => key('ssl_key.pem'), + :SSLVerifyClient => nil, + :SSLCertName => nil + }.merge(config)) + server.mount_proc("/yaml") do |req, res| + res.body = "--- true\n" + end + server.mount_proc("/insecure_redirect") do |req, res| + res.set_redirect(WEBrick::HTTPStatus::MovedPermanently, req.query['to']) + end + server.ssl_context.tmp_dh_callback = proc { TEST_KEY_DH2048 } + t = Thread.new do + begin + server.start + rescue Exception => ex + puts "ERROR during server thread: #{ex.message}" + raise + ensure + server.shutdown end - server.ssl_context.tmp_dh_callback = proc { TEST_KEY_DH2048 } - t = Thread.new do - begin - server.start - rescue Exception => ex - puts "ERROR during server thread: #{ex.message}" - raise - ensure - server.shutdown - end - end - while server.status != :Running - sleep 0.1 - unless t.alive? - t.join - raise - end - end - @ssl_server = server - @ssl_server_thread = t - server - end - - private - - def start_server(data) - null_logger = NilLog.new - s = WEBrick::HTTPServer.new( - :Port => 0, - :DocumentRoot => nil, - :Logger => null_logger, - :AccessLog => null_logger - ) - s.mount_proc("/kill") { |req, res| s.shutdown } - s.mo (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/