diff --git a/tests/test_control/test_client.py b/tests/test_control/test_client.py index 2dceec2..7def352 100644 --- a/tests/test_control/test_client.py +++ b/tests/test_control/test_client.py @@ -71,7 +71,7 @@ async def test__server_handshake(self, mock__client_info: MagicMock): self.assertIsNone(await self.client._server_handshake(self.mock_reader, self.mock_writer)) self.assertTrue(self.client._connected) mock__client_info.assert_called_once_with() - self.mock_write.assert_called_once_with(json.dumps(mock_info).encode()) + self.mock_write.assert_has_calls([call(json.dumps(mock_info).encode()), call(b'\n')]) self.mock_drain.assert_awaited_once_with() self.mock_read.assert_awaited_once_with(SESSION_MSG_BYTES) self.mock_print.assert_has_calls([ @@ -121,7 +121,7 @@ async def test__interact(self, mock__get_command: MagicMock): mock__get_command.return_value = cmd = FOO + BAR + ' 123' self.mock_drain.side_effect = err = ConnectionError() self.assertIsNone(await self.client._interact(self.mock_reader, self.mock_writer)) - self.mock_write.assert_called_once_with(cmd.encode()) + self.mock_write.assert_has_calls([call(cmd.encode()), call(b'\n')]) self.mock_drain.assert_awaited_once_with() self.mock_read.assert_not_awaited() self.mock_print.assert_called_once_with(err, file=sys.stderr) @@ -133,7 +133,7 @@ async def test__interact(self, mock__get_command: MagicMock): self.mock_print.reset_mock() self.assertIsNone(await self.client._interact(self.mock_reader, self.mock_writer)) - self.mock_write.assert_called_once_with(cmd.encode()) + self.mock_write.assert_has_calls([call(cmd.encode()), call(b'\n')]) self.mock_drain.assert_awaited_once_with() self.mock_read.assert_awaited_once_with(SESSION_MSG_BYTES) self.mock_print.assert_called_once_with(FOO) diff --git a/tests/test_control/test_session.py b/tests/test_control/test_session.py index 0ad2786..d3ed90b 100644 --- a/tests/test_control/test_session.py +++ b/tests/test_control/test_session.py @@ -26,7 +26,7 @@ from unittest.mock import AsyncMock, MagicMock, patch, call from asyncio_taskpool.control import session -from asyncio_taskpool.internals.constants import CLIENT_INFO, CMD, SESSION_MSG_BYTES +from asyncio_taskpool.internals.constants import CLIENT_INFO, CMD from asyncio_taskpool.exceptions import HelpRequested from asyncio_taskpool.pool import SimpleTaskPool @@ -102,8 +102,8 @@ async def test_client_handshake(self, mock_parser_cls: MagicMock): mock_parser_cls.return_value = mock_parser width = 5678 msg = ' ' + json.dumps({CLIENT_INFO.TERMINAL_WIDTH: width, FOO: BAR}) + ' ' - mock_read = AsyncMock(return_value=msg.encode()) - self.mock_reader.read = mock_read + mock_readline = AsyncMock(return_value=msg.encode()) + self.mock_reader.readline = mock_readline self.mock_writer.drain = AsyncMock() expected_parser_kwargs = { 'stream': self.session._response_buffer, @@ -117,7 +117,7 @@ async def test_client_handshake(self, mock_parser_cls: MagicMock): } self.assertIsNone(await self.session.client_handshake()) self.assertEqual(mock_parser, self.session._parser) - mock_read.assert_awaited_once_with(SESSION_MSG_BYTES) + mock_readline.assert_awaited_once_with() mock_parser_cls.assert_called_once_with(**expected_parser_kwargs) mock_add_subparsers.assert_called_once_with(**expected_subparsers_kwargs) mock_add_class_commands.assert_called_once_with(self.mock_pool.__class__) @@ -190,27 +190,27 @@ def method(_): pass @patch.object(session.ControlSession, '_parse_command') async def test_listen(self, mock__parse_command: AsyncMock): def make_reader_return_empty(): - self.mock_reader.read.return_value = b'' + self.mock_reader.readline.return_value = b'' self.mock_writer.drain = AsyncMock(side_effect=make_reader_return_empty) msg = "fascinating" - self.mock_reader.read = AsyncMock(return_value=f' {msg} '.encode()) + self.mock_reader.readline = AsyncMock(return_value=f' {msg} '.encode()) response = FOO + BAR + FOO self.session._response_buffer.write(response) self.assertIsNone(await self.session.listen()) - self.mock_reader.read.assert_has_awaits([call(SESSION_MSG_BYTES), call(SESSION_MSG_BYTES)]) + self.mock_reader.readline.assert_has_awaits([call(), call()]) mock__parse_command.assert_awaited_once_with(msg) self.assertEqual('', self.session._response_buffer.getvalue()) self.mock_writer.write.assert_called_once_with(response.encode()) self.mock_writer.drain.assert_awaited_once_with() - self.mock_reader.read.reset_mock() + self.mock_reader.readline.reset_mock() mock__parse_command.reset_mock() self.mock_writer.write.reset_mock() self.mock_writer.drain.reset_mock() self.mock_server.is_serving = MagicMock(return_value=False) self.assertIsNone(await self.session.listen()) - self.mock_reader.read.assert_not_awaited() + self.mock_reader.readline.assert_not_awaited() mock__parse_command.assert_not_awaited() self.mock_writer.write.assert_not_called() self.mock_writer.drain.assert_not_awaited()