Skip to content

Commit 779a4f3

Browse files
chore(profiling): fix flaky test: 'test_upload_resets_profile'
1 parent 55e4eec commit 779a4f3

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

tests/profiling_v2/collector/test_threading.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import sys
77
import threading
8+
import time
89
from typing import Callable
910
from typing import List
1011
from typing import Optional
@@ -1022,6 +1023,10 @@ def test_upload_resets_profile(self) -> None:
10221023
with self.collector_class(capture_pct=100):
10231024
with self.lock_class(): # !CREATE! !ACQUIRE! !RELEASE! test_upload_resets_profile
10241025
pass
1026+
1027+
# Wait for collector to fully stop before uploading
1028+
time.sleep(0.05)
1029+
10251030
ddup.upload() # pyright: ignore[reportCallIssue]
10261031

10271032
linenos: LineNo = get_lock_linenos("test_upload_resets_profile", with_stmt=True)
@@ -1049,20 +1054,19 @@ def test_upload_resets_profile(self) -> None:
10491054
# This can be flaky due to timing or interference from other tests
10501055
pytest.skip(f"Profile validation failed (known flaky on some platforms): {e}")
10511056

1052-
# Now we call upload() again without any new lock operations
1053-
# We expect the profile to be empty or contain no samples
1057+
# Now we call upload() again, and we expect the profile to be empty
1058+
num_files_before_second_upload: int = len(glob.glob(self.output_filename + ".*"))
1059+
10541060
ddup.upload() # pyright: ignore[reportCallIssue]
10551061

1056-
# Try to parse the newest profile - it should either not exist (no new file)
1057-
# or have no samples (which would raise AssertionError in parse_newest_profile)
1058-
try:
1059-
_ = pprof_utils.parse_newest_profile(self.output_filename)
1060-
# If we got here, a profile with samples exists
1061-
# This might be okay if other collectors are running
1062-
pytest.skip("Profile still has samples (possibly from other activity - known flaky)")
1063-
except (AssertionError, IndexError):
1064-
# Expected: no profile file or no samples
1065-
pass
1062+
time.sleep(0.05)
1063+
1064+
num_files_after_second_upload: int = len(glob.glob(self.output_filename + ".*"))
1065+
1066+
# If a new file was created, it means the profile was *not* empty, and an AssertionError is raised
1067+
if num_files_before_second_upload != num_files_after_second_upload:
1068+
with pytest.raises(AssertionError):
1069+
pprof_utils.parse_newest_profile(self.output_filename)
10661070

10671071
def test_lock_hash(self) -> None:
10681072
"""Test that __hash__ allows profiled locks to be used in sets and dicts."""

0 commit comments

Comments
 (0)