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

Remove request_success and request_failure event handlers #2306

Merged
merged 1 commit into from
Feb 27, 2023
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
55 changes: 1 addition & 54 deletions locust/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,6 @@ class Events:
:param exception: Exception instance that was thrown. None if request was successful.
"""

request_success: DeprecatedEventHook
"""
DEPRECATED. Fired when a request is completed successfully. This event is typically used to report requests
when writing custom clients for locust.

Event arguments:

:param request_type: Request type method used
:param name: Path to the URL that was called (or override name if it was used in the call to the client)
:param response_time: Response time in milliseconds
:param response_length: Content-length of the response
"""

request_failure: DeprecatedEventHook
"""
DEPRECATED. Fired when a request fails. This event is typically used to report failed requests when writing
custom clients for locust.

Event arguments:

:param request_type: Request type method used
:param name: Path to the URL that was called (or override name if it was used in the call to the client)
:param response_time: Time in milliseconds until exception was thrown
:param response_length: Content-length of the response
:param exception: Exception instance that was thrown
"""

user_error: EventHook
"""
Fired when an exception occurs inside the execution of a User class.
Expand Down Expand Up @@ -169,8 +142,7 @@ class Events:
"""
Fired when Locust is started, once the Environment instance and locust runner instance
have been created. This hook can be used by end-users' code to run code that requires access to
the Environment. For example to register listeners to request_success, request_failure
or other events.
the Environment. For example to register listeners to other events.

Event arguments:

Expand Down Expand Up @@ -221,28 +193,3 @@ def __init__(self):
for name, value in self.__annotations__.items():
if value == EventHook:
setattr(self, name, value())

self.request_success = DeprecatedEventHook("request_success event deprecated. Use the request event.")

self.request_failure = DeprecatedEventHook("request_failure event deprecated. Use the request event.")

def fire_deprecated_request_handlers(
request_type, name, response_time, response_length, exception, context, **kwargs
):
if exception:
self.request_failure.fire(
request_type=request_type,
name=name,
response_time=response_time,
response_length=response_length,
exception=exception,
)
else:
self.request_success.fire(
request_type=request_type,
name=name,
response_time=response_time,
response_length=response_length,
)

self.request.add_listener(fire_deprecated_request_handlers)
15 changes: 4 additions & 11 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,12 @@ def __init__(self, environment: "Environment") -> None:
self._users_dispatcher: Optional[UsersDispatcher] = None

# set up event listeners for recording requests
def on_request_success(request_type, name, response_time, response_length, **_kwargs):
def on_request(request_type, name, response_time, response_length, exception=None, **_kwargs):
self.stats.log_request(request_type, name, response_time, response_length)
if exception:
self.stats.log_error(request_type, name, exception)

def on_request_failure(request_type, name, response_time, response_length, exception, **_kwargs):
self.stats.log_request(request_type, name, response_time, response_length)
self.stats.log_error(request_type, name, exception)

# temporarily set log level to ignore warnings to suppress deprecation message
loglevel = logging.getLogger().level
logging.getLogger().setLevel(logging.ERROR)
self.environment.events.request_success.add_listener(on_request_success)
self.environment.events.request_failure.add_listener(on_request_failure)
logging.getLogger().setLevel(loglevel)
self.environment.events.request.add_listener(on_request)

self.connection_broken = False
self.final_user_classes_count: Dict[str, int] = {} # just for the ratio report, fills before runner stops
Expand Down
19 changes: 0 additions & 19 deletions locust/test/test_fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,25 +693,6 @@ def test_catch_response_missing_with_block(self):
self.assertRaises(LocustError, r.success)
self.assertRaises(LocustError, r.failure, "")

def test_deprecated_request_events(self):
status = {"success_amount": 0, "failure_amount": 0}

def on_success(**kw):
status["success_amount"] += 1

def on_failure(**kw):
status["failure_amount"] += 1

self.environment.events.request_success.add_listener(on_success)
self.environment.events.request_failure.add_listener(on_failure)
with self.user.client.get("/ultra_fast", catch_response=True) as response:
pass
with self.user.client.get("/wrong_url", catch_response=True) as response:
pass

self.assertEqual(1, status["success_amount"])
self.assertEqual(1, status["failure_amount"])

def test_missing_catch_response_true(self):
# incorrect usage, missing catch_response=True
with self.user.client.get("/fail") as resp:
Expand Down
17 changes: 0 additions & 17 deletions locust/test/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,6 @@ def on_request(**kw):
s.request("get", "/wrong_url")
self.assertEqual("Not Found", kwargs["response"].text)

def test_deprecated_request_events(self):
s = self.get_client()
status = {"success_amount": 0, "failure_amount": 0}

def on_success(**kw):
status["success_amount"] += 1

def on_failure(**kw):
status["failure_amount"] += 1

self.environment.events.request_success.add_listener(on_success)
self.environment.events.request_failure.add_listener(on_failure)
s.request("get", "/request_method")
s.request("get", "/wrong_url")
self.assertEqual(1, status["success_amount"])
self.assertEqual(1, status["failure_amount"])

def test_error_message_with_name_replacement(self):
s = self.get_client()
kwargs = {}
Expand Down