Skip to content

Commit

Permalink
Merge pull request #1983 from uddmorningsun/master
Browse files Browse the repository at this point in the history
hardening Environment.shape_class for distinct usage
  • Loading branch information
cyberw authored Jan 29, 2022
2 parents b56fd99 + 38773c8 commit 7498e05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions locust/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ def __init__(
", ".join(map(methodcaller("fullname"), self.user_classes))
)
)
if self.shape_class is not None and not isinstance(self.shape_class, LoadTestShape):
raise ValueError(
"shape_class should be instance of LoadTestShape or subclass LoadTestShape, but got: %s"
% self.shape_class
)

def _create_runner(
self,
Expand Down
11 changes: 10 additions & 1 deletion locust/test/test_env.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from locust import (
constant,
)
from locust.env import Environment
from locust.env import Environment, LoadTestShape
from locust.user import (
User,
task,
Expand Down Expand Up @@ -190,3 +190,12 @@ def my_task(self):
e.exception.args[0],
"There are no users with weight > 0.",
)

def test_shape_class_attribute(self):
class SubLoadTestShape(LoadTestShape):
"""Inherited from locust.env.LoadTestShape"""

with self.assertRaisesRegex(
ValueError, r"instance of LoadTestShape or subclass LoadTestShape", msg="exception message is mismatching"
):
Environment(user_classes=[MyUserWithSameName1], shape_class=SubLoadTestShape)

0 comments on commit 7498e05

Please sign in to comment.