Skip to content

Commit 1b9fe5c

Browse files
gh-84649: Fix unstable test_rollover_at_midnight (GH-151813)
The test forced a rollover by back-dating the file modification time, but the rollover time is now based also on the creation time, which cannot be changed. Instead, set the rollover time one second after the file creation time and wait until the clock reaches it. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent aec0aed commit 1b9fe5c

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

Lib/test/test_logging.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6705,20 +6705,18 @@ def add_record(message: str) -> None:
67056705

67066706
def test_rollover_at_midnight(self, weekly=False):
67076707
os_helper.unlink(self.fn)
6708+
# Emit the first records a little after the beginning of a whole
6709+
# second, so that their file times fall inside that second and not the
6710+
# previous one, which would cause an unwanted rollover.
67086711
now = datetime.datetime.now()
6709-
atTime = now.time()
6710-
if not 0.1 < atTime.microsecond/1e6 < 0.9:
6711-
# The test requires all records to be emitted within
6712-
# the range of the same whole second.
6713-
time.sleep((0.1 - atTime.microsecond/1e6) % 1.0)
6712+
if not 0.1 < now.microsecond/1e6 < 0.9:
6713+
time.sleep((0.1 - now.microsecond/1e6) % 1.0)
67146714
now = datetime.datetime.now()
6715-
atTime = now.time()
6716-
atTime = atTime.replace(microsecond=0)
67176715
fmt = logging.Formatter('%(asctime)s %(message)s')
67186716
when = f'W{now.weekday()}' if weekly else 'MIDNIGHT'
67196717
for i in range(3):
67206718
fh = logging.handlers.TimedRotatingFileHandler(
6721-
self.fn, encoding="utf-8", when=when, atTime=atTime)
6719+
self.fn, encoding="utf-8", when=when, atTime=now.time())
67226720
fh.setFormatter(fmt)
67236721
r2 = logging.makeLogRecord({'msg': f'testing1 {i}'})
67246722
fh.emit(r2)
@@ -6728,15 +6726,22 @@ def test_rollover_at_midnight(self, weekly=False):
67286726
for i, line in enumerate(f):
67296727
self.assertIn(f'testing1 {i}', line)
67306728

6731-
os.utime(self.fn, (now.timestamp() - 1,)*2)
6729+
# The creation time, used to compute the rollover time, cannot be
6730+
# changed, so the rollover cannot be forced by back-dating the file.
6731+
# Wait until the clock reaches a rollover time set one second ahead.
6732+
rollover = int(time.time()) + 1
6733+
atTime = datetime.datetime.fromtimestamp(rollover).time()
6734+
while time.time() < rollover:
6735+
time.sleep(rollover - time.time())
67326736
for i in range(2):
67336737
fh = logging.handlers.TimedRotatingFileHandler(
67346738
self.fn, encoding="utf-8", when=when, atTime=atTime)
67356739
fh.setFormatter(fmt)
67366740
r2 = logging.makeLogRecord({'msg': f'testing2 {i}'})
67376741
fh.emit(r2)
67386742
fh.close()
6739-
rolloverDate = now - datetime.timedelta(days=7 if weekly else 1)
6743+
rolloverDate = (datetime.datetime.fromtimestamp(rollover)
6744+
- datetime.timedelta(days=7 if weekly else 1))
67406745
otherfn = f'{self.fn}.{rolloverDate:%Y-%m-%d}'
67416746
self.assertLogFile(otherfn)
67426747
with open(self.fn, encoding="utf-8") as f:

0 commit comments

Comments
 (0)