Skip to content

Commit

Permalink
Proper handling of timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
heynemann committed Nov 16, 2013
1 parent 6cbef99 commit 6ccddf8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions octopus/core.py
Expand Up @@ -7,6 +7,7 @@

try:
import requests
import requests.exceptions
except ImportError:
print("Can't import requests. Probably setup.py installing package.")

Expand Down Expand Up @@ -100,6 +101,13 @@ def do_work(self):
body=str(err),
error=err
)
except requests.exceptions.Timeout:
err = sys.exc_info()[1]
response = ResponseError(
status_code=500,
body=str(err),
error=err
)

if self.cache:
self.response_cache.put(url, response)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_octopus.py
Expand Up @@ -181,3 +181,19 @@ def handle_url_response(url, response):
expect(self.response.body).to_include("HTTPConnectionPool(host='kagdjdkjgka.fk', port=80)")
expect(self.response.body).to_include('Max retries exceeded with url: /')
expect(self.response.error).to_be_instance_of(requests.exceptions.ConnectionError)

def test_can_handle_timeouts(self):
url = 'http://baidu.com'
otto = Octopus(concurrency=1, request_timeout_in_seconds=0.1)

def handle_url_response(url, response):
self.response = response

otto.enqueue(url, handle_url_response)

otto.start()

otto.wait(5)

expect(self.response.body).to_include('Connection to baidu.com timed out')
expect(self.response.error).to_be_instance_of(requests.exceptions.Timeout)

0 comments on commit 6ccddf8

Please sign in to comment.