MCPcopy Index your code
hub / github.com/python/cpython / test_rollover

Method test_rollover

Lib/test/test_logging.py:6564–6604  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

6562
6563 # other test methods added below
6564 def test_rollover(self):
6565 fh = logging.handlers.TimedRotatingFileHandler(
6566 self.fn, 'S', encoding="utf-8", backupCount=1)
6567 fmt = logging.Formatter('%(asctime)s %(message)s')
6568 fh.setFormatter(fmt)
6569 r1 = logging.makeLogRecord({'msg': 'testing - initial'})
6570 fh.emit(r1)
6571 self.assertLogFile(self.fn)
6572 time.sleep(1.1) # a little over a second ...
6573 r2 = logging.makeLogRecord({'msg': 'testing - after delay'})
6574 fh.emit(r2)
6575 fh.close()
6576 # At this point, we should have a recent rotated file which we
6577 # can test for the existence of. However, in practice, on some
6578 # machines which run really slowly, we don't know how far back
6579 # in time to go to look for the log file. So, we go back a fair
6580 # bit, and stop as soon as we see a rotated file. In theory this
6581 # could of course still fail, but the chances are lower.
6582 found = False
6583 now = datetime.datetime.now()
6584 GO_BACK = 5 * 60 # seconds
6585 for secs in range(GO_BACK):
6586 prev = now - datetime.timedelta(seconds=secs)
6587 fn = self.fn + prev.strftime(".%Y-%m-%d_%H-%M-%S")
6588 found = os.path.exists(fn)
6589 if found:
6590 self.rmfiles.append(fn)
6591 break
6592 msg = 'No rotated files found, went back %d seconds' % GO_BACK
6593 if not found:
6594 # print additional diagnostics
6595 dn, fn = os.path.split(self.fn)
6596 files = [f for f in os.listdir(dn) if f.startswith(fn)]
6597 print('Test time: %s' % now.strftime("%Y-%m-%d %H-%M-%S"), file=sys.stderr)
6598 print('The only matching files are: %s' % files, file=sys.stderr)
6599 for f in files:
6600 print('Contents of %s:' % f)
6601 path = os.path.join(dn, f)
6602 with open(path, 'r') as tf:
6603 print(tf.read())
6604 self.assertTrue(found, msg=msg)
6605
6606 def test_rollover_at_midnight(self, weekly=False):
6607 os_helper.unlink(self.fn)

Callers

nothing calls this directly

Calls 15

setFormatterMethod · 0.80
assertLogFileMethod · 0.80
nowMethod · 0.80
listdirMethod · 0.80
assertTrueMethod · 0.80
openFunction · 0.50
emitMethod · 0.45
sleepMethod · 0.45
closeMethod · 0.45
strftimeMethod · 0.45
existsMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected