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

ruby-changes:35787

From: normal <ko1@a...>
Date: Sat, 11 Oct 2014 04:16:12 +0900 (JST)
Subject: [ruby-changes:35787] normal:r47869 (trunk): open-uri: accept :open_timeout option

normal	2014-10-11 04:15:56 +0900 (Sat, 11 Oct 2014)

  New Revision: 47869

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47869

  Log:
    open-uri: accept :open_timeout option
    
    * lib/open-uri.rb (OpenURI::Options): add :open_timeout default
    * (def OpenURI.open_http): check :open_timeout option
    * (module OpenURI): rdoc for :open_timeout
    * test/open-uri/test_open-uri.rb (test_open_timeout): new test
      [Feature #10361]

  Modified files:
    trunk/ChangeLog
    trunk/lib/open-uri.rb
    trunk/test/open-uri/test_open-uri.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47868)
+++ ChangeLog	(revision 47869)
@@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Oct 11 04:14:41 2014  Kir Shatrov  <shatrov@m...>
+
+	* lib/open-uri.rb (OpenURI::Options): add :open_timeout default
+	* (def OpenURI.open_http): check :open_timeout option
+	* (module OpenURI): rdoc for :open_timeout
+	* test/open-uri/test_open-uri.rb (test_open_timeout): new test
+	  [Feature #10361]
+
 Fri Oct 10 11:27:49 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* include/ruby/io.h (rb_io_mode_flags, rb_io_modenum_flags):
Index: lib/open-uri.rb
===================================================================
--- lib/open-uri.rb	(revision 47868)
+++ lib/open-uri.rb	(revision 47869)
@@ -102,6 +102,7 @@ module OpenURI https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb#L102
     :content_length_proc => true,
     :http_basic_authentication => true,
     :read_timeout => true,
+    :open_timeout => true,
     :ssl_ca_cert => nil,
     :ssl_verify_mode => nil,
     :ftp_active_mode => false,
@@ -307,6 +308,9 @@ module OpenURI https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb#L308
     if options.include? :read_timeout
       http.read_timeout = options[:read_timeout]
     end
+    if options.include? :open_timeout
+      http.open_timeout = options[:open_timeout]
+    end
 
     resp = nil
     http.start {
@@ -667,6 +671,13 @@ module OpenURI https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb#L671
     #
     #  :read_timeout option specifies a timeout of read for http connections.
     #
+    # [:open_timeout]
+    #  Synopsis:
+    #    :open_timeout=>nil     (no timeout)
+    #    :open_timeout=>10      (10 second)
+    #
+    #  :open_timeout option specifies a timeout of open for http connections.
+    #
     # [:ssl_ca_cert]
     #  Synopsis:
     #    :ssl_ca_cert=>filename
Index: test/open-uri/test_open-uri.rb
===================================================================
--- test/open-uri/test_open-uri.rb	(revision 47868)
+++ test/open-uri/test_open-uri.rb	(revision 47869)
@@ -122,6 +122,24 @@ class TestOpenURI < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/open-uri/test_open-uri.rb#L122
     }
   end
 
+  def test_open_timeout
+    assert_raises(Net::OpenTimeout) do
+      URI("http://example.com/").read(open_timeout: 0.000001)
+    end if false # avoid external resources in tests
+
+    with_http {|srv, dr, url|
+      url += '/'
+      srv.mount_proc('/', lambda { |_, res| res.body = 'hi' })
+      begin
+        URI(url).read(open_timeout: 0.000001)
+      rescue Net::OpenTimeout
+        # not guaranteed to fire, since the kernel negotiates the
+        # TCP connection even if the server thread is sleeping
+      end
+      assert_equal 'hi', URI(url).read(open_timeout: 60), 'should not timeout'
+    }
+  end
+
   def test_invalid_option
     assert_raise(ArgumentError) { open("http://127.0.0.1/", :invalid_option=>true) {} }
   end

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

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