Skip to content

Commit

Permalink
Handle user classes with weight = 0
Browse files Browse the repository at this point in the history
  • Loading branch information
mboutet committed Aug 19, 2021
1 parent 96f349b commit b63c7a4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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.
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.",
)

0 comments on commit b63c7a4

Please sign in to comment.