Example #1
0
    def test_time_with_exception(self):

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

        try:
            with self.assertRaises(Exception) as e:
                with logger.time('a'):
                    logger.log('c')
                    logger.log('d')
                    raise Exception()
        except:
            pass

        self.assertEqual(3, len(logs))
        self.assertRegex(logs[0],
                         '^a \\(\\d+\\.\\d+ seconds\\) \\(exception\\)$')
        self.assertEqual(logs[1], '  * c')
        self.assertEqual(logs[2], '  * d')
Example #2
0
    def test_exception_logged_but_not_thrown(self):
        log_sink = ListSink()

        CobaContext.logger = NullLogger()
        CobaContext.cacher = NullCacher()
        CobaContext.store = None

        logger = DecoratedLogger([ExceptLog()], IndentLogger(), [])

        CobaMultiprocessor.ProcessFilter(ExceptionFilter(), logger, None, None,
                                         log_sink).filter(1)

        self.assertIn('Exception Filter', log_sink.items[0])
Example #3
0
    def test_time_with_interrupt(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 = IndentLogger(sink)
        logs = sink.items

        try:
            with self.assertRaises(Exception) as e:
                with logger.time('a'):
                    logger.log('c')
                    logger.log('d')
                    raise KeyboardInterrupt()
        except:
            pass

        self.assertEqual(3, len(logs))
        self.assertRegex(logs[0],
                         '^a \\(\\d+\\.\\d+ seconds\\) \\(interrupt\\)$')
        self.assertEqual(logs[1], '  * c')
        self.assertEqual(logs[2], '  * d')
Example #4
0
    def test_logging(self):
        logger_sink = ListSink()
        logger = DecoratedLogger([], IndentLogger(logger_sink),
                                 [NameLog(), StampLog()])

        CobaContext.logger = logger
        CobaContext.cacher = NullCacher()

        items = list(
            CobaMultiprocessor(ProcessNameFilter(), 2, 1).filter(range(4)))

        self.assertEqual(len(logger_sink.items), 4)
        self.assertCountEqual(items,
                              [l.split(' ')[3] for l in logger_sink.items])
        self.assertCountEqual(items,
                              [l.split(' ')[-1] for l in logger_sink.items])
Example #5
0
    def test_log_with_1(self):

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

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

        self.assertEqual(4, len(logs))
        self.assertEqual(logs[0], 'a')
        self.assertEqual(logs[1], '  * c')
        self.assertEqual(logs[2], '  * d')
        self.assertEqual(logs[3], 'e')
Example #6
0
    def test_log(self):

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

        logger.log('a')
        logger.log('c')
        logger.log('d')

        self.assertEqual(logs[0], 'a')
        self.assertEqual(logs[1], 'c')
        self.assertEqual(logs[2], 'd')
Example #7
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 = IndentLogger(sink)
        logs = sink.items

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

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

        self.assertAlmostEqual(float(logs[0][3:7]), 0.15, 1)
Example #8
0
    def test_coba_config_set_correctly(self):

        log_sink = ListSink()

        CobaContext.logger = NullLogger()
        CobaContext.cacher = NullCacher()
        CobaContext.store = None

        filter = CobaMultiprocessor.ProcessFilter(Identity(), IndentLogger(),
                                                  MemoryCacher(), {}, log_sink)

        self.assertIsInstance(CobaContext.logger, NullLogger)
        self.assertIsInstance(CobaContext.cacher, NullCacher)
        self.assertIsNone(CobaContext.store)

        filter.filter(1)

        self.assertIsInstance(CobaContext.logger, IndentLogger)
        self.assertIsInstance(CobaContext.cacher, MemoryCacher)
        self.assertIsInstance(CobaContext.store, dict)
        self.assertIsInstance(CobaContext.logger.sink, ListSink)
Example #9
0
    def test_ignore_raise(self):

        CobaContext.logger = IndentLogger(ListSink())

        sim1 = LambdaSimulation(2, lambda i: i, lambda i, c: [0, 1, 2],
                                lambda i, c, a: cast(float, a))
        sim2 = LambdaSimulation(3, lambda i: i, lambda i, c: [3, 4, 5],
                                lambda i, c, a: cast(float, a))
        experiment = Experiment(
            [sim1, sim2], [ModuloLearner(), BrokenLearner()],
            evaluation_task=OnlineOnPolicyEvalTask(False))

        result = experiment.evaluate()
        actual_learners = result.learners.to_tuples()
        actual_environments = result.environments.to_tuples()
        actual_interactions = result.interactions.to_tuples()

        expected_learners = [(0, "Modulo", "Modulo(p=0)", '0'),
                             (1, "Broken", "Broken", float('nan'))]
        expected_environments = [(0, 'LambdaSimulation'),
                                 (1, 'LambdaSimulation')]
        expected_interactions = [(0, 0, 1, 0), (0, 0, 2, 1), (1, 0, 1, 3),
                                 (1, 0, 2, 4), (1, 0, 3, 5)]

        self.assertIsInstance(CobaContext.logger, IndentLogger)
        self.assertEqual(
            2,
            sum([
                int("Unexpected exception:" in item)
                for item in CobaContext.logger.sink.items
            ]))

        self.assertCountEqual(actual_learners[0], expected_learners[0])
        self.assertCountEqual(actual_learners[1][:3], expected_learners[1][:3])
        self.assertTrue(math.isnan(expected_learners[1][3]))

        self.assertCountEqual(actual_environments, expected_environments)
        self.assertCountEqual(actual_interactions, expected_interactions)
Example #10
0
 def test_sink_is_set(self):
     logger = IndentLogger(ListSink())
     self.assertIsInstance(logger.sink, ListSink)
     logger.sink = NullSink()
     self.assertIsInstance(logger.sink, NullSink)
Example #11
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 = IndentLogger(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(7, len(logs))
        self.assertRegex(logs[0], 'a \\(\\d+\\.\\d+ seconds\\)')
        self.assertRegex(logs[1], '  \\* d \\(\\d+\\.\\d+ seconds\\)')
        self.assertEqual(logs[2], '    > e')
        self.assertEqual(logs[3], 'g')
        self.assertRegex(logs[4], 'a \\(\\d+\\.\\d+ seconds\\)')
        self.assertRegex(logs[5], '  \\* d \\(\\d+\\.\\d+ seconds\\)')
        self.assertEqual(logs[6], '    > e')

        self.assertAlmostEqual(float(logs[0][3:7]), 0.10, 1)
        self.assertAlmostEqual(float(logs[1][7:11]), 0.05, 1)
        self.assertAlmostEqual(float(logs[4][3:7]), 0.10, 1)
        self.assertAlmostEqual(float(logs[5][7:11]), 0.05, 1)