Skip to content

Commit

Permalink
Merge pull request #297 from gro-intelligence/jli-closedhttpclientfix
Browse files Browse the repository at this point in the history
fix: each GroClient has its own AsyncHTTPClient (CLEWS-32775)
  • Loading branch information
john li authored Dec 11, 2020
2 parents 158e5d9 + b816cc8 commit 3e763f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 12 additions & 0 deletions groclient/batch_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ def sum_results(inputIndex, inputObject, response, summation):

self.assertEqual(summation, 97680)

# Test that multiple GroClients each have their own AsyncHTTPClient. Note:
# this tests the fix for the `fetch called on closed AsyncHTTPClient`
# error. We can't test for that directly since the `fetch` call is mocked,
# so instead we just ensure that all GroClients have their own
# AsyncHTTPClient.
def test_batch_async_get_data_points_multiple_clients(self):
client = GroClient(MOCK_HOST, MOCK_TOKEN)
ahc_id1 = id(client._async_http_client)
client = GroClient(MOCK_HOST, MOCK_TOKEN)
ahc_id2 = id(client._async_http_client)
self.assertNotEqual(ahc_id1, ahc_id2)

def test_batch_async_get_data_points_bad_request_error(self):
responses = self.client.batch_async_get_data_points([mock_error_selection])
self.assertTrue(isinstance(responses[0], BatchError))
Expand Down
7 changes: 5 additions & 2 deletions groclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ def __init__(self, api_host, access_token):
self._data_series_queue = [] # added but not loaded in data frame
self._data_frame = pandas.DataFrame()
try:
self._async_http_client = AsyncHTTPClient()
# Each GroClient has its own IOLoop and AsyncHTTPClient.
self._ioloop = IOLoop()
# Note: force_instance is needed to disable Tornado's
# pseudo-singleton AsyncHTTPClient caching behavior.
self._async_http_client = AsyncHTTPClient(force_instance=True)
except Exception as e:
self._logger.warning(
"Unable to initialize an event loop. Async methods disabled."
"Unable to initialize event loop, async methods disabled: {}".format(e)
)
self._async_http_client = None
self._ioloop = None
Expand Down

0 comments on commit 3e763f1

Please sign in to comment.