async def test_start_starts_apps_in_reverse_order(self, three_level_app_chain): with connection_manager() as conn: pipeline = BasePipeline(connection_name=conn.name, installed_apps=three_level_app_chain) mock_parent = MagicMock() # Mock all of the apps that have been configured for this pipeline. for key, app in pipeline._installed_apps.items(): app_mock = MagicMock(app.__class__) app_mock.name = app.name app_mock.initialize = get_mock_async() app_mock.start = get_mock_async() setattr(mock_parent, app.name, app_mock) pipeline._installed_apps[key] = app_mock pipeline.stopped_event.set( ) # Stop pipeline immediately after start has been executed await pipeline.start() for app in pipeline.apps.values(): assert app.start.called assert app.start.call_count == 1 call_order = [ call[0].rstrip(".start") for call in mock_parent.method_calls[-len(pipeline.apps):] ] assert call_order == list(reversed(pipeline.apps.keys())) await pipeline.stop()
async def test_listen_reads_a_complete_message(self, base_pipeline, encoder_app): session_app = ClientSessionApp(base_pipeline) session_app.reader = asyncio.StreamReader() session_app.writer = mock.MagicMock(asyncio.StreamWriter) session_app.writer.write = mock.Mock() session_app.writer.transport.is_closing = mock.Mock() session_app.writer.transport.is_closing.return_value = False session_app.pipeline.receive = get_mock_async() msg = admin.TestRequestMessage("Test123") msg.SendingTime = datetime.utcnow().strftime( settings.DATETIME_FORMAT)[:-3] encoded_msg = encoder_app.encode_message(msg) asyncio.create_task(session_app.listen()) session_app.reader.feed_data( encoded_msg[:-1]) # Feed first part of message await asyncio.sleep(0.1) session_app.writer.transport.is_closing.return_value = ( True) # Close listener after this message session_app.reader.feed_data( encoded_msg[-1:]) # Feed second part of message await asyncio.sleep(0.1) assert session_app.pipeline.receive.mock_calls[0][1][0] == encoded_msg
async def test_stop_allows_only_one_stop_process_to_run_concurrently( self, three_level_app_chain): with connection_manager() as conn: pipeline = BasePipeline(connection_name=conn.name, installed_apps=three_level_app_chain) # Mock all of the apps that have been configured for this pipeline. for key, app in pipeline._installed_apps.items(): app_mock = MagicMock(app.__class__) app_mock.stop = get_mock_async() pipeline._installed_apps[key] = app_mock asyncio.create_task(pipeline.stop()) asyncio.create_task(pipeline.stop()) asyncio.create_task(pipeline.stop()) await pipeline.stop() for app in pipeline.apps.values(): assert app.stop.called assert app.stop.call_count == 1 # Wait for separate tasks to complete tasks = asyncio.all_tasks() await asyncio.wait(tasks, timeout=0.1)
async def test_stop_stops_apps_in_top_down_order(self, three_level_app_chain): with connection_manager() as conn: pipeline = BasePipeline(connection_name=conn.name, installed_apps=three_level_app_chain) mock_parent = MagicMock() # Mock all of the apps that have been configured for this pipeline. for key, app in pipeline._installed_apps.items(): app_mock = MagicMock(app.__class__) app_mock.name = app.name app_mock.stop = get_mock_async() setattr(mock_parent, app.name, app_mock) pipeline._installed_apps[key] = app_mock await pipeline.stop() for app in pipeline.apps.values(): assert app.stop.called assert app.stop.call_count == 1 call_order = [ call[0].rstrip("stop").rstrip(".") for call in mock_parent.method_calls[-len(pipeline.apps):] ] assert call_order == list(pipeline.apps.keys())
async def test_start_raises_exception_on_start_timeout( self, three_level_app_chain): with pytest.raises(asyncio.TimeoutError): with connection_manager() as conn: pipeline = BasePipeline(connection_name=conn.name, installed_apps=three_level_app_chain) settings.STARTUP_TIMEOUT = 0.1 # Mock all of the apps that have been configured for this pipeline. for key, app in pipeline._installed_apps.items(): app_mock = MagicMock(app.__class__) app_mock.initialize = get_mock_async() app_mock.start = get_slow_mock_async( settings.STARTUP_TIMEOUT + 0.1) app_mock.stop = get_mock_async() pipeline._installed_apps[key] = app_mock await pipeline.start() await pipeline.stop()
async def test_initialize_initializes_each_app_exactly_once( self, three_level_app_chain): with connection_manager() as conn: pipeline = BasePipeline(connection_name=conn.name, installed_apps=three_level_app_chain) # Mock all of the apps that have been configured for this pipeline. for key, app in pipeline._installed_apps.items(): app_mock = MagicMock(app.__class__) app_mock.name = app.name app_mock.initialize = get_mock_async() app_mock.stop = get_mock_async() pipeline._installed_apps[key] = app_mock await pipeline.initialize() for app in pipeline.apps.values(): assert app.initialize.called assert app.initialize.call_count == 1 await pipeline.stop()
async def test_stop_no_op_if_already_stopped(self, three_level_app_chain): with connection_manager() as conn: pipeline = BasePipeline(connection_name=conn.name, installed_apps=three_level_app_chain) # Mock all of the apps that have been configured for this pipeline. for key, app in pipeline._installed_apps.items(): app_mock = MagicMock(app.__class__) app_mock.stop = get_mock_async() pipeline._installed_apps[key] = app_mock await pipeline.stop() await pipeline.stop() for app in pipeline.apps.values(): assert app.stop.called assert app.stop.call_count == 1