Repository navigation
Expand file tree
/
Copy pathtest_observed.py
More file actions
415 lines (267 loc) · 10.9 KB
/
Copy pathtest_observed.py
File metadata and controls
415 lines (267 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"""Tests for observed(): recording proxies for bare callables."""
import functools
import inspect
from collections.abc import Iterator
from typing import Any
import pytest
import wrapture
from wrapture import ObservedCallable, binding, observed, redact, timeline
class Gateway:
def charge(self, amount: int) -> str:
return f"ch_{amount}"
gateway = Gateway()
def greet(name: str, punctuation: str = "!") -> str:
return f"hello {name}{punctuation}"
def relay(name: str) -> str:
return gateway.charge(7) and greet(name)
def counting(limit: int) -> Iterator[int]:
yield from range(limit)
# ---------------------------------------------------------------------------
# recording
# ---------------------------------------------------------------------------
def test_calls_record_with_a_derived_path_and_label() -> None:
wrapped = observed(greet)
with timeline() as tape:
assert wrapped("world") == "hello world!"
(event,) = tape.all
assert event.kind == "call"
assert event.path == f"{__name__}:greet"
assert event.label is None
assert event.arguments == {"name": "world", "punctuation": "!"}
assert event.result == "hello world!"
def test_label_can_be_overridden() -> None:
wrapped = observed(greet, label="greeter")
with timeline() as tape:
wrapped("world")
assert tape.all[0].label == "greeter"
def test_the_factory_form_captures_the_function_below() -> None:
# Called with only options, observed() returns the decorator, so
# both spellings work: bare @observed on a def, and
# @observed(label=...) when options are wanted.
@observed(label="quiet greeter", capture=redact("name"))
def whisper(name: str) -> str:
return f"psst {name}"
assert isinstance(whisper, ObservedCallable)
with timeline() as tape:
whisper("secret")
event = tape.all[0]
assert event.label == "quiet greeter"
assert event.arguments is not None
assert event.arguments["name"] != "secret"
def test_the_factory_form_options_apply() -> None:
@observed(when=False)
def silent() -> str:
return "s"
@observed
def bare() -> str:
return "b"
with timeline() as tape:
assert silent() == "s"
assert bare() == "b"
(event,) = tape.all
assert event.label is None and event.path.endswith(".bare")
def test_the_factory_form_dedupes_by_label() -> None:
wrapped = observed(greet, label="agent:greet")
again = observed(label="agent:greet")(wrapped)
assert again is wrapped
def test_events_nest_around_observed_calls() -> None:
# An observed callable participates in the tree exactly as a bound
# one: bindings fired inside it nest under it, and it nests under
# whatever called it.
wrapped = observed(relay)
charge = binding(Gateway, "charge")
with charge, timeline() as tape:
wrapped("world")
outer, inner = tape.all
assert outer.binding is wrapped
assert inner.parent_id == outer.seq
def test_exceptions_are_recorded_and_raised() -> None:
def failing() -> None:
raise RuntimeError("boom")
wrapped = observed(failing)
with timeline() as tape:
with pytest.raises(RuntimeError, match="boom"):
wrapped()
assert isinstance(tape.all[0].exception, RuntimeError)
def test_a_generator_outcome_records_iteration() -> None:
wrapped = observed(counting)
with timeline() as tape:
assert list(wrapped(3)) == [0, 1, 2]
event = tape.all[0]
assert event.items == 3
assert event.result is None # a plain generator's return value
def test_not_recording_calls_straight_through() -> None:
wrapped = observed(greet)
assert wrapped("quiet") == "hello quiet!"
def test_capture_options_apply() -> None:
wrapped = observed(greet, capture=redact("name"))
with timeline() as tape:
wrapped("secret")
assert tape.all[0].arguments == {"name": "<redacted>", "punctuation": "!"}
def test_when_declines_recording_but_calls_through() -> None:
wrapped = observed(greet, when=lambda _, args, kwargs: args[0] != "nobody")
with timeline() as tape:
assert wrapped("nobody") == "hello nobody!"
assert wrapped("world") == "hello world!"
assert len(tape.all) == 1
assert wrapped.filtered_calls == 1
def test_when_false_never_records_and_counts_nothing() -> None:
wrapped = observed(greet, when=False)
with timeline() as tape:
assert wrapped("quiet") == "hello quiet!"
assert len(tape.all) == 0
assert wrapped.filtered_calls == 0
# ---------------------------------------------------------------------------
# lifecycle and assertions
# ---------------------------------------------------------------------------
def test_suspend_and_resume() -> None:
wrapped = observed(greet)
with timeline() as tape:
wrapped.suspend()
assert wrapped("quiet") == "hello quiet!"
wrapped.resume()
wrapped("loud")
assert len(tape.all) == 1
assert wrapped.suspended_calls == 1
def test_events_property_filters_to_this_proxy() -> None:
wrapped = observed(greet)
other = observed(counting)
with timeline():
wrapped("world")
list(other(2))
wrapped.events.assert_once()
other.events.assert_once()
def test_events_outside_a_timeline_raises() -> None:
wrapped = observed(greet)
with pytest.raises(RuntimeError, match="inside a timeline"):
_ = wrapped.events
# ---------------------------------------------------------------------------
# transparency
# ---------------------------------------------------------------------------
def test_the_proxy_is_transparent() -> None:
wrapped = observed(greet)
assert wrapped.__name__ == "greet"
assert wrapped == greet
assert list(inspect.signature(wrapped).parameters) == ["name", "punctuation"]
def test_a_coroutine_function_is_still_detected_as_one() -> None:
async def fetch() -> str:
return "data"
wrapped = observed(fetch)
assert inspect.iscoroutinefunction(wrapped)
def test_observed_is_idempotent_per_label() -> None:
# The label identifies the observation; with none given the
# derived name serves, so the wrap-in-place idiom re-runs safely.
wrapped = observed(greet)
assert observed(wrapped) is wrapped
def test_the_same_label_is_applied_only_once() -> None:
# Re-applying an existing label returns the callable unchanged;
# the second call's options do not reconfigure the observation.
wrapped = observed(greet, label="agent:greet")
again = observed(wrapped, label="agent:greet", when=False)
assert again is wrapped
with timeline() as tape:
wrapped("world")
assert len(tape.all) == 1
def test_dedupe_sees_through_foreign_wrappers() -> None:
# Detection follows the full __wrapped__ chain via wrapt's
# wrapper_chain(), so an observation stays deduplicated even after
# a later decorator buries it, and what was given is returned
# unchanged, the outer wrapper intact.
inner = observed(greet, label="agent:greet")
@functools.wraps(inner)
def decorated(*args: Any, **kwargs: Any) -> Any:
return inner(*args, **kwargs)
unchanged: Any = observed(decorated, label="agent:greet")
assert unchanged is decorated
def test_distinct_labels_stack_and_both_record() -> None:
# Two observations of one callable are two layers, each recording
# its own event, nested. An accidental double wrap under different
# labels is indistinguishable from this, so it is not an error;
# the double counting is the visible symptom.
first = observed(greet, label="apm:greet")
second = observed(first, label="audit:greet")
assert second is not first
with timeline() as tape:
second("world")
outer, inner = tape.all
assert outer.label == "audit:greet"
assert inner.label == "apm:greet"
assert inner.parent_id == outer.seq
def test_observed_requires_a_callable() -> None:
with pytest.raises(TypeError, match="wraps a callable"):
observed(42) # type: ignore[call-overload]
def test_the_proxy_type_is_exported() -> None:
assert isinstance(observed(greet), ObservedCallable)
assert isinstance(observed(greet), wrapture.ObservedCallable)
# ---------------------------------------------------------------------------
# placement on a class: the proxy binds as a method
# ---------------------------------------------------------------------------
def task_hook(self: Any, task_id: str, args: tuple[Any, ...]) -> None:
pass
def test_placed_on_a_class_the_proxy_binds_and_records_the_instance() -> None:
hook = observed(task_hook, label="before_start")
class Task:
before_start: Any = hook
task = Task()
with timeline():
task.before_start("id-1", (2, 2))
event = hook.events.assert_once().first
assert event.instance is task
assert event.arguments == {"task_id": "id-1", "args": (2, 2)}
def test_bound_access_reaches_the_proxy_state_and_events() -> None:
hook = observed(task_hook, label="state_hook")
class Task:
before_start: Any = hook
task = Task()
with timeline():
task.before_start.suspend()
task.before_start("id-1", ())
task.before_start.resume()
task.before_start("id-2", ())
assert hook.suspended_calls == 1
task.before_start.events.assert_once()
def test_bound_signature_drops_self() -> None:
hook = observed(task_hook, label="signature_hook")
class Task:
before_start: Any = hook
import inspect
assert list(inspect.signature(Task().before_start).parameters) == [
"task_id",
"args",
]
def test_when_receives_the_bound_instance() -> None:
seen: list[Any] = []
def only_flagged(
instance: Any, args: tuple[Any, ...], kwargs: dict[str, Any]
) -> bool:
seen.append(instance)
return getattr(instance, "flagged", True)
hook = observed(task_hook, label="when_hook", when=only_flagged)
class Task:
flagged: bool
before_start: Any = hook
flagged = Task()
flagged.flagged = True
quiet = Task()
quiet.flagged = False
with timeline():
flagged.before_start("id-1", ())
quiet.before_start("id-2", ())
assert seen == [flagged, quiet]
assert hook.events.assert_once().first.instance is flagged
assert hook.filtered_calls == 1
def test_wrapping_a_bound_method_records_its_instance() -> None:
class Gateway:
def charge(self, amount: int) -> int:
return amount
gateway = Gateway()
charge = observed(gateway.charge)
with timeline():
charge(500)
event = charge.events.assert_once().first
assert event.instance is gateway
assert event.arguments == {"amount": 500}
def test_the_proxy_stays_weak_referenceable() -> None:
import weakref
proxy = observed(task_hook, label="weak_hook")
assert weakref.ref(proxy)() is proxy