Skip to content

Commit

Permalink
Merge pull request #2680 from locustio/add-ruff-pre-commit-hook
Browse files Browse the repository at this point in the history
Add ruff pre commit hook
  • Loading branch information
cyberw committed Apr 18, 2024
2 parents 790a9fb + f7d8f28 commit e8edada
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 31 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion docs/developing-locust.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/locustio/locust/compare>`_.

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 <https://pre-commit.com/>`_ 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
====================
Expand Down
3 changes: 1 addition & 2 deletions locust/test/test_interruptable_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
59 changes: 34 additions & 25 deletions locust/test/test_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e8edada

Please sign in to comment.