Skip to content

Commit

Permalink
Add failing client test
Browse files Browse the repository at this point in the history
  • Loading branch information
callumforrester committed Dec 4, 2024
1 parent 71d536a commit e082f2a
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion tests/unit_tests/client/test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable
from unittest.mock import MagicMock, Mock, call
from unittest.mock import MagicMock, Mock, call, patch

import pytest
from bluesky_stomp.messaging import MessageContext
Expand Down Expand Up @@ -259,6 +259,72 @@ def test_reload_environment(
mock_rest.delete_environment.assert_called_once()


@patch("blueapi.client.client.time.time")
@patch("blueapi.client.client.time.sleep")
def test_reload_environment_no_timeout(
mock_sleep: Mock,
mock_time: Mock,
client: BlueapiClient,
mock_rest: Mock,
):
mock_rest.get_environment.side_effect = [
EnvironmentResponse(initialized=False),
EnvironmentResponse(initialized=False),
EnvironmentResponse(initialized=False),
EnvironmentResponse(initialized=True),
]
mock_time.return_value = 100.0
client.reload_environment(timeout=None)
assert mock_sleep.call_count == 3


@patch("blueapi.client.client.time.time")
@patch("blueapi.client.client.time.sleep")
def test_reload_environment_with_timeout(
_: Mock,
mock_time: Mock,
client: BlueapiClient,
mock_rest: Mock,
):
mock_rest.get_environment.side_effect = [
EnvironmentResponse(initialized=False),
EnvironmentResponse(initialized=False),
EnvironmentResponse(initialized=False),
EnvironmentResponse(initialized=False),
]
mock_time.side_effect = [
100.0,
100.5,
101.0, # Timeout should occur here
101.5,
]
with pytest.raises(
TimeoutError,
match="Failed to reload the environment within 1.0 "
"seconds, a server restart is recommended",
):
client.reload_environment(timeout=1.0)


@patch("blueapi.client.client.time.time")
@patch("blueapi.client.client.time.sleep")
def test_reload_environment_ignores_current_environment(
mock_sleep: Mock,
mock_time: Mock,
client: BlueapiClient,
mock_rest: Mock,
):
mock_rest.get_environment.side_effect = [
EnvironmentResponse(initialized=True), # This is the old environment
EnvironmentResponse(initialized=False),
EnvironmentResponse(initialized=False),
EnvironmentResponse(initialized=True), # This is the new environment
]
mock_time.return_value = 100.0
client.reload_environment(timeout=None)
assert mock_sleep.call_count == 3


def test_reload_environment_failure(
client: BlueapiClient,
mock_rest: Mock,
Expand Down

0 comments on commit e082f2a

Please sign in to comment.