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 TaskSet .user and .parent read only properties, avoids / fixes #1379 #1380

Merged
merged 1 commit into from
May 22, 2020
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
19 changes: 19 additions & 0 deletions locust/test/test_locust_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,25 @@ class MyUser(User):
self.assertTrue(isinstance(parents["sub"], RootTaskSet))
self.assertTrue(isinstance(parents["subsub"], SubTaskSet))

def test_user_is_read_only(self):
class MyTaskSet(TaskSet):
raised_attribute_error = False
@task
def overwrite_user(self):
try:
self.user = "whatever"
except AttributeError:
MyTaskSet.raised_attribute_error = True
raise StopUser()

class MyUser(User):
wait_time = constant(0)
host = ""
tasks = [MyTaskSet]

MyUser(Environment()).run()
self.assertTrue(MyTaskSet.raised_attribute_error)


class TestLocustClass(LocustTestCase):
def test_locust_wait(self):
Expand Down
33 changes: 19 additions & 14 deletions locust/user/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,25 +218,19 @@ class ForumPage(TaskSet):
if not set on the TaskSet.
"""

user = None
"""Will refer to the root User class instance when the TaskSet has been instantiated"""

parent = None
"""
Will refer to the parent TaskSet, or User, class instance when the TaskSet has been
instantiated. Useful for nested TaskSet classes.
"""
_user = None
_parent = None

def __init__(self, parent):
self._task_queue = []
self._time_start = time()

if isinstance(parent, TaskSet):
self.user = parent.user
self._user = parent.user
else:
self.user = parent
self._user = parent

self.parent = parent
self._parent = parent

# if this class doesn't have a min_wait, max_wait or wait_function defined, copy it from Locust
if not self.min_wait:
Expand All @@ -246,9 +240,21 @@ def __init__(self, parent):
if not self.wait_function:
self.wait_function = self.user.wait_function



@property
def user(self):
""":py:class:`User <locust.User>` instance that this TaskSet was created by"""
return self._user

@property
def parent(self):
"""Parent TaskSet instance of this TaskSet (or :py:class:`User <locust.User>` if this is not a nested TaskSet)"""
return self._parent

def on_start(self):
"""
Called when a User starts executing (enters) this TaskSet
Called when a User starts executing this TaskSet
"""
pass

Expand Down Expand Up @@ -392,8 +398,7 @@ def interrupt(self, reschedule=True):
@property
def client(self):
"""
Reference to the :py:attr:`client <locust.User.client>` attribute of the root
User instance.
Shortcut to the client :py:attr:`client <locust.User.client>` attribute of this TaskSet's :py:class:`User <locust.User>`
"""
return self.user.client

Expand Down