def _current_thread(): # This is a custom version of `threading.current_thread` # that does not try # to create a `DummyThread` on `KeyError`. ident = _thread.get_ident() try: thread = threading._active[ident] except KeyError: name = None else: name = thread.name return ident, name
def test_lock_acquire_events(): r = recorder.Recorder() with collector_threading.LockCollector(r, capture_pct=100): lock = threading.Lock() lock.acquire() assert len(r.events[collector_threading.LockAcquireEvent]) == 1 assert len(r.events[collector_threading.LockReleaseEvent]) == 0 event = r.events[collector_threading.LockAcquireEvent][0] assert event.lock_name == "test_threading.py:59" assert event.thread_id == _thread.get_ident() assert event.wait_time_ns > 0 # It's called through pytest so I'm sure it's gonna be that long, right? assert len(event.frames) > 3 assert event.nframes > 3 assert event.frames[0] == (__file__, 60, "test_lock_acquire_events") assert event.sampling_pct == 100
def test_exception_collection(): r = recorder.Recorder() c = stack.StackCollector(r) c.start() try: raise ValueError("hello") except Exception: sleep(1) c.stop() exception_events = r.events[stack.StackExceptionSampleEvent] assert len(exception_events) >= 1 e = exception_events[0] assert e.timestamp > 0 assert e.sampling_period > 0 if not TESTING_GEVENT: assert e.thread_id == _thread.get_ident() assert e.thread_name == "MainThread" assert e.frames == [(__file__, 209, "test_exception_collection")] assert e.nframes == 1 assert e.exc_type == ValueError