Skip to content

Commit

Permalink
add tests for soup sever session
Browse files Browse the repository at this point in the history
  • Loading branch information
SamDanielThangarajan committed Oct 5, 2024
1 parent 49ab89b commit 801e18b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/nasdaq_protocols/soup/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_itch_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 82 additions & 1 deletion tests/test_soup_session.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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()

0 comments on commit 801e18b

Please sign in to comment.