Skip to content

Commit 70abff0

Browse files
authored
Clean up calls to captured_logger (#7521)
1 parent d74f500 commit 70abff0

8 files changed

Lines changed: 33 additions & 40 deletions

File tree

distributed/diagnostics/tests/test_scheduler_plugin.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import logging
4-
53
import pytest
64

75
from distributed import Scheduler, SchedulerPlugin, Worker, get_worker
@@ -234,7 +232,7 @@ async def close(self):
234232
await s.register_scheduler_plugin(OK())
235233
await s.register_scheduler_plugin(Bad())
236234

237-
with captured_logger(logging.getLogger("distributed.scheduler")) as logger:
235+
with captured_logger("distributed.scheduler") as logger:
238236
await s.close()
239237

240238
out, err = capsys.readouterr()

distributed/tests/test_client.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ async def test_gather_skip(c, s, a):
644644
x = c.submit(div, 1, 0, priority=10)
645645
y = c.submit(slowinc, 1, delay=0.5)
646646

647-
with captured_logger(logging.getLogger("distributed.scheduler")) as sched:
648-
with captured_logger(logging.getLogger("distributed.client")) as client:
647+
with captured_logger("distributed.scheduler") as sched:
648+
with captured_logger("distributed.client") as client:
649649
L = await c.gather([x, y], errors="skip")
650650
assert L == [2]
651651

@@ -3479,7 +3479,7 @@ async def test_get_foo_lost_keys(c, s, u, v, w):
34793479
)
34803480
async def test_bad_tasks_fail(c, s, a, b):
34813481
f = c.submit(sys.exit, 0)
3482-
with captured_logger(logging.getLogger("distributed.scheduler")) as logger:
3482+
with captured_logger("distributed.scheduler") as logger:
34833483
with pytest.raises(KilledWorker) as info:
34843484
await f
34853485

@@ -5015,7 +5015,7 @@ async def test_fire_and_forget_err(c, s, a, b):
50155015

50165016

50175017
def test_quiet_client_close(loop):
5018-
with captured_logger(logging.getLogger("distributed")) as logger:
5018+
with captured_logger("distributed") as logger:
50195019
with Client(
50205020
loop=loop,
50215021
processes=False,
@@ -5041,7 +5041,7 @@ def test_quiet_client_close(loop):
50415041

50425042
@pytest.mark.slow
50435043
def test_quiet_client_close_when_cluster_is_closed_before_client(loop):
5044-
with captured_logger(logging.getLogger("tornado.application")) as logger:
5044+
with captured_logger("tornado.application") as logger:
50455045
cluster = LocalCluster(loop=loop, n_workers=1, dashboard_address=":0")
50465046
client = Client(cluster, loop=loop)
50475047
cluster.close()
@@ -5584,7 +5584,7 @@ async def test_profile_keys(c, s, a, b):
55845584

55855585
assert p["count"] == xp["count"] + yp["count"]
55865586

5587-
with captured_logger(logging.getLogger("distributed")) as logger:
5587+
with captured_logger("distributed") as logger:
55885588
prof = await c.profile("does-not-exist")
55895589
assert prof == profile.create()
55905590
out = logger.getvalue()
@@ -5839,7 +5839,7 @@ def test_client_doesnt_close_given_loop(loop_in_thread, s, a, b):
58395839
@gen_cluster(client=True, nthreads=[])
58405840
async def test_quiet_scheduler_loss(c, s):
58415841
c._periodic_callbacks["scheduler-info"].interval = 10
5842-
with captured_logger(logging.getLogger("distributed.client")) as logger:
5842+
with captured_logger("distributed.client") as logger:
58435843
await s.close()
58445844
text = logger.getvalue()
58455845
assert "BrokenPipeError" not in text
@@ -6335,7 +6335,7 @@ async def test_shutdown_is_quiet_with_cluster():
63356335
async with LocalCluster(
63366336
n_workers=1, asynchronous=True, processes=False, dashboard_address=":0"
63376337
) as cluster:
6338-
with captured_logger(logging.getLogger("distributed.client")) as logger:
6338+
with captured_logger("distributed.client") as logger:
63396339
timeout = 0.1
63406340
async with Client(cluster, asynchronous=True, timeout=timeout) as c:
63416341
await c.shutdown()
@@ -6349,7 +6349,7 @@ async def test_client_is_quiet_cluster_close():
63496349
async with LocalCluster(
63506350
n_workers=1, asynchronous=True, processes=False, dashboard_address=":0"
63516351
) as cluster:
6352-
with captured_logger(logging.getLogger("distributed.client")) as logger:
6352+
with captured_logger("distributed.client") as logger:
63536353
timeout = 0.1
63546354
async with Client(cluster, asynchronous=True, timeout=timeout) as c:
63556355
await cluster.close()
@@ -6830,7 +6830,7 @@ def handler(event):
68306830
while len(s.event_subscriber["test-topic"]) != 2:
68316831
await asyncio.sleep(0.01)
68326832

6833-
with captured_logger(logging.getLogger("distributed.client")) as logger:
6833+
with captured_logger("distributed.client") as logger:
68346834
await c.log_event("test-topic", {})
68356835

68366836
while len(received_events) < 2:
@@ -7472,7 +7472,7 @@ def no_message():
74727472
# missing "message" key should log TypeError
74737473
get_worker().log_event("warn", {})
74747474

7475-
with captured_logger(logging.getLogger("distributed.client")) as log:
7475+
with captured_logger("distributed.client") as log:
74767476
await c.submit(no_message)
74777477
assert "TypeError" in log.getvalue()
74787478

@@ -7600,7 +7600,7 @@ def print_otherfile():
76007600
# this should log a TypeError in the client
76017601
get_worker().log_event("print", {"args": ("hello",), "file": "bad value"})
76027602

7603-
with captured_logger(logging.getLogger("distributed.client")) as log:
7603+
with captured_logger("distributed.client") as log:
76047604
await c.submit(print_otherfile)
76057605
assert "TypeError" in log.getvalue()
76067606

@@ -7610,7 +7610,7 @@ async def test_print_manual_bad_args(c, s, a, b, capsys):
76107610
def foo():
76117611
get_worker().log_event("print", {"args": "not a tuple"})
76127612

7613-
with captured_logger(logging.getLogger("distributed.client")) as log:
7613+
with captured_logger("distributed.client") as log:
76147614
await c.submit(foo)
76157615
assert "TypeError" in log.getvalue()
76167616

distributed/tests/test_preload.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import logging
43
import os
54
import re
65
import shutil
@@ -298,8 +297,8 @@ def dask_teardown(worker):
298297
raise Exception(456)
299298
"""
300299

301-
with captured_logger(logging.getLogger("distributed.scheduler")) as s_logger:
302-
with captured_logger(logging.getLogger("distributed.worker")) as w_logger:
300+
with captured_logger("distributed.scheduler") as s_logger:
301+
with captured_logger("distributed.worker") as w_logger:
303302
async with Scheduler(dashboard_address=":0", preload=text) as s:
304303
async with Worker(s.address, preload=[text]) as w:
305304
pass

distributed/tests/test_scheduler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,7 +2834,7 @@ async def test_too_many_groups(c, s, a, b):
28342834

28352835
@gen_test()
28362836
async def test_multiple_listeners():
2837-
with captured_logger(logging.getLogger("distributed.scheduler")) as log:
2837+
with captured_logger("distributed.scheduler") as log:
28382838
async with Scheduler(dashboard_address=":0", protocol=["inproc", "tcp"]) as s:
28392839
async with Worker(s.listeners[0].contact_address) as a:
28402840
async with Worker(s.listeners[1].contact_address) as b:
@@ -2861,7 +2861,7 @@ async def test_worker_name_collision(s, a):
28612861
# test that a name collision for workers produces the expected response
28622862
# and leaves the data structures of Scheduler in a good state
28632863
# is not updated by the second worker
2864-
with captured_logger(logging.getLogger("distributed.scheduler")) as log:
2864+
with captured_logger("distributed.scheduler") as log:
28652865
with raises_with_cause(
28662866
RuntimeError, None, ValueError, f"name taken, {a.name!r}"
28672867
):

distributed/tests/test_sizeof.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

3-
import logging
4-
53
import pytest
64

75
from dask.sizeof import sizeof
@@ -23,13 +21,13 @@ def __sizeof__(self):
2321
foo = BadlySized()
2422

2523
# Defaults to 0.95 MiB by default
26-
with captured_logger(logging.getLogger("distributed.sizeof")) as logs:
24+
with captured_logger("distributed.sizeof") as logs:
2725
assert safe_sizeof(foo) == 1e6
2826

2927
assert "Sizeof calculation failed. Defaulting to 0.95 MiB" in logs.getvalue()
3028

3129
# Can provide custom `default_size`
32-
with captured_logger(logging.getLogger("distributed.sizeof")) as logs:
30+
with captured_logger("distributed.sizeof") as logs:
3331
default_size = 2 * (1024**2) # 2 MiB
3432
assert safe_sizeof(foo, default_size=default_size) == default_size
3533

distributed/tests/test_spill.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import array
4-
import logging
54
import os
65
import random
76
import uuid
@@ -150,7 +149,7 @@ def test_spillbuffer_maxlim(tmp_path_factory):
150149
# size of e < target but e+c > target, this will trigger movement of c to slow
151150
# but the max spill limit prevents it. Resulting in e remaining in fast
152151

153-
with captured_logger(logging.getLogger("distributed.spill")) as logs_e:
152+
with captured_logger("distributed.spill") as logs_e:
154153
buf["e"] = e
155154

156155
assert "disk reached capacity" in logs_e.getvalue()
@@ -159,7 +158,7 @@ def test_spillbuffer_maxlim(tmp_path_factory):
159158
# size of d > target, d should go to slow but slow reached the max_spill limit then
160159
# d will end up on fast with c (which can't be move to slow because it won't fit
161160
# either)
162-
with captured_logger(logging.getLogger("distributed.spill")) as logs_d:
161+
with captured_logger("distributed.spill") as logs_d:
163162
buf["d"] = d
164163

165164
assert "disk reached capacity" in logs_d.getvalue()
@@ -176,7 +175,7 @@ def test_spillbuffer_maxlim(tmp_path_factory):
176175
unlimited_buf["a_large"] = a_large
177176
assert psize(unlimited_buf_dir, a_large=a_large)[1] > 600
178177

179-
with captured_logger(logging.getLogger("distributed.spill")) as logs_alarge:
178+
with captured_logger("distributed.spill") as logs_alarge:
180179
buf["a"] = a_large
181180

182181
assert "disk reached capacity" in logs_alarge.getvalue()
@@ -186,7 +185,7 @@ def test_spillbuffer_maxlim(tmp_path_factory):
186185
# max_spill
187186

188187
d_large = "d" * 501
189-
with captured_logger(logging.getLogger("distributed.spill")) as logs_dlarge:
188+
with captured_logger("distributed.spill") as logs_dlarge:
190189
buf["d"] = d_large
191190

192191
assert "disk reached capacity" in logs_dlarge.getvalue()
@@ -216,7 +215,7 @@ def test_spillbuffer_fail_to_serialize(tmp_path):
216215

217216
# Exception caught in the worker
218217
with pytest.raises(TypeError, match="Could not serialize"):
219-
with captured_logger(logging.getLogger("distributed.spill")) as logs_bad_key:
218+
with captured_logger("distributed.spill") as logs_bad_key:
220219
buf["a"] = a
221220

222221
# spill.py must remain silent because we're already logging in worker.py
@@ -229,7 +228,7 @@ def test_spillbuffer_fail_to_serialize(tmp_path):
229228
assert_buf(buf, tmp_path, {"b": b}, {})
230229

231230
c = "c" * 100
232-
with captured_logger(logging.getLogger("distributed.spill")) as logs_bad_key_mem:
231+
with captured_logger("distributed.spill") as logs_bad_key_mem:
233232
# This will go to fast and try to kick b out,
234233
# but keep b in fast since it's not pickable
235234
buf["c"] = c
@@ -262,7 +261,7 @@ def test_spillbuffer_oserror(tmp_path):
262261
os.chmod(tmp_path, 0o555)
263262

264263
# Add key > than target
265-
with captured_logger(logging.getLogger("distributed.spill")) as logs_oserror_slow:
264+
with captured_logger("distributed.spill") as logs_oserror_slow:
266265
buf["c"] = c
267266

268267
assert "Spill to disk failed" in logs_oserror_slow.getvalue()
@@ -273,7 +272,7 @@ def test_spillbuffer_oserror(tmp_path):
273272

274273
# add key to fast which is smaller than target but when added it triggers spill,
275274
# which triggers OSError
276-
with captured_logger(logging.getLogger("distributed.spill")) as logs_oserror_evict:
275+
with captured_logger("distributed.spill") as logs_oserror_evict:
277276
buf["d"] = d
278277

279278
assert "Spill to disk failed" in logs_oserror_evict.getvalue()
@@ -298,7 +297,7 @@ def test_spillbuffer_evict(tmp_path):
298297
assert_buf(buf, tmp_path, {"bad": bad}, {"a": a})
299298

300299
# unsuccessful eviction
301-
with captured_logger(logging.getLogger("distributed.spill")) as logs_evict_key:
300+
with captured_logger("distributed.spill") as logs_evict_key:
302301
weight = buf.evict()
303302
assert weight == -1
304303

distributed/tests/test_variable.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import asyncio
4-
import logging
54
import random
65
from datetime import timedelta
76
from time import sleep
@@ -64,7 +63,7 @@ def foo():
6463
async def test_delete_unset_variable(c, s, a, b):
6564
x = Variable()
6665
assert x.client is c
67-
with captured_logger(logging.getLogger("distributed.utils")) as logger:
66+
with captured_logger("distributed.utils") as logger:
6867
x.delete()
6968
await c.close()
7069
text = logger.getvalue()

distributed/tests/test_worker_memory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ async def test_fail_to_pickle_spill(c, s, a):
304304
"""
305305
a.monitor.get_process_memory = lambda: 701 if a.data.fast else 0
306306

307-
with captured_logger(logging.getLogger("distributed.spill")) as logs:
307+
with captured_logger("distributed.spill") as logs:
308308
bad = c.submit(FailToPickle, key="bad")
309309
await wait(bad)
310310

@@ -586,7 +586,7 @@ def f(ev):
586586
while a.state.executing_count != 1:
587587
await asyncio.sleep(0.01)
588588

589-
with captured_logger(logging.getLogger("distributed.worker.memory")) as logger:
589+
with captured_logger("distributed.worker.memory") as logger:
590590
# Task that is queued on the worker when the worker pauses
591591
y = c.submit(inc, 1, key="y")
592592
while "y" not in a.state.tasks:
@@ -770,7 +770,7 @@ def __sizeof__(self):
770770
return 8_100_000_000
771771

772772
# Capture output of log_errors()
773-
with captured_logger(logging.getLogger("distributed.utils")) as logger:
773+
with captured_logger("distributed.utils") as logger:
774774
x = c.submit(C)
775775
await wait(x)
776776

0 commit comments

Comments
 (0)