fakeweb icon indicating copy to clipboard operation
fakeweb copied to clipboard

Fix reconnect on keep alive timeout when allowing all connections.

Open dylanahsmith opened this issue 11 years ago • 2 comments

Problem

Ruby 2.0 added a keep_alive_timeout variable to Net::HTTP for persistent. If keep_alive_timeout seconds has passed between requests, then Net::HTTP will close the socket and call connect. However, fakeweb replaces the connect method to have it do nothing (delaying it until request is called), so the socket is closed without reconnecting on a keep_alive_timeout.

The following script will result in a closed stream (IOError) exception being raised, but will run without exceptions after removing require 'fakeweb'.

require 'net/http'
require 'fakeweb'

uri = URI("http://www.google.com/")
req = Net::HTTP::Get.new(uri)

Net::HTTP.new(uri.host, uri.port).start do |http|
  http.request(req)
  sleep 2  # default keep_alive_timeout
  http.request(req)
end

fakeweb sets FakeWeb.allow_net_connect = true on initialization. In this state, we should avoid changing the behaviour of Net::HTTP so that it works in cases that fakeweb wasn't designed for.

Solution

Add FakeWeb.allow_all_connections? which returns true if fakeweb isn't intercepting any any requests (i.e. FakeWeb.allow_net_connect = true). In this state, the Net::HTTP monkey patch returns the behaviour of the connect and request method by just calling the real methods.

Remaining Problem

fakeweb will still have this problem for persistent http connections when passthrough uri's are registered, but I didn't want to complicate this pull request by trying to solve this use case. I wanted to start by making sure fakeweb isn't disruptive when it is required but isn't being used, or when it is disabled for certain tests.

Update: created pull #44 to address this problem.

dylanahsmith avatar Jan 09 '14 00:01 dylanahsmith

Any update on this ?

salimane avatar Mar 25 '15 14:03 salimane

Any update on this?

dcosson avatar Jun 01 '16 19:06 dcosson