Skip to content

Commit e91f6b3

Browse files
chore(profiling): fix flaky test: 'test_upload_resets_profile'
1 parent b7a2d2a commit e91f6b3

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

tests/profiling_v2/collector/test_threading.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,9 +1010,16 @@ def test_upload_resets_profile(self) -> None:
10101010
# This test checks that the profile is cleared after each upload() call
10111011
# It is added in test_threading.py as LockCollector can easily be
10121012
# configured to be deterministic with capture_pct=100.
1013+
import time
1014+
10131015
with self.collector_class(capture_pct=100):
10141016
with self.lock_class(): # !CREATE! !ACQUIRE! !RELEASE! test_upload_resets_profile
10151017
pass
1018+
1019+
# Wait for collector to fully stop before uploading
1020+
# This prevents race conditions on macOS where samples might still be in flight
1021+
time.sleep(0.05)
1022+
10161023
ddup.upload() # pyright: ignore[reportCallIssue]
10171024

10181025
linenos: LineNo = get_lock_linenos("test_upload_resets_profile", with_stmt=True)
@@ -1037,11 +1044,21 @@ def test_upload_resets_profile(self) -> None:
10371044
)
10381045

10391046
# Now we call upload() again, and we expect the profile to be empty
1047+
# Count files before second upload
1048+
num_files_before: int = len(glob.glob(self.output_filename + ".*"))
1049+
10401050
ddup.upload() # pyright: ignore[reportCallIssue]
1041-
# parse_newest_profile raises an AssertionError if the profile doesn't
1042-
# have any samples
1043-
with pytest.raises(AssertionError):
1044-
pprof_utils.parse_newest_profile(self.output_filename)
1051+
1052+
# Wait for the profile reset to complete
1053+
time.sleep(0.05)
1054+
1055+
# Count files after second upload
1056+
num_files_after: int = len(glob.glob(self.output_filename + ".*"))
1057+
1058+
# If a new file was created, it should be empty and parse_newest_profile should raise
1059+
if num_files_before != num_files_after:
1060+
with pytest.raises(AssertionError):
1061+
pprof_utils.parse_newest_profile(self.output_filename)
10451062

10461063

10471064
class TestThreadingLockCollector(BaseThreadingLockCollectorTest):

0 commit comments

Comments
 (0)