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
|
from __future__ import annotations
import contextvars
import time
from typing import Any
import pytest
from ..util import busy_wait
from .util import parametrize_setstatprofile
@parametrize_setstatprofile
def test_context_type(setstatprofile):
with pytest.raises(TypeError):
setstatprofile(lambda f, e, a: 0, 1e6, "not a context var")
setstatprofile(None)
profiler_context_var: contextvars.ContextVar[object | None] = contextvars.ContextVar(
"profiler_context_var", default=None
)
@parametrize_setstatprofile
def test_context_tracking(setstatprofile):
profile_calls = []
def profile_callback(frame, event, arg):
nonlocal profile_calls
profile_calls.append((frame, event, arg))
profiler_1 = object()
profiler_2 = object()
context_1 = contextvars.copy_context()
context_2 = contextvars.copy_context()
context_1.run(profiler_context_var.set, profiler_1)
context_2.run(profiler_context_var.set, profiler_2)
setstatprofile(
profile_callback,
1e10, # set large interval so we only get context_change events
profiler_context_var,
)
context_1.run(busy_wait, 0.001)
context_2.run(busy_wait, 0.001)
setstatprofile(None)
assert all(c[1] == "context_changed" for c in profile_calls)
assert len(profile_calls) == 4
new, old, _ = profile_calls[0][2]
assert old is None
assert new is profiler_1
new, old, _ = profile_calls[1][2]
assert old is profiler_1
assert new is None
new, old, _ = profile_calls[2][2]
assert old is None
assert new is profiler_2
new, old, _ = profile_calls[3][2]
assert old is profiler_2
assert new is None
|