async def test_start_server_proc(self): """ Check that the proc is started and waited for properly """ mock_event_loop = asynctest.Mock(spec = asyncio.BaseEventLoop) mock_event_loop.run_until_complete.side_effect = \ self.event_loop.run_until_complete mock_proc = asynctest.Mock(spec = asyncio.subprocess.Process) mock_proc_func = asynctest.CoroutineMock() mock_proc_func.return_value = mock_proc mock_server = asynctest.Mock(spec = Server) mock_server.server_id = 'test' mock_server.start = asynctest.CoroutineMock() mock_server.start.return_value = mock_proc_func() mock_instances = unittest.mock.MagicMock() manager = Manager( self.host, self.port, self.root, event_loop = mock_event_loop, ) manager.instances = mock_instances await manager.start_server_proc(mock_server) mock_server.start.assert_called_with() mock_proc.wait.assert_called_with() mock_instances.__setitem__.assert_called_with('test', mock_proc) mock_instances.__delitem__.assert_called_with('test')
def test_run(self, start_server, handle_network, listdir, isdir, server, all_tasks, gather): """ Check that the run function starts and stops the event loop """ server_ids = [ 'server0', 'autoserver1', 'autoserver2', 'server3', ] mock_event_loop = asynctest.Mock(asyncio.BaseEventLoop) listdir.return_value = server_ids isdir.return_value = True mock_servers = [ unittest.mock.Mock( spec = Server, server_id = server_id, settings = { 'autostart': server_id.startswith('auto'), }, ) for server_id in server_ids ] server.side_effect = mock_servers all_tasks.return_value = server_ids gather.return_value = gather mock_start_server_proc = asynctest.CoroutineMock() mock_start_server_proc.return_value = mock_start_server_proc manager = Manager( self.host, self.port, self.root, event_loop = mock_event_loop, ) manager.start_server_proc = mock_start_server_proc manager.run() start_server.assert_called_with( handle_network, self.host, self.port, loop = mock_event_loop, ) manager.start_server_proc.assert_has_calls( [ unittest.mock.call(mock_server) for mock_server in mock_servers if mock_server.server_id.startswith('auto') ] ) mock_event_loop.run_forever.assert_called_with() all_tasks.assert_called_with() gather.assert_called_with(*server_ids) mock_event_loop.run_until_complete.assert_called_with(gather) mock_event_loop.close.assert_called_with()