Skip to content

Commit 34d838a

Browse files
committed
gh-157161: Fix test_rollover_based_on_st_birthtime_only on slow machines
The rotated file is named after the creation time of the log file, but the test only looked for it within a fixed 5 seconds before the end of the test. On a slow CI runner the test took 6 seconds, so the file was found nowhere and the test failed although the rollover was correct. Derive the search window from the time the test actually took.
1 parent 0b4c7da commit 34d838a

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎Lib/test/test_logging.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6752,6 +6752,7 @@ def add_record(message: str) -> None:
67526752
fh.emit(record)
67536753
fh.close()
67546754

6755+
start = datetime.datetime.now()
67556756
add_record('testing - initial')
67566757
self.assertLogFile(self.fn)
67576758
# Sleep a little over the half of rollover time - and this value
@@ -6764,18 +6765,21 @@ def add_record(message: str) -> None:
67646765

67656766
# At this point, the log file should be rotated if the rotation
67666767
# is based on creation time but should be not if it's based on
6767-
# modification time.
6768+
# modification time. The rotated file is named after the creation
6769+
# time of the log file, so look back over the whole duration of the
6770+
# test, plus a margin: the file was created in setUp(), and the
6771+
# names have a resolution of one second.
67686772
found = False
67696773
now = datetime.datetime.now()
6770-
GO_BACK = 5 # seconds
6771-
for secs in range(GO_BACK + 1):
6774+
go_back = int((now - start).total_seconds()) + 2
6775+
for secs in range(go_back + 1):
67726776
prev = now - datetime.timedelta(seconds=secs)
67736777
fn = self.fn + prev.strftime(".%Y-%m-%d_%H-%M-%S")
67746778
found = os.path.exists(fn)
67756779
if found:
67766780
self.rmfiles.append(fn)
67776781
break
6778-
msg = 'No rotated files found, went back %d seconds' % GO_BACK
6782+
msg = 'No rotated files found, went back %d seconds' % go_back
67796783
if not found:
67806784
# print additional diagnostics
67816785
dn, fn = os.path.split(self.fn)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``test_rollover_based_on_st_birthtime_only`` in ``test_logging`` on slow
2+
machines: search for the rotated file over the whole duration of the test
3+
instead of a fixed 5 seconds.

0 commit comments

Comments
 (0)