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

--tags filter issue(#2849) fix #2957

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions locust/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ def __init__(self, *args: object, addr=None) -> None:

class RunnerAlreadyExistsError(Exception):
pass


class NoTaskToRun(Exception):
def __init__(self, message):
super().__init__(message)
self.exit_code = 1
3 changes: 2 additions & 1 deletion locust/test/test_locust_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from locust.exception import (
CatchResponseError,
InterruptTaskSet,
NoTaskToRun,
RescheduleTask,
RescheduleTaskImmediately,
ResponseError,
Expand Down Expand Up @@ -60,7 +61,7 @@ class MyUser(User):
wait_time = constant(0.5)

l = MyUser(self.environment)
self.assertRaisesRegex(Exception, "No tasks defined on MyUser.*", l.run)
self.assertRaisesRegex(NoTaskToRun, "No tasks defined on MyUser.*", l.run)
MyUser.task = object()
self.assertRaisesRegex(Exception, ".*but you have set a 'task' attribute.*", l.run)

Expand Down
18 changes: 13 additions & 5 deletions locust/user/task.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from __future__ import annotations

from locust.exception import InterruptTaskSet, MissingWaitTimeError, RescheduleTask, RescheduleTaskImmediately, StopUser
from locust.exception import (
InterruptTaskSet,
MissingWaitTimeError,
NoTaskToRun,
RescheduleTask,
RescheduleTaskImmediately,
StopUser,
)

import logging
import random
Expand Down Expand Up @@ -361,6 +368,8 @@ def run(self):
except Exception:
logging.error("Uncaught exception in on_stop: \n%s", traceback.format_exc())
raise
except NoTaskToRun:
raise
except Exception as e:
self.user.environment.events.user_error.fire(user_instance=self, exception=e, tb=e.__traceback__)
if self.user.environment.catch_exceptions:
Expand Down Expand Up @@ -472,13 +481,12 @@ class DefaultTaskSet(TaskSet):

def get_next_task(self):
if not self.user.tasks:
warning_message = "Use the @task decorator or set the 'tasks' attribute of the User (or mark it as abstract = True if you only intend to subclass it)"
if getattr(self.user, "task", None):
extra_message = ", but you have set a 'task' attribute on your class - maybe you meant to set 'tasks'?"
raise Exception(f"No tasks defined on {self.user.__class__.__name__}{extra_message}{warning_message}")
else:
extra_message = "."
raise Exception(
f"No tasks defined on {self.user.__class__.__name__}{extra_message} Use the @task decorator or set the 'tasks' attribute of the User (or mark it as abstract = True if you only intend to subclass it)"
)
raise NoTaskToRun(f"No tasks defined on {self.user.__class__.__name__}. {warning_message}")
return random.choice(self.user.tasks)

def execute_task(self, task):
Expand Down
Loading