Skip to content

Commit

Permalink
Throw an exception if someone tries to call response.failure()/succes…
Browse files Browse the repository at this point in the history
…s() without using a with block (helps with some misunderstandings, e.g. #1953)
  • Loading branch information
cyberw committed Dec 8, 2021
1 parent c79533d commit ceb2cf2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion locust/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from urllib.parse import urlparse, urlunparse

from .exception import CatchResponseError, ResponseError
from .exception import CatchResponseError, LocustError, ResponseError

absolute_http_url_regexp = re.compile(r"^https?://", re.I)

Expand Down Expand Up @@ -216,6 +216,7 @@ class ResponseContextManager(LocustResponse):
"""

_manual_result = None
_entered = False

def __init__(self, response, request_event, request_meta):
# copy data from response to this object
Expand All @@ -224,6 +225,7 @@ def __init__(self, response, request_event, request_meta):
self.request_meta = request_meta

def __enter__(self):
self._entered = True
return self

def __exit__(self, exc, value, traceback):
Expand Down Expand Up @@ -267,6 +269,10 @@ def success(self):
if response.status_code == 404:
response.success()
"""
if not self._entered:
raise LocustError(
"Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n response.success()"
)
self._manual_result = True

def failure(self, exc):
Expand All @@ -282,6 +288,10 @@ def failure(self, exc):
if response.content == b"":
response.failure("No data")
"""
if not self._entered:
raise LocustError(
"Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n response.failure(...)"
)
if not isinstance(exc, Exception):
exc = CatchResponseError(exc)
self._manual_result = exc
10 changes: 10 additions & 0 deletions locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ class ResponseContextManager(FastResponse):
"""

_manual_result = None
_entered = False

def __init__(self, response, environment, request_meta):
# copy data from response to this object
Expand All @@ -423,6 +424,7 @@ def __init__(self, response, environment, request_meta):
self.request_meta = request_meta

def __enter__(self):
self._entered = True
return self

def __exit__(self, exc, value, traceback):
Expand Down Expand Up @@ -465,6 +467,10 @@ def success(self):
if response.status_code == 404:
response.success()
"""
if not self._entered:
raise LocustError(
"Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n response.success()"
)
self._manual_result = True

def failure(self, exc):
Expand All @@ -480,6 +486,10 @@ def failure(self, exc):
if response.content == "":
response.failure("No data")
"""
if not self._entered:
raise LocustError(
"Tried to set status on a request that has not yet been made. Make sure you use a with-block, like this:\n\nwith self.client.request(..., catch_response=True) as response:\n response.failure(...)"
)
if not isinstance(exc, Exception):
exc = CatchResponseError(exc)
self._manual_result = exc

0 comments on commit ceb2cf2

Please sign in to comment.