Skip to content

Commit cbdd159

Browse files
committed
Test server init
1 parent a8a1524 commit cbdd159

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/ephys_link/back_end/server.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from ephys_link.__about__ import __version__
3434
from ephys_link.back_end.platform_handler import PlatformHandler
3535
from ephys_link.front_end.console import Console
36-
from ephys_link.utils.constants import PORT
36+
from ephys_link.utils.constants import PORT, SERVER_NOT_INITIALIZED_ERROR
3737

3838
# Server message generic types.
3939
INPUT_TYPE = TypeVar("INPUT_TYPE", bound=VBLBaseModel)
@@ -61,9 +61,8 @@ def __init__(self, options: EphysLinkOptions, platform_handler: PlatformHandler,
6161
if not self._options.use_proxy:
6262
# Exit if _sio is not a Server.
6363
if not isinstance(self._sio, AsyncServer):
64-
error = "Server not initialized."
65-
self._console.critical_print(error)
66-
raise TypeError(error)
64+
self._console.critical_print(SERVER_NOT_INITIALIZED_ERROR)
65+
raise TypeError(SERVER_NOT_INITIALIZED_ERROR)
6766

6867
self._app = Application()
6968
self._sio.attach(self._app) # pyright: ignore [reportUnknownMemberType]

src/ephys_link/utils/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ def did_not_reach_target_depth_error(request: SetDepthRequest, final_unified_dep
5858

5959

6060
EMERGENCY_STOP_MESSAGE = "Emergency Stopping All Manipulators..."
61+
62+
SERVER_NOT_INITIALIZED_ERROR = "Server not initialized."

tests/back_end/test_server.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import pytest
22
from pytest_mock import MockerFixture
3+
from socketio import AsyncServer # pyright: ignore[reportMissingTypeStubs]
34
from vbl_aquarium.models.ephys_link import EphysLinkOptions, GetManipulatorsResponse
45

56
import ephys_link.back_end.server
67
from ephys_link.back_end.platform_handler import PlatformHandler
78
from ephys_link.back_end.server import Server
89
from ephys_link.front_end.console import Console
10+
from ephys_link.utils.constants import SERVER_NOT_INITIALIZED_ERROR
911
from tests.conftest import DUMMY_STRING, DUMMY_STRING_LIST
1012

1113

@@ -30,6 +32,21 @@ def proxy_client(self, platform_handler: PlatformHandler, console: Console) -> S
3032
"""Fixture for server as proxy client."""
3133
return Server(EphysLinkOptions(use_proxy=True), platform_handler, console)
3234

35+
def test_failed_server_init(
36+
self, platform_handler: PlatformHandler, console: Console, mocker: MockerFixture
37+
) -> None:
38+
"""Server should raise error if sio is not an AsyncServer."""
39+
# Mock out the AsyncServer init.
40+
patched_async_server = mocker.patch.object(AsyncServer, "__new__")
41+
42+
# Act
43+
with pytest.raises(TypeError) as init_error:
44+
_ = Server(EphysLinkOptions(use_proxy=False), platform_handler, console)
45+
46+
# Assert
47+
patched_async_server.assert_called_once()
48+
assert init_error.value.args[0] == SERVER_NOT_INITIALIZED_ERROR
49+
3350
def test_launch_server(
3451
self, server: Server, platform_handler: PlatformHandler, console: Console, mocker: MockerFixture
3552
) -> None:

0 commit comments

Comments
 (0)