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

Custom messages: Throw an exception if someone tries to register twice for the same event #2513

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
2 changes: 2 additions & 0 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ def register_message(self, msg_type: str, listener: Callable) -> None:
:param msg_type: The type of the message to listen for
:param listener: The function to execute when the message is received
"""
if msg_type in self.custom_messages:
raise Exception(f"Tried to register listener method for {msg_type}, but it already had a listener!")
self.custom_messages[msg_type] = listener


Expand Down
14 changes: 14 additions & 0 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,20 @@ def on_custom_msg(msg, **kw):
msg = self.mocked_log.warning[0]
self.assertIn("Unknown message type received", msg)

def test_duplicate_message_handler_registration(self):
class MyUser(User):
@task
def my_task(self):
pass

def on_custom_msg(msg, **kw):
pass

environment = Environment(user_classes=[MyUser])
runner = LocalRunner(environment)
runner.register_message("test_custom_msg", on_custom_msg)
self.assertRaises(Exception, runner.register_message, "test_custom_msg", on_custom_msg)

def test_swarm_endpoint_is_non_blocking(self):
class TestUser1(User):
@task
Expand Down
Loading