Exemplo n.º 1
0
class ActorLoggingTest(object):
    def setUp(self):
        self.on_stop_was_called = self.event_class()
        self.on_failure_was_called = self.event_class()

        self.actor_ref = self.AnActor.start(self.on_stop_was_called,
            self.on_failure_was_called)
        self.actor_proxy = self.actor_ref.proxy()

        self.log_handler = TestLogHandler(logging.DEBUG)
        self.root_logger = logging.getLogger()
        self.root_logger.addHandler(self.log_handler)

    def tearDown(self):
        self.log_handler.close()
        ActorRegistry.stop_all()

    def test_unexpected_messages_are_logged(self):
        self.actor_ref.ask({'unhandled': 'message'})
        self.assertEqual(1, len(self.log_handler.messages['warning']))
        log_record = self.log_handler.messages['warning'][0]
        self.assertEqual('Unexpected message received by %s' % self.actor_ref,
            log_record.getMessage().split(': ')[0])

    def test_exception_is_logged_when_returned_to_caller(self):
        try:
            self.actor_proxy.raise_exception().get()
            self.fail('Should raise exception')
        except Exception:
            pass
        self.assertEqual(1, len(self.log_handler.messages['debug']))
        log_record = self.log_handler.messages['debug'][0]
        self.assertEqual('Exception returned from %s to caller:' %
            self.actor_ref, log_record.getMessage())
        self.assertEqual(Exception, log_record.exc_info[0])
        self.assertEqual('foo', str(log_record.exc_info[1]))

    def test_exception_is_logged_when_not_reply_requested(self):
        self.on_failure_was_called.clear()
        self.actor_ref.tell({'command': 'raise exception'})
        self.on_failure_was_called.wait(5)
        self.assertTrue(self.on_failure_was_called.is_set())
        self.assertEqual(1, len(self.log_handler.messages['error']))
        log_record = self.log_handler.messages['error'][0]
        self.assertEqual('Unhandled exception in %s:' % self.actor_ref,
            log_record.getMessage())
        self.assertEqual(Exception, log_record.exc_info[0])
        self.assertEqual('foo', str(log_record.exc_info[1]))

    def test_base_exception_is_logged(self):
        self.log_handler.reset()
        self.on_stop_was_called.clear()
        self.actor_ref.tell({'command': 'raise base exception'})
        self.on_stop_was_called.wait(5)
        self.assertTrue(self.on_stop_was_called.is_set())
        self.assertEqual(3, len(self.log_handler.messages['debug']))
        log_record = self.log_handler.messages['debug'][0]
        self.assertEqual('BaseException() in %s. Stopping all actors.'
            % self.actor_ref, log_record.getMessage())
Exemplo n.º 2
0
    def setUp(self):
        self.on_stop_was_called = self.event_class()
        self.on_failure_was_called = self.event_class()

        self.actor_ref = self.AnActor.start(self.on_stop_was_called,
                                            self.on_failure_was_called)
        self.actor_proxy = self.actor_ref.proxy()

        self.log_handler = TestLogHandler(logging.DEBUG)
        self.root_logger = logging.getLogger()
        self.root_logger.addHandler(self.log_handler)
Exemplo n.º 3
0
    def setUp(self):
        self.on_stop_was_called = self.event_class()
        self.on_failure_was_called = self.event_class()

        self.actor_ref = self.AnActor.start(
            self.on_stop_was_called, self.on_failure_was_called)
        self.actor_proxy = self.actor_ref.proxy()

        self.log_handler = TestLogHandler(logging.DEBUG)
        self.root_logger = logging.getLogger()
        self.root_logger.addHandler(self.log_handler)
Exemplo n.º 4
0
class ActorLoggingTest(object):
    def setUp(self):
        self.on_stop_was_called = self.event_class()
        self.on_failure_was_called = self.event_class()

        self.actor_ref = self.AnActor.start(self.on_stop_was_called,
                                            self.on_failure_was_called)
        self.actor_proxy = self.actor_ref.proxy()

        self.log_handler = TestLogHandler(logging.DEBUG)
        self.root_logger = logging.getLogger()
        self.root_logger.addHandler(self.log_handler)

    def tearDown(self):
        self.log_handler.close()
        ActorRegistry.stop_all()

    def test_unexpected_messages_are_logged(self):
        self.actor_ref.ask({'unhandled': 'message'})
        self.assertEqual(1, len(self.log_handler.messages['warning']))
        log_record = self.log_handler.messages['warning'][0]
        self.assertEqual('Unexpected message received by %s' % self.actor_ref,
                         log_record.getMessage().split(': ')[0])

    def test_exception_is_logged_when_returned_to_caller(self):
        # pylint: disable = W0703
        try:
            self.actor_proxy.raise_exception().get()
            self.fail('Should raise exception')
        except Exception:
            pass
        # pylint: enable = W0703
        self.assertEqual(1, len(self.log_handler.messages['debug']))
        log_record = self.log_handler.messages['debug'][0]
        self.assertEqual(
            'Exception returned from %s to caller:' % self.actor_ref,
            log_record.getMessage())
        self.assertEqual(Exception, log_record.exc_info[0])
        self.assertEqual('foo', str(log_record.exc_info[1]))

    def test_exception_is_logged_when_not_reply_requested(self):
        self.on_failure_was_called.clear()
        self.actor_ref.tell({'command': 'raise exception'})
        self.on_failure_was_called.wait(5)
        self.assertTrue(self.on_failure_was_called.is_set())
        self.assertEqual(1, len(self.log_handler.messages['error']))
        log_record = self.log_handler.messages['error'][0]
        self.assertEqual('Unhandled exception in %s:' % self.actor_ref,
                         log_record.getMessage())
        self.assertEqual(Exception, log_record.exc_info[0])
        self.assertEqual('foo', str(log_record.exc_info[1]))

    def test_base_exception_is_logged(self):
        self.log_handler.reset()
        self.on_stop_was_called.clear()
        self.actor_ref.tell({'command': 'raise base exception'})
        self.on_stop_was_called.wait(5)
        self.assertTrue(self.on_stop_was_called.is_set())
        self.assertEqual(3, len(self.log_handler.messages['debug']))
        log_record = self.log_handler.messages['debug'][0]
        self.assertEqual(
            'BaseException() in %s. Stopping all actors.' % self.actor_ref,
            log_record.getMessage())

    def test_exception_in_on_start_is_logged(self):
        self.log_handler.reset()
        start_event = self.event_class()
        actor_ref = self.EarlyFailingActor.start(start_event)
        start_event.wait(5)
        time.sleep(0.01)  # Too ensure that the log handler is updated
        self.assertEqual(1, len(self.log_handler.messages['error']))
        log_record = self.log_handler.messages['error'][0]
        self.assertEqual('Unhandled exception in %s:' % actor_ref,
                         log_record.getMessage())

    def test_exception_in_on_stop_is_logged(self):
        self.log_handler.reset()
        stop_event = self.event_class()
        actor_ref = self.LateFailingActor.start(stop_event)
        stop_event.wait(5)
        time.sleep(0.01)  # Too ensure that the log handler is updated
        self.assertEqual(1, len(self.log_handler.messages['error']))
        log_record = self.log_handler.messages['error'][0]
        self.assertEqual('Unhandled exception in %s:' % actor_ref,
                         log_record.getMessage())

    def test_exception_in_on_failure_is_logged(self):
        self.log_handler.reset()
        failure_event = self.event_class()
        actor_ref = self.FailingOnFailureActor.start(failure_event)
        actor_ref.tell({'command': 'raise exception'})
        failure_event.wait(5)
        time.sleep(0.01)  # Too ensure that the log handler is updated
        self.assertEqual(2, len(self.log_handler.messages['error']))
        log_record = self.log_handler.messages['error'][0]
        self.assertEqual('Unhandled exception in %s:' % actor_ref,
                         log_record.getMessage())