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()
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
def test_abort_callback(event_loop, cancelled_future, queue): """Test that aborted callbacks stop execution.""" # This test sets up a callback and a postprocessor. The callback # will raise an Abort exception. The postprocessor shouldn't be # called. callback_called = False postprocess_called = False queue.put_nowait({'a': 1}) async def callback(app, message): nonlocal callback_called callback_called = True raise exceptions.Abort('testing', message) app = Application('testing', callback=callback) @app.result_postprocessor async def postprocess(app, result): nonlocal postprocess_called postprocess_called = True return result event_loop.run_until_complete( app._process(cancelled_future, queue, event_loop)) assert callback_called assert not postprocess_called
def test_abort_error(event_loop, cancelled_future, queue): """Test that aborted error callbacks stop execution.""" # This test sets up a callback and two error callbacks. The callback # will raise an exception and the first error callback will an raise # Abort exception. The second error callback shouldn't be called. callback_called = False error1_called = False error2_called = False queue.put_nowait({'a': 1}) async def callback(app, message): nonlocal callback_called callback_called = True raise TypeError('testing') app = Application('testing', callback=callback) @app.error async def error1(app, message, exc): nonlocal error1_called error1_called = True raise exceptions.Abort('testing', message) @app.error async def error2(app, message, exc): nonlocal error2_called error2_called = True event_loop.run_until_complete( app._process(cancelled_future, queue, event_loop)) assert callback_called assert error1_called assert not error2_called
def test_abort_postprocess(event_loop, cancelled_future, queue): """Test that aborted postprocessors stop execution of the result.""" # This test sets up a callback and two postprocessors. The first # will raise an Abort exception for one of the two results returned # by the callback. postprocess1_called_count = 0 postprocess2_called_count = 0 queue.put_nowait({'a': 1}) async def callback(app, message): return [True, False] app = Application('testing', callback=callback) @app.result_postprocessor async def postprocess1(app, result): nonlocal postprocess1_called_count postprocess1_called_count += 1 if result: raise exceptions.Abort('testing', result) return result @app.result_postprocessor async def postprocess2(app, result): nonlocal postprocess2_called_count postprocess2_called_count += 1 return result event_loop.run_until_complete( app._process(cancelled_future, queue, event_loop)) assert postprocess1_called_count == 2 assert postprocess2_called_count == 1
def test_postprocess_results(original, expected): """Test Application._postprocess_results.""" callback1_called = False callback2_called = False app = Application('testing') @app.result_postprocessor @asyncio.coroutine def callback1(app, message): nonlocal callback1_called callback1_called = True return message + 1 @app.result_postprocessor @asyncio.coroutine def callback2(app, message): nonlocal callback2_called callback2_called = True # Nothing is returned out of Application._postprocess_results so # the assertion needs to happen inside a callback. assert message == expected yield from app._postprocess_results([original]) assert callback1_called assert callback2_called
def test_message_acknowledgement_original_message(event_loop, coroutine, cancelled_future, queue): """Test that original message is acknowledged.""" actual = '' expected = 'original' queue.put_nowait(expected) app = Application('testing', callback=coroutine) @app.message_preprocessor @asyncio.coroutine def preprocess(app, message): return 'changed' @app.message_acknowledgement @asyncio.coroutine def acknowledge(app, message): nonlocal actual actual = message event_loop.run_until_complete( app._process(cancelled_future, queue, event_loop)) assert actual == expected
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
def test_abort_callback(event_loop, cancelled_future, queue): """Test that aborted callbacks stop execution.""" # This test sets up a callback and a postprocessor. The callback # will raise an Abort exception. The postprocessor shouldn't be # called. callback_called = False postprocess_called = False queue.put_nowait({'a': 1}) @asyncio.coroutine def callback(app, message): nonlocal callback_called callback_called = True raise exceptions.Abort('testing', message) app = Application('testing', callback=callback) @app.result_postprocessor @asyncio.coroutine def postprocess(app, result): nonlocal postprocess_called postprocess_called = True return result event_loop.run_until_complete( app._process(cancelled_future, queue, event_loop)) assert callback_called assert not postprocess_called
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()
def test_consume(event_loop, test_consumer, cancelled_future): """Test Application._consume.""" queue = asyncio.Queue(maxsize=1) app = Application('testing', consumer=test_consumer) asyncio.async(app._consume(queue, cancelled_future)) event_loop.stop() # Run the event loop once. event_loop.run_forever() # The size of the queue won't ever be larger than 1 because of the # maxsize argument. assert queue.qsize() == 1
def test_abort_preprocessor(event_loop, cancelled_future, queue): """Test that aborted preprocessors stop execution.""" # This test sets up two preprocessors, a callback, and a # postprocessor. The first preprocessor will raise an Abort # exception. None of the others should be called. preprocess1_called = False preprocess2_called = False callback_called = False postprocess_called = False queue.put_nowait({'a': 1}) @asyncio.coroutine def callback(app, message): nonlocal callback_called callback_called = True return message app = Application('testing', callback=callback) @app.message_preprocessor @asyncio.coroutine def preprocess1(app, message): nonlocal preprocess1_called preprocess1_called = True raise exceptions.Abort('testing', message) @app.message_preprocessor @asyncio.coroutine def preprocess2(app, message): nonlocal preprocess2_called preprocess2_called = True return message @app.result_postprocessor @asyncio.coroutine def postprocess(app, result): nonlocal postprocess_called postprocess_called = True return result event_loop.run_until_complete( app._process(cancelled_future, queue, event_loop)) assert preprocess1_called assert not preprocess2_called assert not callback_called assert not postprocess_called
async def test_postprocess_results(original, expected, ASYNC_QUEUE): """Test Application._postprocess_results.""" callback1_called = False callback2_called = False app = Application('testing', settings={'ASYNC_QUEUE': ASYNC_QUEUE}) @app.result_postprocessor async def callback1(app, message): nonlocal callback1_called callback1_called = True return message + 1 @app.result_postprocessor async def callback2(app, message): nonlocal callback2_called callback2_called = True # Nothing is returned out of Application._postprocess_results so # the assertion needs to happen inside a callback. assert message == expected await app._postprocess_results([original]) assert callback1_called assert callback2_called
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_consume(event_loop, test_consumer, ASYNC_QUEUE): """Test Application._consume.""" if ASYNC_QUEUE: queue = asyncio.Queue(maxsize=1) else: queue = sync_queue.Queue(maxsize=1) app = Application('testing', settings={"ASYNC_QUEUE": ASYNC_QUEUE}, consumer=test_consumer) asyncio.ensure_future(app._consume(queue), loop=event_loop) event_loop.stop() # Run the event loop once. event_loop.run_forever() # The size of the queue won't ever be larger than 1 because of the # maxsize argument. assert queue.qsize() == 1
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
def test_abort_error(event_loop, cancelled_future, queue): """Test that aborted error callbacks stop execution.""" # This test sets up a callback and two error callbacks. The callback # will raise an exception and the first error callback will an raise # Abort exception. The second error callback shouldn't be called. callback_called = False error1_called = False error2_called = False queue.put_nowait({'a': 1}) @asyncio.coroutine def callback(app, message): nonlocal callback_called callback_called = True raise TypeError('testing') app = Application('testing', callback=callback) @app.error @asyncio.coroutine def error1(app, message, exc): nonlocal error1_called error1_called = True raise exceptions.Abort('testing', message) @app.error @asyncio.coroutine def error2(app, message, exc): nonlocal error2_called error2_called = True event_loop.run_until_complete( app._process(cancelled_future, queue, event_loop)) assert callback_called assert error1_called assert not error2_called
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_abort_postprocess(event_loop, cancelled_future, queue): """Test that aborted postprocessors stop execution of the result.""" # This test sets up a callback and two postprocessors. The first # will raise an Abort exception for one of the two results returned # by the callback. postprocess1_called_count = 0 postprocess2_called_count = 0 queue.put_nowait({'a': 1}) @asyncio.coroutine def callback(app, message): return [True, False] app = Application('testing', callback=callback) @app.result_postprocessor @asyncio.coroutine def postprocess1(app, result): nonlocal postprocess1_called_count postprocess1_called_count += 1 if result: raise exceptions.Abort('testing', result) return result @app.result_postprocessor @asyncio.coroutine def postprocess2(app, result): nonlocal postprocess2_called_count postprocess2_called_count += 1 return result event_loop.run_until_complete( app._process(cancelled_future, queue, event_loop)) assert postprocess1_called_count == 2 assert postprocess2_called_count == 1
def test_apply_callbacks(original, expected): """Test Application._apply_callbacks.""" callback1_called = False callback2_called = False @asyncio.coroutine def callback1(app, message): nonlocal callback1_called callback1_called = True return message + 1 @asyncio.coroutine def callback2(app, message): nonlocal callback2_called callback2_called = True return message * 2 app = Application('testing') actual = yield from app._apply_callbacks([callback1, callback2], original) assert actual == expected assert callback1_called assert callback2_called
async def test_apply_callbacks(original, expected, ASYNC_QUEUE): """Test Application._apply_callbacks.""" callback1_called = False callback2_called = False async def callback1(app, message): nonlocal callback1_called callback1_called = True return message + 1 async def callback2(app, message): nonlocal callback2_called callback2_called = True return message * 2 app = Application('testing', settings={'ASYNC_QUEUE': ASYNC_QUEUE}) actual = await app._apply_callbacks([callback1, callback2], original) assert actual == expected assert callback1_called assert callback2_called
def test_error_not_coroutine_typeerror(error_callback): """Test TypeError is raised if error callback isn't a coroutine.""" app = Application('testing') with pytest.raises(TypeError): app.error(error_callback)
def test_callback_not_coroutine_typerror(callback): """Test TypeError is raised if callback isn't a coroutine.""" app = Application('testing', consumer=[], callback=callback) with pytest.raises(TypeError): app.run_forever()
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()
def test_error_not_coroutine_typeerror(error_callback, ASYNC_QUEUE): """Test TypeError is raised if error callback isn't a coroutine.""" app = Application('testing', settings={'ASYNC_QUEUE': ASYNC_QUEUE}) with pytest.raises(TypeError): app.error(error_callback)
def test_message_acknowledgement_not_coroutine_typeerror( acknowledgement, ASYNC_QUEUE): """Test TypeError is raised if acknowledgement isn't a coroutine.""" app = Application('testing', settings={'ASYNC_QUEUE': ASYNC_QUEUE}) with pytest.raises(TypeError): app.message_acknowledgement(acknowledgement)
def test_teardown_not_coroutine_typeerror(teardown): """Test TypeError is raised if teardown isn't a coroutine.""" app = Application('testing') with pytest.raises(TypeError): app.teardown(teardown)
def test_startup_not_coroutine_typeerror(startup): """Test TypeError is raised if startup isn't a coroutine.""" app = Application('testing') with pytest.raises(TypeError): app.startup(startup)
def test_result_postprocessor_not_coroutine_typeerror(postprocess): """Test TypeError is raised if postprocessor isn't a coroutine.""" app = Application('testing') with pytest.raises(TypeError): app.result_postprocessor(postprocess)
def test_result_postprocessor_not_coroutine_typeerror(postprocess, ASYNC_QUEUE): """Test TypeError is raised if postprocessor isn't a coroutine.""" app = Application('testing', settings={'ASYNC_QUEUE': ASYNC_QUEUE}) with pytest.raises(TypeError): app.result_postprocessor(postprocess)
def test_message_preprocessor_not_coroutine_typeerror(preprocess): """Test TypeError is raised if preprocessor isn't a coroutine.""" app = Application('testing') with pytest.raises(TypeError): app.message_preprocessor(preprocess)
def test_startup_not_coroutine_typeerror(startup, ASYNC_QUEUE): """Test TypeError is raised if startup isn't a coroutine.""" app = Application('testing', settings={'ASYNC_QUEUE': ASYNC_QUEUE}) with pytest.raises(TypeError): app.startup(startup)
def test_message_acknowledgement_not_coroutine_typeerror(acknowledgement): """Test TypeError is raised if acknowledgement isn't a coroutine.""" app = Application('testing') with pytest.raises(TypeError): app.message_acknowledgement(acknowledgement)
def test_teardown_not_coroutine_typeerror(teardown, ASYNC_QUEUE): """Test TypeError is raised if teardown isn't a coroutine.""" app = Application('testing', settings={'ASYNC_QUEUE': ASYNC_QUEUE}) with pytest.raises(TypeError): app.teardown(teardown)