Skip to content

Commit

Permalink
Fixes issue with urllib3 request_timeout param
Browse files Browse the repository at this point in the history
Closes elastic#1044. Fixing bug with mark_dead in connection pool
  • Loading branch information
Alex Kahan committed Oct 29, 2019
1 parent a90345c commit c79acc8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
4 changes: 4 additions & 0 deletions elasticsearch/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ def perform_request(self, method, url, headers=None, params=None, body=None):
timeout = None
if params:
timeout = params.pop("request_timeout", None)
try:
timeout = float(timeout)
except (TypeError, ValueError):
timeout = None
ignore = params.pop("ignore", ())
if isinstance(ignore, int):
ignore = (ignore,)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
long_description = f.read().strip()
f.close()

install_requires = ["urllib3>=1.21.1"]
install_requires = ["urllib3==1.21.1"]
tests_require = [
"requests>=2.0.0, <3.0.0",
"nose",
Expand Down
3 changes: 1 addition & 2 deletions test_elasticsearch/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def test_connection_is_skipped_when_dead(self):
def test_new_connection_is_not_marked_dead(self):
# Create 10 connections
pool = ConnectionPool([(Connection(), {}) for _ in range(10)])

# Pass in a new connection that is not in the pool, to mark as dead
# Pass in a new connection that is not in the pool to mark as dead
new_connection = Connection()
pool.mark_dead(new_connection)

Expand Down
37 changes: 26 additions & 11 deletions test_elasticsearch/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,32 @@ def test_single_connection_uses_dummy_connection_pool(self):
def test_request_timeout_extracted_from_params_and_passed(self):
t = Transport([{}], connection_class=DummyConnection)

t.perform_request("GET", "/", params={"request_timeout": 42})
self.assertEquals(1, len(t.get_connection().calls))
self.assertEquals(("GET", "/", {}, None), t.get_connection().calls[0][0])
self.assertEquals(
{
"timeout": 42,
"ignore": (),
"headers": None,
},
t.get_connection().calls[0][1],
)
# Test cases for the `reuest_timeout` parameter. In the form of input -> expected output.
# Int and Float values will be converted to a float, all others will be converted to None.
test_cases = {
42: 42,
'42': 42,
'42.0': 42,
'42.00000': 42,
'42.1': 42.1,
'not a float or int': None,
None: None,
}

call_count = 0
for timeout, expected_timeout in test_cases.iteritems():
t.perform_request("GET", "/", params={"request_timeout": timeout})
self.assertEquals(
{
"timeout": expected_timeout,
"ignore": (),
"headers": None,
},
t.get_connection().calls[call_count][1],
)
self.assertEquals(("GET", "/", {}, None), t.get_connection().calls[call_count][0])
call_count += 1
self.assertEquals(call_count, len(t.get_connection().calls))

def test_request_with_custom_user_agent_header(self):
t = Transport([{}], connection_class=DummyConnection)
Expand Down

0 comments on commit c79acc8

Please sign in to comment.