Skip to content

Commit

Permalink
update format method name
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-dot committed Mar 14, 2024
1 parent 3287e1e commit 29ac01e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/blueapi/utils/ophyd_async_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def _wait_for_tasks(tasks: Dict[asyncio.Task, str], timeout: float):
t.cancel()
with suppress(Exception):
await t
msg += format_error_message(tasks, t)
msg += _format_awaited_task_error_message(tasks, t)

Check warning on line 33 in src/blueapi/utils/ophyd_async_connect.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/utils/ophyd_async_connect.py#L33

Added line #L33 was not covered by tests
logging.error(msg)
raised = [t for t in done if t.exception()]
if raised:
Expand All @@ -41,7 +41,9 @@ async def _wait_for_tasks(tasks: Dict[asyncio.Task, str], timeout: float):
raise NotConnected("Not all Devices connected")


def format_error_message(tasks: Dict[asyncio.Task, str], t: asyncio.Task) -> str:
def _format_awaited_task_error_message(
tasks: Dict[asyncio.Task, str], t: asyncio.Task
) -> str:
e = t.exception()
part_one = f"\n {tasks[t]}: {type(e).__name__}"
lines = str(e).splitlines()
Expand Down
18 changes: 13 additions & 5 deletions tests/utils/test_ophyd_async_connect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import unittest

from blueapi.utils.ophyd_async_connect import format_error_message
from blueapi.utils.ophyd_async_connect import _format_awaited_task_error_message
from blueapi.worker.task import Task

_SIMPLE_TASK = Task(name="sleep", params={"time": 0.0})
Expand Down Expand Up @@ -34,15 +34,19 @@ def test_format_error_message_single_line(self):
task = self.loop.run_until_complete(self._create_task_with_exception(exception))
tasks = {task: "Task1"}
expected_output = "\n Task1: ValueError: A single-line error"
self.assertEqual(format_error_message(tasks, task), expected_output)
self.assertEqual(
_format_awaited_task_error_message(tasks, task), expected_output
)

def test_format_error_message_multi_line(self):
# Test formatting with an exception that has a multi-line message
exception = ValueError("A multi-line\nerror message")
task = self.loop.run_until_complete(self._create_task_with_exception(exception))
tasks = {task: "Task2"}
expected_output = "\n Task2: ValueError\n A multi-line\n error message"
self.assertEqual(format_error_message(tasks, task), expected_output)
self.assertEqual(
_format_awaited_task_error_message(tasks, task), expected_output
)

def test_format_error_message_simple_task_failure(self):
# Test formatting with the _SIMPLE_TASK key and a failing asyncio task
Expand All @@ -52,7 +56,9 @@ def test_format_error_message_simple_task_failure(self):
)
tasks = {failing_task: _SIMPLE_TASK.name}
expected_output = "\n sleep: RuntimeError: Simple task error"
self.assertEqual(format_error_message(tasks, failing_task), expected_output)
self.assertEqual(
_format_awaited_task_error_message(tasks, failing_task), expected_output
)

def test_format_error_message_long_task_failure(self):
# Test formatting with the _LONG_TASK key and a failing asyncio task
Expand All @@ -62,7 +68,9 @@ def test_format_error_message_long_task_failure(self):
)
tasks = {failing_task: _LONG_TASK.name}
expected_output = "\n sleep: RuntimeError: Long task error"
self.assertEqual(format_error_message(tasks, failing_task), expected_output)
self.assertEqual(
_format_awaited_task_error_message(tasks, failing_task), expected_output
)


if __name__ == "__main__":
Expand Down

0 comments on commit 29ac01e

Please sign in to comment.