Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not call blocking content property and lazily load response #2643

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ class ResponseContextManager(FastResponse):
def __init__(self, response, environment, request_meta):
# copy data from response to this object
self.__dict__ = response.__dict__
self._cached_content = response.content
try:
self._cached_content = response._cached_content
except AttributeError:
pass
neiser marked this conversation as resolved.
Show resolved Hide resolved
# store reference to locust Environment
self._environment = environment
self.request_meta = request_meta
Expand Down
22 changes: 22 additions & 0 deletions locust/test/test_fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ def test_streaming_response(self):
# download the content of the streaming response (so we don't get an ugly exception in the log)
_ = r.content

def test_streaming_response_catch_response(self):
"""
Test a request to an endpoint that returns a streaming response, and uses catch_response
"""
s = self.get_client()

with s.get("/streaming/30", stream=True, catch_response=True) as r:
# typical usage of r when stream=True is to read the stream as desired,
# with the possibility to "fail fast" when some things are read early on
response_content = str(r.stream.read())
r.failure("some error")

self.assertRegex(response_content, "streaming response")

stats = self.runner.stats.get("/streaming/30", "GET")
self.assertEqual(1, stats.num_requests)
self.assertEqual(1, stats.num_failures)

# verify that response time does NOT include whole download time, when using stream=True
self.assertGreaterEqual(stats.avg_response_time, 0)
self.assertLess(stats.avg_response_time, 250)

def test_slow_redirect(self):
s = self.get_client()
url = "/redirect?url=/redirect&delay=0.5"
Expand Down
Loading