[前][次][番号順一覧][スレッド一覧]

ruby-changes:61024

From: Nobuyoshi <ko1@a...>
Date: Wed, 6 May 2020 16:07:50 +0900 (JST)
Subject: [ruby-changes:61024] 46b93175ed (master): Fakes IPSocket.getaddress in the whole method

https://git.ruby-lang.org/ruby.git/commit/?id=46b93175ed

From 46b93175ed9fe061f309fe5538f23dc13aa1de0b Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 6 May 2020 16:06:25 +0900
Subject: Fakes IPSocket.getaddress in the whole method

To get rid of calling `getaddrinfo`, which may keep FDs
internally.

diff --git a/test/uri/test_generic.rb b/test/uri/test_generic.rb
index 90f779c..b449a0a 100644
--- a/test/uri/test_generic.rb
+++ b/test/uri/test_generic.rb
@@ -865,34 +865,30 @@ class URI::TestGeneric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/uri/test_generic.rb#L865
   end
 
   def test_find_proxy_no_proxy
+    getaddress = IPSocket.method(:getaddress)
+    example_address = nil
+    IPSocket.singleton_class.class_eval do
+      undef getaddress
+      define_method(:getaddress) do |host|
+        case host
+        when "example.org", "www.example.org"
+          example_address
+        when /\A\d+(?:\.\d+){3}\z/
+          host
+        else
+          raise host
+        end
+      end
+    end
+
     with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'192.0.2.2') {|env|
       assert_equal(URI('http://127.0.0.1:8080'), URI("http://192.0.2.1/").find_proxy(env))
       assert_nil(URI("http://192.0.2.2/").find_proxy(env))
 
-      getaddress = IPSocket.method(:getaddress)
-      begin
-        class << IPSocket
-          undef getaddress
-          def getaddress(host)
-            host == "example.org" or raise
-            "192.0.2.1"
-          end
-        end
-        assert_equal(URI('http://127.0.0.1:8080'), URI.parse("http://example.org").find_proxy(env))
-        class << IPSocket
-          undef getaddress
-          def getaddress(host)
-            host == "example.org" or raise
-            "192.0.2.2"
-          end
-        end
-        assert_nil(URI.parse("http://example.org").find_proxy(env))
-      ensure
-        IPSocket.singleton_class.class_eval do
-          undef getaddress
-          define_method(:getaddress, getaddress)
-        end
-      end
+      example_address = "192.0.2.1"
+      assert_equal(URI('http://127.0.0.1:8080'), URI.parse("http://example.org").find_proxy(env))
+      example_address = "192.0.2.2"
+      assert_nil(URI.parse("http://example.org").find_proxy(env))
     }
     with_proxy_env('http_proxy'=>'http://127.0.0.1:8080', 'no_proxy'=>'example.org') {|env|
       assert_nil(URI("http://example.org/").find_proxy(env))
@@ -902,6 +898,11 @@ class URI::TestGeneric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/uri/test_generic.rb#L898
       assert_equal(URI('http://127.0.0.1:8080'), URI("http://example.org/").find_proxy(env))
       assert_nil(URI("http://www.example.org/").find_proxy(env))
     }
+  ensure
+    IPSocket.singleton_class.class_eval do
+      undef getaddress
+      define_method(:getaddress, getaddress)
+    end
   end
 
   def test_find_proxy_no_proxy_cidr
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]