def test_run_forever(event_loop, test_consumer_with_abort):
    """Test Application.run_forever."""
    startup_called = False
    preprocess_called = False
    callback_called = False
    postprocess_called = False
    acknowledgement_called = False
    teardown_called = False

    @asyncio.coroutine
    def callback(app, message):
        nonlocal callback_called
        callback_called = True
        return [message + 1]

    app = Application(
        'testing',
        consumer=test_consumer_with_abort,
        callback=callback,
    )

    @app.startup
    @asyncio.coroutine
    def startup(app):
        nonlocal startup_called
        startup_called = True

    @app.message_preprocessor
    @asyncio.coroutine
    def preprocess(app, message):
        nonlocal preprocess_called
        preprocess_called = True
        return message + 1

    @app.result_postprocessor
    @asyncio.coroutine
    def postprocess(app, result):
        nonlocal postprocess_called
        postprocess_called = True

    @app.message_acknowledgement
    @asyncio.coroutine
    def acknowledge(app, message):
        nonlocal acknowledgement_called
        acknowledgement_called = True

    @app.teardown
    @asyncio.coroutine
    def teardown(app):
        nonlocal teardown_called
        teardown_called = True

    app.run_forever(loop=event_loop)

    assert startup_called
    assert preprocess_called
    assert callback_called
    assert postprocess_called
    assert acknowledgement_called
    assert teardown_called
Exemple #2
0
def test_consumer_aborts(event_loop, ASYNC_QUEUE):
    """Test that the application stops after the consumer aborts."""
    consumer_called = False
    callback_called = False

    class Consumer:
        async def read(self):
            nonlocal consumer_called
            consumer_called = True
            raise Abort('reason', 'message')

    async def callback(app, message):
        nonlocal callback_called
        callback_called = True
        while True:
            await asyncio.sleep(0)

    app = Application('testing',
                      consumer=Consumer(),
                      callback=callback,
                      settings={'ASYNC_QUEUE': ASYNC_QUEUE})
    app.run_forever(loop=event_loop)

    assert consumer_called
    assert not callback_called
Exemple #3
0
def test_consumer_is_none_typeerror(ASYNC_QUEUE):
    """Test TypeError is raised if the consumer is None."""
    app = Application('testing',
                      consumer=None,
                      settings={'ASYNC_QUEUE': ASYNC_QUEUE})
    with pytest.raises(TypeError):
        app.run_forever()
Exemple #4
0
def test_callback_not_coroutine_typerror(callback, ASYNC_QUEUE):
    """Test TypeError is raised if callback isn't a coroutine."""
    app = Application('testing',
                      consumer=[],
                      callback=callback,
                      settings={'ASYNC_QUEUE': ASYNC_QUEUE})
    with pytest.raises(TypeError):
        app.run_forever()
Exemple #5
0
def test_process_exception_stops_application(event_loop, test_consumer,
                                             ASYNC_QUEUE):
    """Test that the application stops after a processing exception."""
    async def callback(app, message):
        return [{}]

    app = Application('testing',
                      consumer=test_consumer,
                      callback=callback,
                      settings={'ASYNC_QUEUE': ASYNC_QUEUE})

    @app.result_postprocessor
    async def postprocess(app, message):
        raise Exception()

    with pytest.raises(Exception):
        app.run_forever(loop=event_loop)
def test_consumer_exception(event_loop):
    """Test that the application stops after a consumer exception."""
    consumer_called = False
    callback_called = False

    class Consumer:
        @asyncio.coroutine
        def read(self):
            nonlocal consumer_called
            consumer_called = True
            raise Exception()

    @asyncio.coroutine
    def callback(app, message):
        nonlocal callback_called
        callback_called = True

    app = Application('testing', consumer=Consumer(), callback=callback)
    app.run_forever(loop=event_loop)

    assert consumer_called
    assert not callback_called
Exemple #7
0
def test_consumer_exception(event_loop, ASYNC_QUEUE):
    """Test that the application stops after a consumer exception."""
    consumer_called = False
    callback_called = False

    class Consumer:
        async def read(self):
            nonlocal consumer_called
            consumer_called = True
            raise Exception()

    async def callback(app, message):
        nonlocal callback_called
        callback_called = True

    app = Application('testing',
                      consumer=Consumer(),
                      callback=callback,
                      settings={'ASYNC_QUEUE': ASYNC_QUEUE})
    app.run_forever(loop=event_loop)

    assert consumer_called
    assert not callback_called
def test_consumer_is_none_typeerror():
    """Test TypeError is raised if the consumer is None."""
    app = Application('testing', consumer=None)
    with pytest.raises(TypeError):
        app.run_forever()