From 801e18b643f14fc3d91aeb13727b889757f0935f Mon Sep 17 00:00:00 2001 From: SamDanielThangarajan <12202554+SamDanielThangarajan@users.noreply.github.com> Date: Sat, 5 Oct 2024 22:36:48 +0200 Subject: [PATCH] add tests for soup sever session --- src/nasdaq_protocols/soup/session.py | 4 +- tests/test_itch_tools.py | 1 + tests/test_soup_session.py | 83 +++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/nasdaq_protocols/soup/session.py b/src/nasdaq_protocols/soup/session.py index 1a42cdc..7b4de94 100644 --- a/src/nasdaq_protocols/soup/session.py +++ b/src/nasdaq_protocols/soup/session.py @@ -233,7 +233,7 @@ async def on_unsequenced(self, msg: UnSequencedData) -> None: :param msg: UnSequencedData message. """ - def send_seq_msg(self, data: bytes) -> None: + def send_seq_msg(self, data: bytes | SequencedData) -> None: """ Send sequenced data to the client. @@ -251,7 +251,7 @@ def end_session(self): self.initiate_close() async def on_debug(self, msg: Debug) -> None: - self.log.info('%s> ++ client debug : %s', msg) + self.log.info('%s> ++ client debug : %s', self.session_id, msg) async def send_heartbeat(self): """ diff --git a/tests/test_itch_tools.py b/tests/test_itch_tools.py index 2eca0d6..1ed8b38 100644 --- a/tests/test_itch_tools.py +++ b/tests/test_itch_tools.py @@ -135,6 +135,7 @@ async def test__itch_tools__tail_itch__wrong_server(load_itch_tools): assert tailer.done() +@pytest.mark.xfail(reason='https://github.com/Nasdaq/nasdaq-protocols/issues/21') async def test__itch_tools__tail_itch__ctrl_c(mock_server_session, load_itch_tools): definitions, tools = load_itch_tools('test__itch_tools__tail_itch__ctrl_c') port, server_session = mock_server_session diff --git a/tests/test_soup_session.py b/tests/test_soup_session.py index 6c4a076..38c2ba7 100644 --- a/tests/test_soup_session.py +++ b/tests/test_soup_session.py @@ -1,14 +1,35 @@ import asyncio import logging + +import attrs import pytest from nasdaq_protocols import soup -from nasdaq_protocols.soup import LoginRequest, LoginAccepted, LoginRejected +from nasdaq_protocols.soup import LoginRequest, LoginAccepted, LoginRejected, UnSequencedData from tests.mocks import matches, send LOG = logging.getLogger(__name__) +@attrs.define(auto_attribs=True) +class SampleTestSourServerSession(soup.SoupServerSession): + output_sent: list[soup.SoupMessage] = attrs.field(factory=list) + + async def on_login(self, msg: LoginRequest) -> LoginAccepted | LoginRejected: + return LoginAccepted('session', 1) + + async def on_unsequenced(self, msg: UnSequencedData) -> None: + if msg.data == b'case1': + self.send_seq_msg(msg.data) + else: + self.send_seq_msg( + soup.SequencedData(b'case2') + ) + + def send_msg(self, msg): + self.output_sent.append(msg) + + def configure_login_accept(server_session): server_session.when( matches(soup.LoginRequest('test-u', 'test-p', 'session', '1')), 'login-request-match', @@ -169,3 +190,63 @@ class BaseSessionType(soup.SoupSession): pass assert BaseSessionType.SessionType is 'base' + + +async def test__soup_session__login_message_server_session(): + server_session = SampleTestSourServerSession() + assert server_session is not None + + await server_session.on_msg_coro( + soup.LoginRequest('test-u', 'test-p', 'session', '1') + ) + assert len(server_session.output_sent) == 1 + assert isinstance(server_session.output_sent[0], LoginAccepted) + + +async def test__soup_session__unsequenced_msg__server_session(): + server_session = SampleTestSourServerSession() + + await server_session.on_msg_coro( + soup.UnSequencedData(b'case1') + ) + assert len(server_session.output_sent) == 1 + assert isinstance(server_session.output_sent[0], soup.SequencedData) + + await server_session.on_msg_coro( + soup.UnSequencedData(b'case2') + ) + assert len(server_session.output_sent) == 2 + assert isinstance(server_session.output_sent[1], soup.SequencedData) + + +async def test__soup_session__send_debug(): + server_session = SampleTestSourServerSession() + await server_session.on_msg_coro( + soup.Debug('hello') + ) + assert len(server_session.output_sent) == 0 + + +async def test__soup_session__send_heartbeat(): + server_session = SampleTestSourServerSession() + await server_session.send_heartbeat() + assert len(server_session.output_sent) == 1 + assert isinstance(server_session.output_sent[0], soup.ServerHeartbeat) + + +async def test__soup_session__end_session(): + server_session = SampleTestSourServerSession() + server_session.end_session() + assert len(server_session.output_sent) == 1 + assert isinstance(server_session.output_sent[0], soup.EndOfSession) + + +async def test__soup_session__send_logout(): + server_session = SampleTestSourServerSession() + assert not server_session.is_closed() + + await server_session.on_msg_coro( + soup.LogoutRequest() + ) + assert len(server_session.output_sent) == 0 + assert server_session.is_closed() \ No newline at end of file