FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-88352: Make TimedRotatingFileHandler tests more stable by serhiy-storchaka · Pull Request #116409 · python/cpython · GitHub

Repository navigation

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Next Next commit
gh-88352: Make TimedRotatingFileHandler tests more stable
The tests failed (with less than 1% probability) if for example the file
was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
tests are always run within the range of the same whole second.
  • Loading branch information
serhiy-storchaka committed Mar 6, 2024
commit 8f7b2aa34f95c0d59a1d1d977b8cc3edcc1a626d
27 changes: 22 additions & 5 deletions Lib/test/test_logging.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6081,7 +6081,16 @@ def test_rollover(self):
self.assertTrue(found, msg=msg)

def test_rollover_at_midnight(self):
atTime = datetime.datetime.now().time()
os_helper.unlink(self.fn)
now = datetime.datetime.now()
atTime = now.time()
if not 0.1 < atTime.microsecond/1e6 < 0.9:
# The test requires all records to be emitted within
# the range of the same whole second.
time.sleep((0.1 - atTime.microsecond/1e6) % 1.0)
now = datetime.datetime.now()
atTime = now.time()
atTime = atTime.replace(microsecond=0)
fmt = logging.Formatter('%(asctime)s %(message)s')
for i in range(3):
fh = logging.handlers.TimedRotatingFileHandler(
Expand All @@ -6095,15 +6104,15 @@ def test_rollover_at_midnight(self):
for i, line in enumerate(f):
self.assertIn(f'testing1 {i}', line)

os.utime(self.fn, (time.time() - 1,)*2)
os.utime(self.fn, (now.timestamp() - 1,)*2)
for i in range(2):
fh = logging.handlers.TimedRotatingFileHandler(
self.fn, encoding="utf-8", when='MIDNIGHT', atTime=atTime)
fh.setFormatter(fmt)
r2 = logging.makeLogRecord({'msg': f'testing2 {i}'})
fh.emit(r2)
fh.close()
rolloverDate = datetime.datetime.now() - datetime.timedelta(days=1)
rolloverDate = now - datetime.timedelta(days=1)
otherfn = f'{self.fn}.{rolloverDate:%Y-%m-%d}'
self.assertLogFile(otherfn)
with open(self.fn, encoding="utf-8") as f:
Expand All @@ -6114,8 +6123,16 @@ def test_rollover_at_midnight(self):
self.assertIn(f'testing1 {i}', line)

def test_rollover_at_weekday(self):
os_helper.unlink(self.fn)
now = datetime.datetime.now()
atTime = now.time()
if not 0.1 < atTime.microsecond/1e6 < 0.9:
# The test requires all records to be emitted within
# the range of the same whole second.
time.sleep((0.1 - atTime.microsecond/1e6) % 1.0)
now = datetime.datetime.now()
atTime = now.time()
atTime = atTime.replace(microsecond=0)
fmt = logging.Formatter('%(asctime)s %(message)s')
for i in range(3):
fh = logging.handlers.TimedRotatingFileHandler(
Expand All @@ -6129,15 +6146,15 @@ def test_rollover_at_weekday(self):
for i, line in enumerate(f):
self.assertIn(f'testing1 {i}', line)

os.utime(self.fn, (time.time() - 1,)*2)
os.utime(self.fn, (now.timestamp() - 1,)*2)
for i in range(2):
fh = logging.handlers.TimedRotatingFileHandler(
self.fn, encoding="utf-8", when=f'W{now.weekday()}', atTime=atTime)
fh.setFormatter(fmt)
r2 = logging.makeLogRecord({'msg': f'testing2 {i}'})
fh.emit(r2)
fh.close()
rolloverDate = datetime.datetime.now() - datetime.timedelta(days=7)
rolloverDate = now - datetime.timedelta(days=7)
otherfn = f'{self.fn}.{rolloverDate:%Y-%m-%d}'
self.assertLogFile(otherfn)
with open(self.fn, encoding="utf-8") as f:
Expand Down

Back | FazBrowse Home | New Git URL