|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Hashable |
| 4 | + |
| 5 | +from distributed import Event, Worker |
| 6 | +from distributed.utils_test import async_wait_for, gen_cluster, inc, wait_for_state |
| 7 | + |
| 8 | + |
| 9 | +def get_digests(w: Worker, allow: str | None = None) -> dict[Hashable, float]: |
| 10 | + # import pprint; pprint.pprint(dict(w.digests_total)) |
| 11 | + digests = { |
| 12 | + k: v |
| 13 | + for k, v in w.digests_total.items() |
| 14 | + if k |
| 15 | + not in { |
| 16 | + "latency", |
| 17 | + "tick-duration", |
| 18 | + "transfer-bandwidth", |
| 19 | + "transfer-duration", |
| 20 | + "compute-duration", |
| 21 | + } |
| 22 | + and (allow is None or allow in k) |
| 23 | + } |
| 24 | + assert all(v >= 0 for v in digests.values()), digests |
| 25 | + return digests |
| 26 | + |
| 27 | + |
| 28 | +@gen_cluster(client=True, nthreads=[("", 1)]) |
| 29 | +async def test_basic_execute(c, s, a): |
| 30 | + await c.submit(inc, 1, key="x") |
| 31 | + assert list(get_digests(a)) == [ |
| 32 | + ("execute", "x", "thread", "thread-cpu"), |
| 33 | + ("execute", "x", "thread"), |
| 34 | + ("execute", "x"), |
| 35 | + ] |
| 36 | + |
| 37 | + |
| 38 | +# @gen_cluster(client=True, nthreads=[("", 1)]) |
| 39 | +# async def test_run_spec_deserialization(c, s, a): |
| 40 | +# """Test that deserialization of run_spec is metered""" |
| 41 | +# await c.submit(inc, 1, key="x") |
| 42 | +# assert 0 < a.digests_total["execute", "x", "deserialize", "seconds"] < 1 |
| 43 | + |
| 44 | + |
| 45 | +@gen_cluster(client=True, nthreads=[("", 1)]) |
| 46 | +async def test_cancelled_execute(c, s, a): |
| 47 | + """cancelled(execute) tasks are metered as a separate lump total""" |
| 48 | + ev = await Event() |
| 49 | + x = c.submit(lambda ev: ev.wait(), ev, key="x") |
| 50 | + await wait_for_state("x", "executing", a) |
| 51 | + del x |
| 52 | + await wait_for_state("x", "cancelled", a) |
| 53 | + await ev.set() |
| 54 | + await async_wait_for(lambda: not a.state.tasks, timeout=5) |
| 55 | + |
| 56 | + print(list(get_digests(a))) |
| 57 | + assert list(get_digests(a)) == [("execute", "x", "cancelled")] |
0 commit comments