Skip to content

Commit ffaa18b

Browse files
author
Julien Danjou
authored
test(profiling): enhance gevent thread hub/task detection (#2990)
This test regularly fails because the first events from the stack collector are from the gevent thread booting up, while we expect to get frames from the thread running. This changes the test to ignore events where we see the thread boot time, and only look for when the thread actually runs. If we find it once, we know we're good.
1 parent 9f8b827 commit ffaa18b

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

tests/profiling/collector/test_stack.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ def test_collect_gevent_thread_hub():
605605

606606
# Start some greenthreads: they do nothing we just keep switching between them.
607607
def _nothing():
608-
for _ in range(10000):
608+
for _ in range(100):
609609
# Do nothing and just switch to another greenlet
610-
time.sleep(0)
610+
time.sleep(0.01)
611611

612612
threads = []
613613
with s:
@@ -619,24 +619,21 @@ def _nothing():
619619
t.join()
620620

621621
main_thread_found = False
622-
for event in r.events[stack.StackSampleEvent]:
622+
sleep_task_found = False
623+
events = r.events[stack.StackSampleEvent]
624+
for event in events:
623625
if event.task_id == compat.main_thread.ident:
624626
if event.task_name is None:
625627
pytest.fail("Task with no name detected, is it the Hub?")
626628
else:
627629
main_thread_found = True
628-
elif event.task_id in {t.ident for t in threads}:
630+
elif event.task_id in {t.ident for t in threads} and event.frames[0][2] in (
631+
"_nothing",
632+
"sleep",
633+
):
629634
# Make sure we capture the sleep call and not a gevent hub frame
630-
assert event.frames[0][2] in (
631-
"_nothing",
632-
"sleep",
633-
"get_ident",
634-
"__bootstrap_inner",
635-
"_bootstrap_inner",
636-
"switch",
637-
"run",
638-
"notify",
639-
)
635+
sleep_task_found = True
640636

641637
# Make sure we did at least one check
642-
assert main_thread_found
638+
assert main_thread_found, events
639+
assert sleep_task_found, events

0 commit comments

Comments
 (0)