Skip to content

Commit 4ffa84a

Browse files
committed
Do not call blocking content property and lazily load response on first access
This reverts commit 7ce89a0 and improves it
1 parent c69316d commit 4ffa84a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Diff for: locust/contrib/fasthttp.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,10 @@ class ResponseContextManager(FastResponse):
578578
def __init__(self, response, environment, request_meta):
579579
# copy data from response to this object
580580
self.__dict__ = response.__dict__
581-
self._cached_content = response.content
581+
try:
582+
self._cached_content = response._cached_content
583+
except AttributeError:
584+
pass
582585
# store reference to locust Environment
583586
self._environment = environment
584587
self.request_meta = request_meta

Diff for: locust/test/test_fasthttp.py

+22
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ def test_streaming_response(self):
7171
# download the content of the streaming response (so we don't get an ugly exception in the log)
7272
_ = r.content
7373

74+
def test_streaming_response_catch_response(self):
75+
"""
76+
Test a request to an endpoint that returns a streaming response, and uses catch_response
77+
"""
78+
s = self.get_client()
79+
80+
with s.get("/streaming/30", stream=True, catch_response=True) as r:
81+
# typical usage of r when stream=True is to read the stream as desired,
82+
# with the possibility to "fail fast" when some things are read early on
83+
response_content = str(r.stream.read())
84+
r.failure("some error")
85+
86+
self.assertRegex(response_content, "streaming response")
87+
88+
stats = self.runner.stats.get("/streaming/30", "GET")
89+
self.assertEqual(1, stats.num_requests)
90+
self.assertEqual(1, stats.num_failures)
91+
92+
# verify that response time does NOT include whole download time, when using stream=True
93+
self.assertGreaterEqual(stats.avg_response_time, 0)
94+
self.assertLess(stats.avg_response_time, 250)
95+
7496
def test_slow_redirect(self):
7597
s = self.get_client()
7698
url = "/redirect?url=/redirect&delay=0.5"

0 commit comments

Comments
 (0)