Debian Codegraph @

src/trio/_core/_tests/test_instrumentation.py

raw
1 from __future__ import annotations
2
3 from typing import TYPE_CHECKING, NoReturn
4
5 import attrs
6 import pytest
7
8 from ... import _abc, _core
9 from .tutil import check_sequence_matches
10
11 if TYPE_CHECKING:
12 from collections.abc import Container, Iterable
13
14 from ...lowlevel import Task
15
16
17 @attrs.define(eq=False, slots=False)
18 class TaskRecorder(_abc.Instrument):
19 record: list[tuple[str, Task | None]] = attrs.Factory(list)
20
21 def before_run(self) -> None:
22 self.record.append(("before_run", None))
23
24 def task_scheduled(self, task: Task) -> None:
25 self.record.append(("schedule", task))
26
27 def before_task_step(self, task: Task) -> None:
28 assert task is _core.current_task()
29 self.record.append(("before", task))
30
31 def after_task_step(self, task: Task) -> None:
32 assert task is _core.current_task()
33 self.record.append(("after", task))
34
35 def after_run(self) -> None:
36 self.record.append(("after_run", None))
37
38 def filter_tasks(self, tasks: Container[Task]) -> Iterable[tuple[str, Task | None]]:
39 for item in self.record:
40 if item[0] in ("schedule", "before", "after") and item[1] in tasks:
41 yield item
42 if item[0] in ("before_run", "after_run"):
43 yield item
44
45
46 def test_instruments(recwarn: object) -> None:
47 r1 = TaskRecorder()
48 r2 = TaskRecorder()
49 r3 = TaskRecorder()
50
51 task = None
52
53 # We use a child task for this, because the main task does some extra
54 # bookkeeping stuff that can leak into the instrument results, and we
55 # don't want to deal with it.
56 async def task_fn() -> None:
57 nonlocal task
58 task = _core.current_task()
59
60 for _ in range(4):
61 await _core.checkpoint()
62 # replace r2 with r3, to test that we can manipulate them as we go
63 _core.remove_instrument(r2)
64 with pytest.raises(KeyError):
65 _core.remove_instrument(r2)
66 # add is idempotent
67 _core.add_instrument(r3)
68 _core.add_instrument(r3)
69 for _ in range(1):
70 await _core.checkpoint()
71
72 async def main() -> None:
73 async with _core.open_nursery() as nursery:
74 nursery.start_soon(task_fn)
75
76 _core.run(main, instruments=[r1, r2])
77
78 # It sleeps 5 times, so it runs 6 times. Note that checkpoint()
79 # reschedules the task immediately upon yielding, before the
80 # after_task_step event fires.
81 expected = (
82 [("before_run", None), ("schedule", task)]
83 + [("before", task), ("schedule", task), ("after", task)] * 5
84 + [("before", task), ("after", task), ("after_run", None)]
85 )
86 assert r1.record == r2.record + r3.record
87 assert task is not None
88 assert list(r1.filter_tasks([task])) == expected
89
90
91 def test_instruments_interleave() -> None:
92 tasks = {}
93
94 async def two_step1() -> None:
95 tasks["t1"] = _core.current_task()
96 await _core.checkpoint()
97
98 async def two_step2() -> None:
99 tasks["t2"] = _core.current_task()
100 await _core.checkpoint()
101
102 async def main() -> None:
103 async with _core.open_nursery() as nursery:
104 nursery.start_soon(two_step1)
105 nursery.start_soon(two_step2)
106
107 r = TaskRecorder()
108 _core.run(main, instruments=[r])
109
110 expected = [
111 ("before_run", None),
112 ("schedule", tasks["t1"]),
113 ("schedule", tasks["t2"]),
114 {
115 ("before", tasks["t1"]),
116 ("schedule", tasks["t1"]),
117 ("after", tasks["t1"]),
118 ("before", tasks["t2"]),
119 ("schedule", tasks["t2"]),
120 ("after", tasks["t2"]),
121 },
122 {
123 ("before", tasks["t1"]),
124 ("after", tasks["t1"]),
125 ("before", tasks["t2"]),
126 ("after", tasks["t2"]),
127 },
128 ("after_run", None),
129 ]
130 print(list(r.filter_tasks(tasks.values())))
131 check_sequence_matches(list(r.filter_tasks(tasks.values())), expected)
132
133
134 def test_null_instrument() -> None:
135 # undefined instrument methods are skipped
136 class NullInstrument(_abc.Instrument):
137 def something_unrelated(self) -> None:
138 pass # pragma: no cover
139
140 async def main() -> None:
141 await _core.checkpoint()
142
143 _core.run(main, instruments=[NullInstrument()])
144
145
146 def test_instrument_before_after_run() -> None:
147 record = []
148
149 class BeforeAfterRun(_abc.Instrument):
150 def before_run(self) -> None:
151 record.append("before_run")
152
153 def after_run(self) -> None:
154 record.append("after_run")
155
156 async def main() -> None:
157 pass
158
159 _core.run(main, instruments=[BeforeAfterRun()])
160 assert record == ["before_run", "after_run"]
161
162
163 def test_instrument_task_spawn_exit() -> None:
164 record = []
165
166 class SpawnExitRecorder(_abc.Instrument):
167 def task_spawned(self, task: Task) -> None:
168 record.append(("spawned", task))
169
170 def task_exited(self, task: Task) -> None:
171 record.append(("exited", task))
172
173 async def main() -> Task:
174 return _core.current_task()
175
176 main_task = _core.run(main, instruments=[SpawnExitRecorder()])
177 assert ("spawned", main_task) in record
178 assert ("exited", main_task) in record
179
180
181 # This test also tests having a crash before the initial task is even spawned,
182 # which is very difficult to handle.
183 def test_instruments_crash(caplog: pytest.LogCaptureFixture) -> None:
184 record = []
185
186 class BrokenInstrument(_abc.Instrument):
187 def task_scheduled(self, task: Task) -> NoReturn:
188 record.append("scheduled")
189 raise ValueError("oops")
190
191 def close(self) -> None:
192 # Shouldn't be called -- tests that the instrument disabling logic
193 # works right.
194 record.append("closed") # pragma: no cover
195
196 async def main() -> Task:
197 record.append("main ran")
198 return _core.current_task()
199
200 r = TaskRecorder()
201 main_task = _core.run(main, instruments=[r, BrokenInstrument()])
202 assert record == ["scheduled", "main ran"]
203 # the TaskRecorder kept going throughout, even though the BrokenInstrument
204 # was disabled
205 assert ("after", main_task) in r.record
206 assert ("after_run", None) in r.record
207 # And we got a log message
208 assert caplog.records[0].exc_info is not None
209 exc_type, exc_value, _exc_traceback = caplog.records[0].exc_info
210 assert exc_type is ValueError
211 assert str(exc_value) == "oops"
212 assert "Instrument has been disabled" in caplog.records[0].message
213
214
215 def test_instruments_monkeypatch() -> None:
216 class NullInstrument(_abc.Instrument):
217 pass
218
219 instrument = NullInstrument()
220
221 async def main() -> None:
222 record: list[Task] = []
223
224 # Changing the set of hooks implemented by an instrument after
225 # it's installed doesn't make them start being called right away
226 instrument.before_task_step = ( # type: ignore[method-assign]
227 record.append # type: ignore[assignment] # append is pos-only
228 )
229
230 await _core.checkpoint()
231 await _core.checkpoint()
232 assert len(record) == 0
233
234 # But if we remove and re-add the instrument, the new hooks are
235 # picked up
236 _core.remove_instrument(instrument)
237 _core.add_instrument(instrument)
238 await _core.checkpoint()
239 await _core.checkpoint()
240 assert record.count(_core.current_task()) == 2
241
242 _core.remove_instrument(instrument)
243 await _core.checkpoint()
244 await _core.checkpoint()
245 assert record.count(_core.current_task()) == 2
246
247 _core.run(main, instruments=[instrument])
248
249
250 def test_instrument_that_raises_on_getattr() -> None:
251 class EvilInstrument(_abc.Instrument):
252 def task_exited(self, task: Task) -> NoReturn:
253 raise AssertionError("this should never happen") # pragma: no cover
254
255 @property
256 def after_run(self) -> NoReturn:
257 raise ValueError("oops")
258
259 async def main() -> None:
260 with pytest.raises(ValueError, match=r"^oops$"):
261 _core.add_instrument(EvilInstrument())
262
263 # Make sure the instrument is fully removed from the per-method lists
264 runner = _core.current_task()._runner
265 assert "after_run" not in runner.instruments
266 assert "task_exited" not in runner.instruments
267
268 _core.run(main)
269
270
271 def test_instrument_call_trio_context() -> None:
272 called = set()
273
274 class Instrument(_abc.Instrument):
275 pass
276
277 hooks = {
278 # not run in task context
279 "after_io_wait": (True, False),
280 "before_io_wait": (True, False),
281 "before_run": (True, False),
282 "after_run": (True, False),
283 # run in task context
284 "before_task_step": (True, True),
285 "after_task_step": (True, True),
286 "task_exited": (True, True),
287 # depends
288 "task_scheduled": (True, None),
289 "task_spawned": (True, None),
290 }
291 for hook, val in hooks.items():
292
293 def h(
294 self: Instrument,
295 *args: object,
296 hook: str = hook,
297 val: tuple[bool, bool | None] = val,
298 ) -> None:
299 fail_str = f"failed in {hook}"
300
301 assert _core.in_trio_run() == val[0], fail_str
302 if val[1] is not None:
303 assert _core.in_trio_task() == val[1], fail_str
304 called.add(hook)
305
306 setattr(Instrument, hook, h)
307
308 async def main() -> None:
309 await _core.checkpoint()
310
311 async with _core.open_nursery() as nursery:
312 nursery.start_soon(_core.checkpoint)
313
314 _core.run(main, instruments=[Instrument()])
315 assert called == set(hooks)