Permalink
Browse files

Refactorings following #28

Refactored all startup activity into #start

Refactored socket_options tests to test_start
  • Loading branch information...
1 parent 07b4092 commit 13b89f5b2bec3dd086a716fde18f876329047579 @drbrain committed Oct 17, 2012
Showing with 36 additions and 24 deletions.
  1. +1 −1 History.txt
  2. +5 −7 lib/net/http/persistent.rb
  3. +30 −16 test/test_net_http_persistent.rb
View
@@ -10,7 +10,7 @@
* Bug fixes
* Socket options are set again following connection reset. Pull request #28
by cmaion.
- * #shutdown now works even if no connections were made. Pull Requset #24 by
+ * #shutdown now works even if no connections were made. Pull Request #24 by
James Tucker.
=== 2.7 / 2012-06-06
View
@@ -561,13 +561,7 @@ def connection_for uri
reset connection if expired? connection
end
- unless connection.started? then
- connection.set_debug_output @debug_output if @debug_output
- connection.open_timeout = @open_timeout if @open_timeout
- connection.read_timeout = @read_timeout if @read_timeout
-
- start connection
- end
+ start connection unless connection.started?
connection
rescue Errno::ECONNREFUSED
@@ -614,6 +608,10 @@ def expired? connection
# Starts the Net::HTTP +connection+
def start connection
+ connection.set_debug_output @debug_output if @debug_output
+ connection.open_timeout = @open_timeout if @open_timeout
+ connection.read_timeout = @read_timeout if @read_timeout
+
connection.start
socket = connection.instance_variable_get :@socket
@@ -61,8 +61,9 @@ def teardown
end
class BasicConnection
- attr_accessor :started, :finished, :address, :port
- attr_reader :req
+ attr_accessor :started, :finished, :address, :port,
+ :read_timeout, :open_timeout
+ attr_reader :req, :debug_output
def initialize
@started, @finished = 0, 0
@address, @port = 'example.com', 80
@@ -80,6 +81,9 @@ def pipeline requests, &block
def reset?
@started == @finished + 1
end
+ def set_debug_output io
+ @debug_output = io
+ end
def start
@started += 1
io = Object.new
@@ -433,20 +437,6 @@ def cached.started?; false end
assert_match %r%connection refused%, e.message
end
- def test_connection_for_socket_options
- @http.socket_options << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1]
- c = @http.connection_for @uri
-
- socket = c.instance_variable_get :@socket
-
- expected = []
- expected << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if
- Socket.const_defined? :TCP_NODELAY
- expected << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1]
-
- assert_equal expected, socket.io.instance_variable_get(:@setsockopts)
- end
-
def test_connection_for_ssl
uri = URI.parse 'https://example.com/path'
c = @http.connection_for uri
@@ -1310,6 +1300,30 @@ def test_ssl_version_equals
assert_equal 1, @http.ssl_generation
end if RUBY_VERSION > '1.9'
+ def test_start
+ c = basic_connection
+
+ @http.socket_options << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1]
+ @http.debug_output = $stderr
+ @http.read_timeout = 5
+ @http.open_timeout = 6
+
+ @http.start c
+
+ assert_equal $stderr, c.debug_output
+ assert_equal 5, c.read_timeout
+ assert_equal 6, c.open_timeout
+
+ socket = c.instance_variable_get :@socket
+
+ expected = []
+ expected << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if
+ Socket.const_defined? :TCP_NODELAY
+ expected << [Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1]
+
+ assert_equal expected, socket.io.instance_variable_get(:@setsockopts)
+ end
+
def test_verify_callback_equals
@http.verify_callback = :verify_callback

0 comments on commit 13b89f5

Please sign in to comment.