Skip to content

Commit

Permalink
cleaned up publisher, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanni-t committed May 1, 2024
1 parent d02bf45 commit eb5239f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def initialize(
await self._publish_runs_advise_refetch_async(run_id=run_id)
engine_state_summary = self._run_hooks.get_state_summary(self._run_hooks.run_id)
if (
engine_state_summary is not None
isinstance(engine_state_summary, StateSummary)
and engine_state_summary.completedAt is not None
):
await self.publish_pre_serialized_commands_notification(run_id=run_id)
Expand All @@ -101,9 +101,16 @@ async def _publish_runs_advise_refetch_async(self, run_id: str) -> None:

async def _publish_runs_advise_unsubscribe_async(self, run_id: str) -> None:
"""Publish an unsubscribe flag for relevant runs topics."""
await self._client.publish_advise_unsubscribe_async(
topic=f"{Topics.RUNS}/{run_id}"
)
if self._run_hooks is not None:
await self._client.publish_advise_unsubscribe_async(
topic=f"{Topics.RUNS}/{run_id}"
)
await self._client.publish_advise_unsubscribe_async(
topic=Topics.RUNS_CURRENT_COMMAND
)
await self._client.publish_advise_unsubscribe_async(
topic=f"{Topics.RUNS_PRE_SERIALIZED_COMMANDS}/{run_id}"
)

async def publish_pre_serialized_commands_notification(self, run_id: str) -> None:
"""Publishes notification for GET /runs/:runId/commandsAsPreSerializedList."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from unittest.mock import MagicMock, AsyncMock

from robot_server.service.notifications import RunsPublisher, Topics
from opentrons.protocol_engine import CurrentCommand, EngineStatus
from opentrons.protocol_engine import CurrentCommand, EngineStatus, StateSummary


def mock_curent_command(command_id: str) -> CurrentCommand:
Expand All @@ -17,6 +17,19 @@ def mock_curent_command(command_id: str) -> CurrentCommand:
)


def mock_state_summary(run_id: str) -> StateSummary:
return StateSummary.construct(
status=EngineStatus.FAILED,
errors=[],
labware=[],
pipettes=[],
modules=[],
labwareOffsets=[],
startedAt=None,
completedAt=datetime(year=2021, month=1, day=1),
)


@pytest.fixture
def notification_client() -> AsyncMock:
"""Mocked notification client."""
Expand Down Expand Up @@ -64,6 +77,25 @@ async def test_initialize(
)


async def test_initialize_publisher_on_completed_runs(
runs_publisher: RunsPublisher, notification_client: AsyncMock
) -> None:
"""It should initialize the publisher w/ parameters & callbacks and notify for pre-serialized commands."""
run_id = "1234"
get_current_command = AsyncMock()

await runs_publisher.initialize(run_id, get_current_command, mock_state_summary)

assert runs_publisher._engine_state_slice
notification_client.publish_advise_refetch_async.assert_any_await(topic=Topics.RUNS)
notification_client.publish_advise_refetch_async.assert_any_await(
topic=f"{Topics.RUNS}/1234"
)
notification_client.publish_advise_refetch_async.assert_any_await(
topic=f"{Topics.RUNS_PRE_SERIALIZED_COMMANDS}/1234"
)


@pytest.mark.asyncio
async def test_clean_up_current_run(
runs_publisher: RunsPublisher, notification_client: AsyncMock
Expand All @@ -80,6 +112,12 @@ async def test_clean_up_current_run(
notification_client.publish_advise_unsubscribe_async.assert_any_await(
topic=f"{Topics.RUNS}/1234"
)
notification_client.publish_advise_unsubscribe_async.assert_any_await(
topic=Topics.RUNS_CURRENT_COMMAND
)
notification_client.publish_advise_unsubscribe_async.assert_any_await(
topic=f"{Topics.RUNS_PRE_SERIALIZED_COMMANDS}/1234"
)


@pytest.mark.asyncio
Expand Down Expand Up @@ -143,3 +181,24 @@ async def test_handle_engine_status_change(
notification_client.publish_advise_refetch_async.assert_any_await(
topic=f"{Topics.RUNS}/1234"
)


async def test_publish_pre_serialized_commannds_notif(
runs_publisher: RunsPublisher, notification_client: AsyncMock
) -> None:
"""It should send out a notification for pre serialized commands."""
await runs_publisher.initialize(
"1234", lambda _: mock_curent_command("command1"), AsyncMock()
)

assert runs_publisher._run_hooks
assert runs_publisher._engine_state_slice
assert notification_client.publish_advise_refetch_async.call_count == 2

await runs_publisher.publish_pre_serialized_commands_notification(run_id="1234")

assert notification_client.publish_advise_refetch_async.call_count == 3

notification_client.publish_advise_refetch_async.assert_any_await(
topic=f"{Topics.RUNS_PRE_SERIALIZED_COMMANDS}/1234"
)

0 comments on commit eb5239f

Please sign in to comment.