Skip to content

Commit

Permalink
Do not call blocking content property and lazily load response on fir…
Browse files Browse the repository at this point in the history
…st access

This reverts commit 7ce89a0 and improves it
  • Loading branch information
neiser committed Mar 20, 2024
1 parent c69316d commit 4ffa84a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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
# 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

0 comments on commit 4ffa84a

Please sign in to comment.