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

Make User.run/TaskSet.run final and raise an exception if someone marks it with @task #1895

Merged
merged 4 commits into from
Sep 27, 2021
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
6 changes: 6 additions & 0 deletions locust/user/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import traceback
from time import time
from typing import Any, Callable, List, Union
from typing_extensions import final

import gevent
from gevent import GreenletExit
Expand Down Expand Up @@ -36,6 +37,10 @@ def decorator_func(func):
logging.warning(
"You have tagged your on_stop/start function with @task. This will make the method get called both as a task AND on stop/start."
) # this is usually not what the user intended
if func.__name__ == "run":
raise Exception(
"User.run() is a method used internally by Locust, and you must not override it or register it as a task"
)
func.locust_task_weight = weight
return func

Expand Down Expand Up @@ -270,6 +275,7 @@ def on_stop(self):
"""
pass

@final
def run(self):
try:
self.on_start()
Expand Down
2 changes: 2 additions & 0 deletions locust/user/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from locust.user.wait_time import constant
from typing import Any, Callable, Dict, List, TypeVar, Union
from typing_extensions import final
from gevent import GreenletExit, greenlet
from gevent.pool import Group
from locust.clients import HttpSession
Expand Down Expand Up @@ -124,6 +125,7 @@ def on_stop(self):
"""
pass

@final
def run(self):
self._state = LOCUST_STATE_RUNNING
self._taskset_instance = DefaultTaskSet(self)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"Flask-BasicAuth>=0.2.0",
"Flask-Cors>=3.0.10",
"roundrobin>=0.0.2",
"typing-extensions>=3.7.4.3",
],
test_suite="locust.test",
tests_require=[
Expand Down