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

Handle user classes with weight = 0 #1860

Merged
merged 1 commit into from
Aug 20, 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
15 changes: 15 additions & 0 deletions locust/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def __init__(

self._filter_tasks_by_tags()

self._remove_user_classes_with_weight_zero()

# Validate there's no class with the same name but in different modules
if len(set(user_class.__name__ for user_class in self.user_classes)) != len(self.user_classes):
raise ValueError(
Expand Down Expand Up @@ -216,6 +218,19 @@ def _filter_tasks_by_tags(self):
for user_class in self.user_classes:
filter_tasks_by_tags(user_class, self.tags, self.exclude_tags)

def _remove_user_classes_with_weight_zero(self):
"""
Remove user classes having a weight of zero.
"""
if len(self.user_classes) == 0:
# Preserve previous behaviour that allowed no user classes to be specified.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it ok to specify no user classes before? I dont know when that should not be considered an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this should make locust fail fast because it is invalid. The reason I put it there is that otherwise more than 20 tests are failing. If I remove this condition, I'll have to update all those tests so that they contain fake user class.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! Yea I guess we still fail in main.py on line 165 if you actually launch locust proper without any classes. Ready to merge, or is there something else that is WIP?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready

return
filtered_user_classes = [user_class for user_class in self.user_classes if user_class.weight > 0]
if len(filtered_user_classes) == 0:
# TODO: Better exception than `ValueError`?
raise ValueError("There are no users with weight > 0.")
self.user_classes[:] = filtered_user_classes

def assign_equal_weights(self):
"""
Update the user classes such that each user runs their specified tasks with equal
Expand Down
47 changes: 47 additions & 0 deletions locust/test/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,50 @@ class MyUser5(User):
environment.assign_equal_weights()
u = environment.user_classes[0]
verify_tasks(u, ["outside_task", "outside_task_2", "dict_task_1", "dict_task_2", "dict_task_3"])

def test_user_classes_with_zero_weight_are_removed(self):
class MyUser1(User):
wait_time = constant(0)
weight = 0

@task
def my_task(self):
pass

class MyUser2(User):
wait_time = constant(0)
weight = 1

@task
def my_task(self):
pass

environment = Environment(user_classes=[MyUser1, MyUser2])

self.assertEqual(len(environment.user_classes), 1)
self.assertIs(environment.user_classes[0], MyUser2)

def test_all_user_classes_with_zero_weight_raises_exception(self):
class MyUser1(User):
wait_time = constant(0)
weight = 0

@task
def my_task(self):
pass

class MyUser2(User):
wait_time = constant(0)
weight = 0

@task
def my_task(self):
pass

with self.assertRaises(ValueError) as e:
environment = Environment(user_classes=[MyUser1, MyUser2])

self.assertEqual(
e.exception.args[0],
"There are no users with weight > 0.",
)