Skip to content

Commit

Permalink
Catch RequestsException Instead Of Connection Errors To Allow For Ret… (
Browse files Browse the repository at this point in the history
  • Loading branch information
rjduffner committed Sep 15, 2020
1 parent 4736d1c commit da233f7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
46 changes: 44 additions & 2 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from requests.exceptions import ConnectionError, ReadTimeout
from wukong.request import SolrRequest
from wukong.errors import *
import pytest
import json

try:
Expand Down Expand Up @@ -214,7 +213,7 @@ def test_request_request__malformed_response(self):
solr_error = cm.exception
self.assertEqual(str(solr_error), "Parsing Error: Malformed Response" )

def test_request_request__server_down(self):
def test_request_request__server_down_connection_error(self):

with mock.patch('requests.sessions.Session.request') as mock_request:
def request(*args, **kwargs):
Expand Down Expand Up @@ -256,3 +255,46 @@ def request(*args, **kwargs):

solr_error = cm.exception
self.assertEqual(str(solr_error), "Unable to fetch from any SOLR nodes" )

def test_request_request__server_down_read_timeout(self):

with mock.patch('requests.sessions.Session.request') as mock_request:
def request(*args, **kwargs):
raise ReadTimeout("Server down!")

mock_request.side_effect = request
with self.assertRaises(SolrError) as cm:
response = self.client.request(
'fake_path',
{"fake_params": "fake_value"},
'GET',
body={"fake_body": "fake_value"}
)

mock_request.assert_any_call(
'GET', 'http://localsolr:8080/solr/fake_path',
params={
"fake_params": "fake_value",
'wt': 'json',
'omitHeader': 'true',
'json.nl': 'map'
},
headers={'content-type': 'application/json'},
data={"fake_body": "fake_value"},
timeout=15
)
mock_request.assert_any_call(
'GET', 'http://localsolr:7070/solr/fake_path',
params={
"fake_params": "fake_value",
'wt': 'json',
'omitHeader': 'true',
'json.nl': 'map'
},
headers={'content-type': 'application/json'},
data={"fake_body": "fake_value"},
timeout=15
)

solr_error = cm.exception
self.assertEqual(str(solr_error), "Unable to fetch from any SOLR nodes" )
4 changes: 2 additions & 2 deletions wukong/request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from wukong.zookeeper import Zookeeper
from requests.exceptions import ConnectionError
from requests.exceptions import RequestException
from wukong.errors import SolrError
import requests
import random
Expand Down Expand Up @@ -151,7 +151,7 @@ def request(self, path, params, method, body=None, headers=None, is_retry=False)
)
response = None

except ConnectionError:
except RequestException:
response = None
logger.info(
'Failed to connect to SOLR',
Expand Down

0 comments on commit da233f7

Please sign in to comment.