Fix reconnect on keep alive timeout when allowing all connections. #43
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 removingrequire 'fakeweb'
.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 theconnect
andrequest
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.