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

Ensure target_user_count is set before test_start event is fired #1894

Merged
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
9 changes: 5 additions & 4 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ def start(self, user_count: int, spawn_rate: float, wait: bool = False):
If False (the default), a greenlet that spawns the users will be
started and the call to this method will return immediately.
"""
self.target_user_count = user_count

if self.state != STATE_RUNNING and self.state != STATE_SPAWNING:
self.stats.clear_all()
self.exceptions = {}
Expand All @@ -329,8 +331,6 @@ def start(self, user_count: int, spawn_rate: float, wait: bool = False):

self._users_dispatcher.new_dispatch(user_count, spawn_rate)

self.target_user_count = user_count

try:
for dispatched_users in self._users_dispatcher:
user_classes_spawn_count = {}
Expand Down Expand Up @@ -659,6 +659,9 @@ def cpu_log_warning(self):

def start(self, user_count: int, spawn_rate: float, **kwargs) -> None:
self.spawning_completed = False

self.target_user_count = user_count

num_workers = len(self.clients.ready) + len(self.clients.running) + len(self.clients.spawning)
if not num_workers:
logger.warning(
Expand Down Expand Up @@ -700,8 +703,6 @@ def start(self, user_count: int, spawn_rate: float, **kwargs) -> None:

self._users_dispatcher.new_dispatch(target_user_count=user_count, spawn_rate=spawn_rate)

self.target_user_count = user_count

try:
for dispatched_users in self._users_dispatcher:
dispatch_greenlets = Group()
Expand Down
18 changes: 18 additions & 0 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,13 @@ def my_task(self):
environment = Environment(user_classes=[MyUser1])
runner = LocalRunner(environment)

test_start_event_fired = [False]

@environment.events.test_start.add_listener
def on_test_start(*args, **kwargs):
test_start_event_fired[0] = True
self.assertEqual(runner.target_user_count, 3)

runner.start(user_count=3, spawn_rate=1, wait=False)

gevent.sleep(1)
Expand All @@ -639,6 +646,8 @@ def my_task(self):

runner.quit()

self.assertTrue(test_start_event_fired[0])


class TestMasterWorkerRunners(LocustTestCase):
def test_distributed_integration_run(self):
Expand Down Expand Up @@ -1594,6 +1603,13 @@ def my_task(self):
master_env = Environment(user_classes=[MyUser1])
master = master_env.create_master_runner("*", 0)

test_start_event_fired = [False]

@master_env.events.test_start.add_listener
def on_test_start(*args, **kwargs):
test_start_event_fired[0] = True
self.assertEqual(master.target_user_count, 3)

sleep(0)

# start 1 worker runner
Expand Down Expand Up @@ -1624,6 +1640,8 @@ def my_task(self):
# make sure users are killed
self.assertEqual(0, worker.user_count)

self.assertTrue(test_start_event_fired[0])


class TestMasterRunner(LocustTestCase):
def setUp(self):
Expand Down