Skip to content

Commit

Permalink
Lint various tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberw committed Apr 18, 2024
1 parent c0b9177 commit 7eae4da
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 76 deletions.
3 changes: 1 addition & 2 deletions locust/test/mock_logging.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

import logging

from typing import List, Union, Dict
from types import TracebackType
from typing import Union

LogMessage = list[Union[str, dict[str, TracebackType]]]

Expand Down
40 changes: 7 additions & 33 deletions locust/test/test_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2167,9 +2167,7 @@ def test_ramp_up_from_0_to_100_000_users_with_50_user_classes_and_1000_workers_a
dispatch_iteration_duration <= tol
for dispatch_iteration_duration in users_dispatcher.dispatch_iteration_durations
),
"One or more dispatch took more than {:.0f}s to compute (max = {}ms)".format(
tol * 1000, 1000 * max(users_dispatcher.dispatch_iteration_durations)
),
f"One or more dispatch took more than {tol * 1000:.0f}s to compute (max = {1000 * max(users_dispatcher.dispatch_iteration_durations)}ms)",
)

self.assertEqual(_user_count(all_dispatched_users[-1]), target_user_count)
Expand All @@ -2181,9 +2179,7 @@ def test_ramp_up_from_0_to_100_000_users_with_50_user_classes_and_1000_workers_a
self.assertLessEqual(
max(user_count_on_workers) - min(user_count_on_workers),
1,
"One or more workers have too much users compared to the other workers when user count is {}".format(
_user_count(dispatch_users)
),
f"One or more workers have too much users compared to the other workers when user count is {_user_count(dispatch_users)}",
)

for i, dispatch_users in enumerate(all_dispatched_users):
Expand All @@ -2202,9 +2198,7 @@ def test_ramp_up_from_0_to_100_000_users_with_50_user_classes_and_1000_workers_a
self.assertLessEqual(
error_percent,
tol,
"Distribution for user class {} is off by more than {}% when user count is {}".format(
user_class, tol, _user_count(dispatch_users)
),
f"Distribution for user class {user_class} is off by more than {tol}% when user count is {_user_count(dispatch_users)}",
)

def test_ramp_down_from_100_000_to_0_users_with_50_user_classes_and_1000_workers_and_5000_spawn_rate(self):
Expand Down Expand Up @@ -2236,9 +2230,7 @@ def test_ramp_down_from_100_000_to_0_users_with_50_user_classes_and_1000_workers
dispatch_iteration_duration <= tol
for dispatch_iteration_duration in users_dispatcher.dispatch_iteration_durations
),
"One or more dispatch took more than {:.0f}ms to compute (max = {}ms)".format(
tol * 1000, 1000 * max(users_dispatcher.dispatch_iteration_durations)
),
f"One or more dispatch took more than {tol * 1000:.0f}ms to compute (max = {1000 * max(users_dispatcher.dispatch_iteration_durations)}ms)",
)

self.assertEqual(_user_count(all_dispatched_users[-1]), 0)
Expand All @@ -2250,9 +2242,7 @@ def test_ramp_down_from_100_000_to_0_users_with_50_user_classes_and_1000_workers
self.assertLessEqual(
max(user_count_on_workers) - min(user_count_on_workers),
1,
"One or more workers have too much users compared to the other workers when user count is {}".format(
_user_count(dispatch_users)
),
f"One or more workers have too much users compared to the other workers when user count is {_user_count(dispatch_users)}",
)

for dispatch_users in all_dispatched_users[:-1]:
Expand All @@ -2267,9 +2257,7 @@ def test_ramp_down_from_100_000_to_0_users_with_50_user_classes_and_1000_workers
self.assertLessEqual(
error_percent,
tol,
"Distribution for user class {} is off by more than {}% when user count is {}".format(
user_class, tol, _user_count(dispatch_users)
),
f"Distribution for user class {user_class} is off by more than {tol}% when user count is {_user_count(dispatch_users)}",
)


Expand Down Expand Up @@ -3448,9 +3436,7 @@ def __init__(self, fixed_counts: tuple[int], weights: tuple[int], target_user_co
self.target_user_count = target_user_count

def __str__(self):
return "<RampUpCase fixed_counts={} weights={} target_user_count={}>".format(
self.fixed_counts, self.weights, self.target_user_count
)
return f"<RampUpCase fixed_counts={self.fixed_counts} weights={self.weights} target_user_count={self.target_user_count}>"

def case_handler(self, cases: list[RampUpCase], expected: list[dict[str, int]], user_classes: list[type[User]]):
self.assertEqual(len(cases), len(expected))
Expand Down Expand Up @@ -3734,8 +3720,6 @@ class User3(User):

worker_node1 = WorkerNode("1")

sleep_time = 0.2

user_dispatcher = UsersDispatcher(worker_nodes=[worker_node1], user_classes=[User1, User2, User3])

user_dispatcher.new_dispatch(target_user_count=3, spawn_rate=3)
Expand All @@ -3761,8 +3745,6 @@ class User3(User):

worker_node1 = WorkerNode("1")

sleep_time = 0.2

user_dispatcher = UsersDispatcher(worker_nodes=[worker_node1], user_classes=[User1, User2, User3])

user_dispatcher.new_dispatch(target_user_count=10, spawn_rate=10, user_classes=[User2])
Expand All @@ -3780,8 +3762,6 @@ class User3(User):

worker_node1 = WorkerNode("1")

sleep_time = 0.2

user_dispatcher = UsersDispatcher(worker_nodes=[worker_node1], user_classes=[User1, User2, User3])

user_dispatcher.new_dispatch(target_user_count=10, spawn_rate=10, user_classes=[User2])
Expand All @@ -3802,8 +3782,6 @@ class User3(User):

worker_node1 = WorkerNode("1")

sleep_time = 0.2

user_dispatcher = UsersDispatcher(worker_nodes=[worker_node1], user_classes=[User1, User2, User3])

user_dispatcher.new_dispatch(target_user_count=10, spawn_rate=10, user_classes=[User2])
Expand All @@ -3826,8 +3804,6 @@ class User3(User):
worker_node2 = WorkerNode("2")
worker_node3 = WorkerNode("3")

sleep_time = 0.2

user_dispatcher = UsersDispatcher(
worker_nodes=[worker_node1, worker_node2, worker_node3], user_classes=[User1, User2, User3]
)
Expand Down Expand Up @@ -3886,8 +3862,6 @@ class User3(User):
worker_node2 = WorkerNode("2")
worker_node3 = WorkerNode("3")

sleep_time = 0.2

user_dispatcher = UsersDispatcher(
worker_nodes=[worker_node1, worker_node2, worker_node3], user_classes=[User1, User2, User3]
)
Expand Down
6 changes: 3 additions & 3 deletions locust/test/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def my_task(self):

def test_user_classes_with_same_name_is_error(self):
with self.assertRaises(ValueError) as e:
environment = Environment(user_classes=[MyUserWithSameName1, MyUserWithSameName2])
Environment(user_classes=[MyUserWithSameName1, MyUserWithSameName2])

self.assertEqual(
e.exception.args[0],
Expand Down Expand Up @@ -186,7 +186,7 @@ def my_task(self):
pass

with self.assertRaises(ValueError) as e:
environment = Environment(user_classes=[MyUser1, MyUser2])
Environment(user_classes=[MyUser1, MyUser2])

self.assertEqual(
e.exception.args[0],
Expand Down Expand Up @@ -269,7 +269,7 @@ def my_task(self):
available_user_classes={"User1": MyUser1, "User2": MyUser2},
available_user_tasks={"User1": MyUser1.tasks, "User2": MyUser2.tasks},
)
worker = worker_env.create_worker_runner("127.0.0.1", master.server.port)
worker_env.create_worker_runner("127.0.0.1", master.server.port)

master_env.update_user_class({"user_class_name": "User1", "host": "http://localhost", "tasks": ["my_task_2"]})

Expand Down
18 changes: 9 additions & 9 deletions locust/test/test_fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_streaming_response_catch_response(self):
def test_slow_redirect(self):
s = self.get_client()
url = "/redirect?url=/redirect&delay=0.5"
r = s.get(url)
s.get(url)
stats = self.runner.stats.get(url, method="GET")
self.assertEqual(1, stats.num_requests)
self.assertGreater(stats.avg_response_time, 500)
Expand Down Expand Up @@ -187,7 +187,7 @@ class OtherException(Exception):
with s.get("/fail", catch_response=True) as r:
r.success()
raise OtherException("wtf")
except OtherException as e:
except OtherException:
pass
else:
self.fail("OtherException should have been raised")
Expand All @@ -197,14 +197,14 @@ class OtherException(Exception):

def test_catch_response_default_success(self):
s = self.get_client()
with s.get("/ultra_fast", catch_response=True) as r:
with s.get("/ultra_fast", catch_response=True):
pass
self.assertEqual(1, self.environment.stats.get("/ultra_fast", "GET").num_requests)
self.assertEqual(0, self.environment.stats.get("/ultra_fast", "GET").num_failures)

def test_catch_response_default_fail(self):
s = self.get_client()
with s.get("/fail", catch_response=True) as r:
with s.get("/fail", catch_response=True):
pass
self.assertEqual(1, self.environment.stats.total.num_requests)
self.assertEqual(1, self.environment.stats.total.num_failures)
Expand Down Expand Up @@ -301,7 +301,7 @@ class MyUser(FastHttpUser):

l = MyUser(self.environment)
path = "/no_content_length"
r = l.client.get(path)
l.client.get(path)
self.assertEqual(
self.runner.stats.get(path, "GET").avg_content_length,
len("This response does not have content-length in the header"),
Expand All @@ -313,7 +313,7 @@ class MyUser(FastHttpUser):

l = MyUser(self.environment)
path = "/no_content_length"
r = l.client.get(path, stream=True)
l.client.get(path, stream=True)
self.assertEqual(0, self.runner.stats.get(path, "GET").avg_content_length)

def test_request_stats_named_endpoint(self):
Expand Down Expand Up @@ -526,7 +526,7 @@ class MyLocust(FastHttpUser):
def test_slow_redirect(self):
s = FastHttpSession(self.environment, "http://127.0.0.1:%i" % self.port, user=None)
url = "/redirect?url=/redirect&delay=0.5"
r = s.get(url)
s.get(url)
stats = self.runner.stats.get(url, method="GET")
self.assertEqual(1, stats.num_requests)
self.assertGreater(stats.avg_response_time, 500)
Expand Down Expand Up @@ -650,7 +650,7 @@ def test_catch_response(self):
self.assertEqual(1, self.num_success)

def test_catch_response_http_fail(self):
with self.user.client.get("/fail", catch_response=True) as response:
with self.user.client.get("/fail", catch_response=True):
pass
self.assertEqual(1, self.num_failures)
self.assertEqual(0, self.num_success)
Expand Down Expand Up @@ -683,7 +683,7 @@ def test_interrupt_taskset_with_catch_response(self):
class MyTaskSet(TaskSet):
@task
def interrupted_task(self):
with self.client.get("/ultra_fast", catch_response=True) as r:
with self.client.get("/ultra_fast", catch_response=True):
raise InterruptTaskSet()

class MyUser(FastHttpUser):
Expand Down
14 changes: 7 additions & 7 deletions locust/test/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_streaming_response(self):
def test_slow_redirect(self):
s = self.get_client()
url = "/redirect?url=/redirect&delay=0.5"
r = s.get(url)
s.get(url)
stats = self.runner.stats.get(url, method="GET")
self.assertEqual(1, stats.num_requests)
self.assertGreater(stats.avg_response_time, 500)
Expand Down Expand Up @@ -217,7 +217,7 @@ class OtherException(Exception):
with s.get("/fail", catch_response=True) as r:
r.success()
raise OtherException("wtf")
except OtherException as e:
except OtherException:
pass
else:
self.fail("OtherException should have been raised")
Expand All @@ -228,24 +228,24 @@ class OtherException(Exception):
def test_catch_response_response_error(self):
s = self.get_client()
try:
with s.get("/fail", catch_response=True) as r:
with s.get("/fail", catch_response=True):
raise ResponseError("response error")
except ResponseError as e:
except ResponseError:
self.fail("ResponseError should not have been raised")

self.assertEqual(1, self.environment.stats.total.num_requests)
self.assertEqual(1, self.environment.stats.total.num_failures)

def test_catch_response_default_success(self):
s = self.get_client()
with s.get("/ultra_fast", catch_response=True) as r:
with s.get("/ultra_fast", catch_response=True):
pass
self.assertEqual(1, self.environment.stats.get("/ultra_fast", "GET").num_requests)
self.assertEqual(0, self.environment.stats.get("/ultra_fast", "GET").num_failures)

def test_catch_response_default_fail(self):
s = self.get_client()
with s.get("/fail", catch_response=True) as r:
with s.get("/fail", catch_response=True):
pass
self.assertEqual(1, self.environment.stats.total.num_requests)
self.assertEqual(1, self.environment.stats.total.num_failures)
Expand All @@ -260,7 +260,7 @@ def on_request(**kw):

self.environment.events.request.add_listener(on_request)

with s.get("/wrong_url/01", name="replaced_url_name") as r:
with s.get("/wrong_url/01", name="replaced_url_name"):
pass

self.assertIn("for url: replaced_url_name", str(kwargs["exception"]))
Expand Down
8 changes: 4 additions & 4 deletions locust/test/test_locust_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,19 +748,19 @@ def test_catch_response(self):
self.assertEqual(1, self.num_failures)
self.assertEqual(0, self.num_success)

with self.locust.client.get("/ultra_fast", catch_response=True) as response:
with self.locust.client.get("/ultra_fast", catch_response=True):
pass
self.assertEqual(1, self.num_failures)
self.assertEqual(1, self.num_success)

with self.locust.client.get("/ultra_fast", catch_response=True) as response:
with self.locust.client.get("/ultra_fast", catch_response=True):
raise ResponseError("Not working")

self.assertEqual(2, self.num_failures)
self.assertEqual(1, self.num_success)

def test_catch_response_http_fail(self):
with self.locust.client.get("/fail", catch_response=True) as response:
with self.locust.client.get("/fail", catch_response=True):
pass
self.assertEqual(1, self.num_failures)
self.assertEqual(0, self.num_success)
Expand Down Expand Up @@ -793,7 +793,7 @@ def test_interrupt_taskset_with_catch_response(self):
class MyTaskSet(TaskSet):
@task
def interrupted_task(self):
with self.client.get("/ultra_fast", catch_response=True) as r:
with self.client.get("/ultra_fast", catch_response=True):
raise InterruptTaskSet()

class MyUser(HttpUser):
Expand Down
4 changes: 2 additions & 2 deletions locust/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ def my_task(self):
try:
response = requests.get(f"http://localhost:{port}/")
except ConnectionError:
succcess = False
success = False
try:
_, stderr = proc.communicate(timeout=5)
except subprocess.TimeoutExpired:
Expand Down Expand Up @@ -1084,7 +1084,7 @@ def test_html_report_option(self):
with mock_locustfile() as mocked:
with temporary_file("", suffix=".html") as html_report_file_path:
try:
output = subprocess.check_output(
subprocess.check_output(
[
"locust",
"-f",
Expand Down
Loading

0 comments on commit 7eae4da

Please sign in to comment.