def test_print_timings_correct(self): """Test timing is about right """ buf = io.StringIO() # Nothing should be logged yet self.assertEqual(len(self.log_stream.getvalue()), 0) log.initialize() # Still, nothing should be logged yet self.assertEqual(len(self.log_stream.getvalue()), 0) for _ in range(3): with log.timed("Foo", level=logging.FATAL): time.sleep(0.01) with stdout_redirector(buf): log.print_timings() # Should print correct stats self.assertRegexpMatches(buf.getvalue(), r'\'Foo\'') self.assertRegexpMatches(buf.getvalue(), r'3 times') self.assertRegexpMatches(buf.getvalue(), r'total = 0.03') self.assertRegexpMatches(buf.getvalue(), r'median = 0.01') log.close() # Correct timings should be logged three times self.assertRegexpMatches(self.log_stream.getvalue(), r'Foo') self.assertEqual(len(re.findall(r'Foo', self.log_stream.getvalue())), 3) self.assertRegexpMatches(self.log_stream.getvalue(), r'took 0.01') self.assertEqual(len(re.findall(r'took 0.01', self.log_stream.getvalue())), 3)
def test_timing_logger_logs(self): """Test timing code logs a message """ # Nothing should be logged yet self.assertEqual(len(self.log_stream.getvalue()), 0) log.initialize() # Still, nothing should be logged yet self.assertEqual(len(self.log_stream.getvalue()), 0) with log.timed(level=logging.FATAL): _ = 1 + 1 log.close() # Something should be logged self.assertNotEqual(len(self.log_stream.getvalue()), 0)
def test_print_timings_prints(self): """Test timing code and printing really prints a message """ buf = io.StringIO() # Nothing should be logged yet self.assertEqual(len(self.log_stream.getvalue()), 0) log.initialize() # Still, nothing should be logged yet self.assertEqual(len(self.log_stream.getvalue()), 0) with log.timed(level=logging.FATAL): _ = 1 + 1 with stdout_redirector(buf): log.print_timings() # Something should be printed self.assertNotEqual(len(buf.getvalue()), 0) log.close() # Something should be logged self.assertNotEqual(len(self.log_stream.getvalue()), 0)
def hello(): """Logs a message """ with log.timed("bla"): pass