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

fix: Ensure new test starts with specified number of users after previous test has been stopped #2152

Merged
merged 4 commits into from
Aug 7, 2022
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
2 changes: 2 additions & 0 deletions locust/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ def stop(self) -> None:

self.stop_users(self.user_classes_count)

self._users_dispatcher = None

self.update_state(STATE_STOPPED)

self.cpu_log_warning()
Expand Down
55 changes: 55 additions & 0 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,61 @@ class BaseUser2(BaseUser1):
sleep(1)
self.assertEqual(0, runner.user_count)

def test_user_count_starts_from_specified_amount_when_creating_new_test_after_previous_step_has_been_stopped(self):
"""Test for https://github.com/locustio/locust/issues/2135"""

class MyUser1(User):
wait_time = constant(0)

@task
def my_task(self):
pass

env = Environment(user_classes=[MyUser1], stop_timeout=0)
local_runner = env.create_local_runner()
web_ui = env.create_web_ui("127.0.0.1", 0)

gevent.sleep(0.1)

response = requests.post(
f"http://127.0.0.1:{web_ui.server.server_port}/swarm",
data={"user_count": 20, "spawn_rate": 20, "host": "https://localhost"},
)
self.assertEqual(200, response.status_code)

t0 = time.perf_counter()
while local_runner.user_count != 20:
self.assertTrue(time.perf_counter() - t0 <= 1, local_runner.user_count)
gevent.sleep(0.1)

response = requests.get(
f"http://127.0.0.1:{web_ui.server.server_port}/stop",
)
self.assertEqual(200, response.status_code)

t0 = time.perf_counter()
while local_runner.state != STATE_STOPPED:
self.assertTrue(time.perf_counter() - t0 <= 1, local_runner.state)
gevent.sleep(0.1)

t0 = time.perf_counter()
response = requests.post(
f"http://127.0.0.1:{web_ui.server.server_port}/swarm",
data={"user_count": 1, "spawn_rate": 1, "host": "https://localhost"},
)
self.assertEqual(200, response.status_code)
self.assertTrue(time.perf_counter() - t0 <= 1, "Stop endpoint should not be blocking")

# Make sure user count stays at 1 over 2s (2s is arbitrary, but we
# wait long enough to make sure it stays constant)
t0 = time.perf_counter()
while time.perf_counter() - t0 <= 2:
self.assertTrue(local_runner.user_count <= 1, local_runner.user_count)
gevent.sleep(0.1)

local_runner.stop()
web_ui.stop()


class TestMasterWorkerRunners(LocustTestCase):
def test_distributed_integration_run(self):
Expand Down