From fe84ce7b560ebf1cbb1a3d2e5fec7761ef7f143e Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Thu, 18 Apr 2024 10:42:48 +0200 Subject: [PATCH 1/4] Add ruff pre-commit hook (see https://pre-commit.com/ for installation) --- .pre-commit-config.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..0710f6a0bb --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,10 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.3.7 + hooks: + # Run the linter. + - id: ruff + args: [--fix] + # Run the formatter. + - id: ruff-format From c480693a89f6be9b8d9f193f4cda4bc3441d8200 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Thu, 18 Apr 2024 10:45:42 +0200 Subject: [PATCH 2/4] lint --- locust/test/test_interruptable_task.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/locust/test/test_interruptable_task.py b/locust/test/test_interruptable_task.py index 967f9ac798..33cbd981eb 100644 --- a/locust/test/test_interruptable_task.py +++ b/locust/test/test_interruptable_task.py @@ -3,12 +3,11 @@ from locust.exception import StopUser from collections import defaultdict -from typing import DefaultDict from unittest import TestCase class InterruptableTaskSet(SequentialTaskSet): - counter: DefaultDict[str, int] = defaultdict(int) + counter: defaultdict[str, int] = defaultdict(int) def on_start(self): super().on_start() From be0d9250c577ba15a3a206a50dce5a72243cf83e Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Thu, 18 Apr 2024 10:52:20 +0200 Subject: [PATCH 3/4] Mention pre-commit in developer documentation. --- docs/developing-locust.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/developing-locust.rst b/docs/developing-locust.rst index e36713b24c..9cef85f79c 100644 --- a/docs/developing-locust.rst +++ b/docs/developing-locust.rst @@ -20,7 +20,9 @@ Now the ``locust`` command will run *your* code with no need for reinstalling af To contribute your changes, push to a branch in your repo and then `open a PR on github `_. -Before you open a pull request, make sure all the checks work. And if you are adding a feature, make sure it is documented (in ``docs/*.rst``). +If you install `pre-commit `_ linting and format checks/fixes will be automatically performed saving you a round trip to GitHub actions. + +Before you open a pull request, make sure all the tests work. And if you are adding a feature, make sure it is documented (in ``docs/*.rst``). Testing your changes ==================== From f7d8f28401a60af4d5350572ef1aa5237198acb3 Mon Sep 17 00:00:00 2001 From: Lars Holmberg Date: Thu, 18 Apr 2024 10:59:40 +0200 Subject: [PATCH 4/4] format stuff --- locust/test/test_main.py | 7 +++-- locust/test/test_runners.py | 59 +++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/locust/test/test_main.py b/locust/test/test_main.py index a71f8ef24e..ecedfaec41 100644 --- a/locust/test/test_main.py +++ b/locust/test/test_main.py @@ -1617,9 +1617,10 @@ def t(self): pass """ ) - with mock_locustfile(content=LOCUSTFILE_CONTENT) as mocked, patch_env( - "LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.01" - ) as _: + with ( + mock_locustfile(content=LOCUSTFILE_CONTENT) as mocked, + patch_env("LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.01") as _, + ): proc = subprocess.Popen( [ "locust", diff --git a/locust/test/test_runners.py b/locust/test/test_runners.py index cd5c1e6fe1..eb060f3197 100644 --- a/locust/test/test_runners.py +++ b/locust/test/test_runners.py @@ -992,8 +992,9 @@ def incr_stats(self): context={}, ) - with mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3), patch_env( - "LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1" + with ( + mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3), + patch_env("LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1"), ): # start a Master runner options = parse_options(["--enable-rebalancing"]) @@ -1419,9 +1420,12 @@ def tick(self): return None locust_worker_additional_wait_before_ready_after_stop = 5 - with mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3), patch_env( - "LOCUST_WORKER_ADDITIONAL_WAIT_BEFORE_READY_AFTER_STOP", - str(locust_worker_additional_wait_before_ready_after_stop), + with ( + mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3), + patch_env( + "LOCUST_WORKER_ADDITIONAL_WAIT_BEFORE_READY_AFTER_STOP", + str(locust_worker_additional_wait_before_ready_after_stop), + ), ): stop_timeout = 5 master_env = Environment( @@ -1676,9 +1680,12 @@ def tick(self): user_class.weight = random.uniform(1, 20) locust_worker_additional_wait_before_ready_after_stop = 5 - with mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3), patch_env( - "LOCUST_WORKER_ADDITIONAL_WAIT_BEFORE_READY_AFTER_STOP", - str(locust_worker_additional_wait_before_ready_after_stop), + with ( + mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3), + patch_env( + "LOCUST_WORKER_ADDITIONAL_WAIT_BEFORE_READY_AFTER_STOP", + str(locust_worker_additional_wait_before_ready_after_stop), + ), ): stop_timeout = 5 master_env = Environment( @@ -1706,12 +1713,7 @@ def tick(self): while master.state != STATE_STOPPED: self.assertTrue(time.time() - ts <= master_env.shape_class.stages[-1][0] + 60, master.state) print( - "{:.2f}/{:.2f} | {} | {:.0f} | ".format( - time.time() - ts, - master_env.shape_class.stages[-1][0], - master.state, - sum(master.reported_user_classes_count.values()), - ) + f"{time.time() - ts:.2f}/{master_env.shape_class.stages[-1][0]:.2f} | {master.state} | {sum(master.reported_user_classes_count.values()):.0f} | " + json.dumps(dict(sorted(master.reported_user_classes_count.items(), key=itemgetter(0)))) ) sleep(1) @@ -1822,9 +1824,12 @@ def tick(self): return None locust_worker_additional_wait_before_ready_after_stop = 2 - with mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3), patch_env( - "LOCUST_WORKER_ADDITIONAL_WAIT_BEFORE_READY_AFTER_STOP", - str(locust_worker_additional_wait_before_ready_after_stop), + with ( + mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=0.3), + patch_env( + "LOCUST_WORKER_ADDITIONAL_WAIT_BEFORE_READY_AFTER_STOP", + str(locust_worker_additional_wait_before_ready_after_stop), + ), ): master_env = Environment(user_classes=[TestUser1], shape_class=TestShape()) @@ -2192,8 +2197,9 @@ class TestUser(User): def my_task(self): gevent.sleep(600) - with mock.patch("locust.rpc.rpc.Server", mocked_rpc()) as server, patch_env( - "LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1" + with ( + mock.patch("locust.rpc.rpc.Server", mocked_rpc()) as server, + patch_env("LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1"), ): master = self.get_runner(user_classes=[TestUser]) server.mocked_send(Message("client_ready", __version__, "fake_client1")) @@ -2220,8 +2226,9 @@ class TestUser(User): def my_task(self): gevent.sleep(600) - with mock.patch("locust.rpc.rpc.Server", mocked_rpc()) as server, patch_env( - "LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1" + with ( + mock.patch("locust.rpc.rpc.Server", mocked_rpc()) as server, + patch_env("LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1"), ): master = self.get_runner(user_classes=[TestUser]) server.mocked_send(Message("client_ready", __version__, "fake_client1")) @@ -2291,8 +2298,9 @@ class TestUser(User): def my_task(self): gevent.sleep(600) - with mock.patch("locust.rpc.rpc.Server", mocked_rpc()) as server, patch_env( - "LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1" + with ( + mock.patch("locust.rpc.rpc.Server", mocked_rpc()) as server, + patch_env("LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "0.1"), ): master = self.get_runner(user_classes=[TestUser]) server.mocked_send(Message("client_ready", __version__, "fake_client1")) @@ -3154,8 +3162,9 @@ def assert_cache_hits(): assert_cache_hits() master._wait_for_workers_report_after_ramp_up.cache_clear() - with mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=1.5), patch_env( - "LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "5.7 * WORKER_REPORT_INTERVAL" + with ( + mock.patch("locust.runners.WORKER_REPORT_INTERVAL", new=1.5), + patch_env("LOCUST_WAIT_FOR_WORKERS_REPORT_AFTER_RAMP_UP", "5.7 * WORKER_REPORT_INTERVAL"), ): self.assertEqual(master._wait_for_workers_report_after_ramp_up(), 5.7 * 1.5) assert_cache_hits()