Exemplo n.º 1
0
def after_request(response):
    session = Client().session()
    if response.status_code not in xrange(200, 399):
        coordinated_rollback(g.queued_tasks)
        return response

    coordinated_commit(g.queued_tasks)

    return response
    def test_revokes_tasks_and_rolls_back_in_the_happy_case(self):
        session = Client().session()
        task = Mock()
        with session_cleanup():
            session.rollback = Mock()

            coordinated_rollback([task])

            session.rollback.assert_called_once_with()
            task.revoke.assert_called_once_with()
    def test_revokes_tasks_on_connection_error(self):
        session = Client().session()
        task = Mock()
        with session_cleanup():
            session.rollback = Mock()
            session.rollback.side_effect = \
                    OperationalError(None, None, 'database timeout')

            assert_raises(OperationalError,
                          lambda: coordinated_rollback([task]))

            task.revoke.assert_called_once_with()
    def test_sky_is_falling_if_database_and_redis_are_unreachable(self):
        session = Client().session()
        task = Mock()
        task.revoke.side_effect = TimeoutError('go sit in the corner')
        with session_cleanup():
            session.rollback = Mock()
            session.rollback.side_effect = \
                    OperationalError(None, None, b'database timeout')

            assert_raises(SkyIsFallingError,
                          lambda: coordinated_rollback([task]))

            task.revoke.assert_called_once_with()