You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're attempting to mock HTTP responses requested from within a Python thread pool. Is that supported? The following example suggest it does not work, unfortunately:
importconcurrent.futuresimporthttprettyas_httprettyimportpytestimportrequests@pytest.fixture(scope="session")defhttpretty():
_httpretty.enable(verbose=True, allow_net_connect=False)
try:
yield_httprettyfinally:
_httpretty.disable()
_httpretty.reset()
@pytest.fixturedefmock_response(httpretty):
defrequest_callback(request, uri, response_headers):
return200, response_headers, request.body# Pass through the requested bodyhttpretty.register_uri(method="POST", uri="https://my-server.com/", body=request_callback)
@pytest.fixturedefexample_payloads():
return [0, 1, 3]
defpost(payload):
r=requests.post("https://my-server.com/", json=payload)
returnr.json()
deftest_simple_map_passes(mock_response, example_payloads):
results=map(post, example_payloads)
assertlist(results) ==example_payloadsdeftest_threadpool_map_fails(mock_response, example_payloads):
executor=concurrent.futures.ThreadPoolExecutor()
results=executor.map(post, example_payloads)
# Responses appear to be randomly drawn (with duplicates) from the expected listassertlist(results) ==example_payloads
Interestingly, occasionally, the response body appears to be the full string like this:
We're attempting to mock HTTP responses requested from within a Python thread pool. Is that supported? The following example suggest it does not work, unfortunately:
Interestingly, occasionally, the response body appears to be the full string like this:
which is really not expected at all since it should just contain the JSON data, e.g.
0
,1
.I believe this works with Python 3.8 and 3.9, but fails from Python 3.10 onwards.
The text was updated successfully, but these errors were encountered: