Ejemplo n.º 1
0
class CommandProcessorFixture(unittest.TestCase):

    """ Command Processor tests """

    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)

    def test_handle_command(self):
        """ given that we have a handler registered for a command, when we send a command, it should call the handler"""
        self._handler = MyCommandHandler()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)
        self._commandProcessor.send(self._request)

        self.assertTrue(self._handler.called, "Expected the handle method on the handler to be called with the message")

    def test_handle_event(self):
        """ Given that we have many handlers registered of an event, when we raise an event, it should call all the handlers"""

        self._handler = MyEventHandler()
        self._other_handler = MyEventHandler()
        self._request = MyEvent()
        self._subscriber_registry.register(MyEvent, lambda: self._handler)
        self._subscriber_registry.register(MyEvent, lambda: self._other_handler)
        self._commandProcessor.publish(self._request)

        self.assertTrue(self._handler.called, "The first handler should be called with the message")
        self.assertTrue((self._other_handler, "The second handler should also be called with the message"))

    def test_missing_command_handler_registration(self):
        """Given that we are missing a handler for a command, when we send a command, it should throw an exception"""

        self._handler = MyCommandHandler()
        self._request = MyCommand()

        exception_thrown = False
        try:
            self._commandProcessor.send(self._request)
        except:
            exception_thrown = True

        self.assertTrue(exception_thrown, "Expected an exception to be thrown when no handler is registered for a command")

    def test_missing_event_handler_registration(self):
        """Given that we have no handlers register for an event, when we raise an event, it should not error """

        self._handler = MyEventHandler()
        self._other_handler = MyEventHandler()
        self._request = MyEvent()

        exception_thrown = False
        try:
            self._commandProcessor.publish(self._request)
        except:
            exception_thrown = True

        self.assertFalse(exception_thrown, "Did not expect an exception to be thrown where there are no handlers for an event")
Ejemplo n.º 2
0
class LoggingAndMonitoringFixture(unittest.TestCase):

    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)

    def test_logging_a_handler(self):
        """s
        Given that I have a handler decorated for logging
        When I call that handler
        Then I should receive logs indicating the call and return of the handler
        * N.b. This is an example of using decorators to extend the Brightside pipeline
        """
        handler = MyCommandHandler()
        request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: handler)
        logger = logging.getLogger("tests.handlers_testdoubles")
        with patch.object(logger, 'log') as mock_log:
            self._commandProcessor.send(request)

        mock_log.assert_has_calls([call(logging.DEBUG, "Entering handle " + str(request)),
                                   call(logging.DEBUG, "Exiting handle " + str(request))])
class PipelineTests(unittest.TestCase):
    """Tests of decorators on handlers.
        It is worth noting that whereas Brighter has to create a mechanism to allow decorators on the target handler, Python
        already has a decorator syntax. As this maps well to the attributes used by Brighter to signal the decorators
        used to meet orthogonal concerns it is natural to just use Python's own decorator syntax in Brightside, over
        rolling up a bespoke mechanism. Indeed, the use of attributes and decorator classes in Brighter mirrors functionality avaailable
        to Pythoh via its decorator syntax
        This test suite is really about proving we can provide equivalent functionality
        We use Benjamin Hodgoson's Poll library, which is itself inspired by Polly, the library used by Brighter
    """
    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(
            registry=self._subscriber_registry)

    def test_handle_retry_on_command(self):
        """ given that we have a retry plocy for a command, when we raise an exception, then we should retry n times or until succeeds """
        self._handler = MyHandlerSupportingRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)
        self._commandProcessor.send(self._request)

        self.assertTrue(
            self._handler.called,
            "Expected the handle method on the handler to be called with the message"
        )
        self.assertTrue(self._handler.call_count == 3,
                        "Expected two retries of the pipeline")

    def test_exceed_retry_on_command(self):
        """ given that we have a retry policy for a command, when we raise an exception, then we should bubble the exception out after n retries"""
        self._handler = MyHandlerBreakingAfterRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        self.assertTrue(
            exception_raised,
            "Exepcted the exception to bubble out, when we run out of retries")
        self.assertFalse(
            self._handler.called,
            "Did not expect the handle method on the handler to be called with the message"
        )
        self.assertTrue(self._handler.call_count == 3,
                        "Expected two retries of the pipeline")

    def test_handle_circuit_breaker_on_command(self):
        """ given that we have a circuit breaker policy for a command, when we raise an exception, then we should break the circuit after n retries"""
        self._handler = MyHandlerBreakingCircuitAfterThreeFailures()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        # Now see if the circuit is broken following the failed call
        circuit_broken = False
        try:
            self._commandProcessor.send(self._request)
        except CircuitBrokenError:
            circuit_broken = True

        self.assertTrue(
            exception_raised,
            "Exepcted an exception to be raised, when we run out of retries")
        self.assertTrue(circuit_broken,
                        "Expected the circuit to be broken to further calls")
        self.assertFalse(
            self._handler.called,
            "Did not expect the handle method on the handler to be called with the message"
        )
        self.assertTrue(self._handler.call_count == 3,
                        "Expected two retries of the pipeline")
class PipelineTests(unittest.TestCase):

    """Tests of decorators on handlers.
        It is worth noting that whereas Brighter has to create a mechanism to allow decorators on the target handler, Python
        already has a decorator syntax. As this maps well to the attributes used by Brighter to signal the decorators
        used to meet orthogonal concerns it is natural to just use Python's own decorator syntax in Brightside, over
        rolling up a bespoke mechanism. Indeed, the use of attributes and decorator classes in Brighter mirrors functionality avaailable
        to Pythoh via its decorator syntax
        This test suite is really about proving we can provide equivalent functionality
        We use Benjamin Hodgoson's Poll library, which is itself inspired by Polly, the library used by Brighter
    """

    def setUp(self):
        self._subscriber_registry = Registry()
        self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)

    def test_handle_retry_on_command(self):
        """ given that we have a retry plocy for a command, when we raise an exception, then we should retry n times or until succeeds """
        self._handler = MyHandlerSupportingRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)
        self._commandProcessor.send(self._request)

        self.assertTrue(self._handler.called, "Expected the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")

    def test_exceed_retry_on_command(self):
        """ given that we have a retry policy for a command, when we raise an exception, then we should bubble the exception out after n retries"""
        self._handler = MyHandlerBreakingAfterRetry()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        self.assertTrue(exception_raised, "Exepcted the exception to bubble out, when we run out of retries")
        self.assertFalse(self._handler.called, "Did not expect the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")

    def test_handle_circuit_breaker_on_command(self):
        """ given that we have a circuit breaker policy for a command, when we raise an exception, then we should break the circuit after n retries"""
        self._handler = MyHandlerBreakingCircuitAfterThreeFailures()
        self._request = MyCommand()
        self._subscriber_registry.register(MyCommand, lambda: self._handler)

        exception_raised = False
        try:
            self._commandProcessor.send(self._request)
        except RuntimeError:
            exception_raised = True

        # Now see if the circuit is broken following the failed call
        circuit_broken = False
        try:
            self._commandProcessor.send(self._request)
        except CircuitBrokenError:
            circuit_broken = True

        self.assertTrue(exception_raised, "Exepcted an exception to be raised, when we run out of retries")
        self.assertTrue(circuit_broken, "Expected the circuit to be broken to further calls")
        self.assertFalse(self._handler.called, "Did not expect the handle method on the handler to be called with the message")
        self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")