def test_pre_post_decorators(self): pre_decorators = [LogDecorator(1)] post_decorators = [LogDecorator(2)] sink = ListSink() logger = DecoratedLogger(pre_decorators, BasicLogger(sink), post_decorators) logger.log('a') self.assertEqual(['2 -- 1 -- a'], sink.items) sink = ListSink() logger = DecoratedLogger(pre_decorators, BasicLogger(sink), post_decorators) with logger.log('a'): pass self.assertEqual(['2 -- 1 -- a', '2 -- 1 -- a (completed)'], sink.items) sink = ListSink() logger = DecoratedLogger(pre_decorators, BasicLogger(sink), post_decorators) with logger.time('a'): pass self.assertEqual( ['2 -- 1 -- a', '2 -- 1 -- a (0.0 seconds) (completed)'], sink.items)
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)
def test_log_with_interrupt(self): sink = ListSink() logger = BasicLogger(sink) logs = sink.items with self.assertRaises(BaseException) as e: with logger.log('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.assertEqual(logs[3], 'a (interrupt)')
def test_log_with_exception(self): sink = ListSink() logger = BasicLogger(sink) logs = sink.items with self.assertRaises(Exception): with logger.log('a'): logger.log('c') logger.log('d') raise Exception() self.assertEqual(4, len(logs)) self.assertEqual(logs[0], 'a') self.assertEqual(logs[1], 'c') self.assertEqual(logs[2], 'd') self.assertEqual(logs[3], 'a (exception)')
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\\)$')
def test_not_picklable_logging(self): logger_sink = ListSink() CobaContext.logger = BasicLogger(logger_sink) CobaContext.cacher = NullCacher() list( CobaMultiprocessor(ProcessNameFilter(), 2, 1).filter([lambda a: 1])) self.assertEqual(1, len(logger_sink.items)) self.assertIn("pickle", logger_sink.items[0])
def test_filter_exception_logging(self): CobaContext.logger = DecoratedLogger([ExceptLog()], BasicLogger(ListSink()), []) CobaContext.cacher = NullCacher() list(CobaMultiprocessor(ExceptionFilter(), 2, 1).filter(range(4))) self.assertEqual(4, len(CobaContext.logger.sink.items)) self.assertIn("Exception Filter", CobaContext.logger.sink.items[0]) self.assertIn("Exception Filter", CobaContext.logger.sink.items[1]) self.assertIn("Exception Filter", CobaContext.logger.sink.items[2]) self.assertIn("Exception Filter", CobaContext.logger.sink.items[3])
def test_wrapped_not_picklable_learner_sans_reduce(self): sim1 = LambdaSimulation(5, lambda i: i, lambda i, c: [0, 1, 2], lambda i, c, a: cast(float, a)) learner = WrappedLearner(NotPicklableLearner()) experiment = Experiment([sim1], [learner]) CobaContext.logger = BasicLogger(ListSink()) experiment.evaluate() self.assertEqual(1, len(CobaContext.logger.sink.items)) self.assertIn("pickle", CobaContext.logger.sink.items[0])
def test_read_exception_logging(self): CobaContext.logger = DecoratedLogger([ExceptLog()], BasicLogger(ListSink()), []) CobaContext.cacher = NullCacher() def broken_generator(): yield [1] raise Exception("Generator Exception") list(CobaMultiprocessor(Identity(), 2, 1).filter(broken_generator())) self.assertEqual(1, len(CobaContext.logger.sink.items)) self.assertIn("Generator Exception", CobaContext.logger.sink.items[0])
def test_sink(self): pre_decorators = [LogDecorator(1)] post_decorators = [LogDecorator(2)] sink = ListSink() logger = DecoratedLogger(pre_decorators, BasicLogger(sink), post_decorators) self.assertIs(logger.sink, sink) sink = ListSink() logger.sink = sink self.assertIs(logger.sink, sink) logger.log('a') self.assertEqual(['2 -- 1 -- a'], sink.items)
def test_log(self): sink = ListSink() logger = BasicLogger(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')
def test_log_with(self): sink = ListSink() logger = BasicLogger(sink) logs = sink.items with logger.log('a'): logger.log('c') 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.assertEqual(logs[3], 'a (completed)') self.assertEqual(logs[4], 'e')
def test_sink_is_set(self): logger = BasicLogger(ListSink()) self.assertIsInstance(logger.sink, ListSink) logger.sink = NullSink() self.assertIsInstance(logger.sink, NullSink)
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)