diff options
| author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2021-12-14 00:53:37 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-14 00:53:37 (GMT) |
| commit | cb589d1b6bad4b75852c2e2a471a3800d5efdca7 (patch) | |
| tree | 8f740a9418027df57d63bdb1ee510afa8ec1d148 /Lib/test/test_logging.py | |
| parent | eb483c46d62707bdf705491f76cf1fa9642fb47e (diff) | |
| download | cpython-cb589d1b6bad4b75852c2e2a471a3800d5efdca7.zip cpython-cb589d1b6bad4b75852c2e2a471a3800d5efdca7.tar.gz cpython-cb589d1b6bad4b75852c2e2a471a3800d5efdca7.tar.bz2 | |
bpo-46063: Improve algorithm for computing which rolled-over log file⦠(GH-30093)
Diffstat (limited to 'Lib/test/test_logging.py')
| -rw-r--r-- | Lib/test/test_logging.py | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 85b6e5f..c85d26e 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1,4 +1,4 @@ -# Copyright 2001-2019 by Vinay Sajip. All Rights Reserved. +# Copyright 2001-2021 by Vinay Sajip. All Rights Reserved. # # Permission to use, copy, modify, and distribute this software and its # documentation for any purpose and without fee is hereby granted, @@ -16,7 +16,7 @@ """Test harness for the logging module. Run all tests. -Copyright (C) 2001-2019 Vinay Sajip. All Rights Reserved. +Copyright (C) 2001-2021 Vinay Sajip. All Rights Reserved. """ import logging @@ -36,6 +36,7 @@ import os import queue import random import re +import shutil import socket import struct import sys @@ -5434,6 +5435,53 @@ class TimedRotatingFileHandlerTest(BaseFileTest): finally: rh.close() + def test_compute_files_to_delete(self): + # See bpo-46063 for background + wd = tempfile.mkdtemp(prefix='test_logging_') + self.addCleanup(shutil.rmtree, wd) + times = [] + dt = datetime.datetime.now() + for i in range(10): + times.append(dt.strftime('%Y-%m-%d_%H-%M-%S')) + dt += datetime.timedelta(seconds=5) + prefixes = ('a.b', 'a.b.c', 'd.e', 'd.e.f') + files = [] + rotators = [] + for prefix in prefixes: + p = os.path.join(wd, '%s.log' % prefix) + rotator = logging.handlers.TimedRotatingFileHandler(p, when='s', + interval=5, + backupCount=7) + rotators.append(rotator) + if prefix.startswith('a.b'): + for t in times: + files.append('%s.log.%s' % (prefix, t)) + else: + rotator.namer = lambda name: name.replace('.log', '') + '.log' + for t in times: + files.append('%s.%s.log' % (prefix, t)) + # Create empty files + for fn in files: + p = os.path.join(wd, fn) + with open(p, 'wb') as f: + pass + # Now the checks that only the correct files are offered up for deletion + for i, prefix in enumerate(prefixes): + rotator = rotators[i] + candidates = rotator.getFilesToDelete() + self.assertEqual(len(candidates), 3) + if prefix.startswith('a.b'): + p = '%s.log.' % prefix + for c in candidates: + d, fn = os.path.split(c) + self.assertTrue(fn.startswith(p)) + else: + for c in candidates: + d, fn = os.path.split(c) + self.assertTrue(fn.endswith('.log')) + self.assertTrue(fn.startswith(prefix + '.') and + fn[len(prefix) + 2].isdigit()) + def secs(**kw): return datetime.timedelta(**kw) // datetime.timedelta(seconds=1) |
