示例#1
0
    def test_time_with_1(self):

        #This test is somewhat time dependent.
        #I don't think it should ever fail, but if it does
        #try running it again and see if it works that time.

        sink = ListSink()
        logger = BasicLogger(sink)
        logs = sink.items

        with logger.time('a'):
            logger.log('c')
            time.sleep(0.15)
            logger.log('d')
        logger.log('e')

        self.assertEqual(5, len(logs))

        self.assertEqual(logs[0], 'a')
        self.assertEqual(logs[1], 'c')
        self.assertEqual(logs[2], 'd')
        self.assertRegex(logs[3],
                         '^a \\(\\d+\\.\\d+ seconds\\) \\(completed\\)$')
        self.assertEqual(logs[4], 'e')

        self.assertAlmostEqual(float(logs[3][3:7]), 0.15, 1)
示例#2
0
    def test_time_two_separate(self):

        #This test is somewhat time dependent.
        #I don't think it should ever fail, but if it does
        #try running it again and see if it works that time.

        sink = ListSink()
        logger = BasicLogger(sink)
        logs = sink.items

        with logger.time('a'):
            time.sleep(0.05)
            with logger.time('d'):
                logger.log('e')
                time.sleep(0.05)

        logger.log('g')

        with logger.time('a'):
            time.sleep(0.05)
            with logger.time('d'):
                logger.log('e')
                time.sleep(0.05)

        self.assertEqual(11, len(logs))

        self.assertEqual(logs[0], 'a')
        self.assertEqual(logs[1], 'd')
        self.assertEqual(logs[2], 'e')
        self.assertRegex(logs[3], 'd \\(\\d+\\.\\d+ seconds\\)')
        self.assertRegex(logs[4], 'a \\(\\d+\\.\\d+ seconds\\)')
        self.assertEqual(logs[5], 'g')
        self.assertEqual(logs[6], 'a')
        self.assertEqual(logs[7], 'd')
        self.assertEqual(logs[8], 'e')
        self.assertRegex(logs[9], 'd \\(\\d+\\.\\d+ seconds\\)')
        self.assertRegex(logs[10], 'a \\(\\d+\\.\\d+ seconds\\)')

        self.assertAlmostEqual(float(logs[3][3:7]), 0.05, 1)
        self.assertAlmostEqual(float(logs[4][3:7]), 0.10, 1)
        self.assertAlmostEqual(float(logs[9][3:7]), 0.05, 1)
        self.assertAlmostEqual(float(logs[10][3:7]), 0.10, 1)
示例#3
0
    def test_time_with_interrupt(self):

        sink = ListSink()
        logger = BasicLogger(sink)
        logs = sink.items

        with self.assertRaises(BaseException) as e:
            with logger.time('a'):
                logger.log('c')
                logger.log('d')
                raise KeyboardInterrupt()

        self.assertEqual(4, len(logs))

        self.assertEqual(logs[0], 'a')
        self.assertEqual(logs[1], 'c')
        self.assertEqual(logs[2], 'd')
        self.assertRegex(logs[3],
                         '^a \\(\\d+\\.\\d+ seconds\\) \\(interrupt\\)$')