From 528f6a05c2091e67a7b1cf7d968901119ec924df Mon Sep 17 00:00:00 2001 From: Alan <82540753+alan-ls@users.noreply.github.com> Date: Tue, 23 Aug 2022 14:47:06 -0400 Subject: [PATCH] Fix asyncio rawsocket protocol transport details (#1592) (#1593) * fix asyncio rawsocket client and server tests * Fix asyncio rawsocket transport details as per session details in wamp.protocol.ApplicationSession * Makefile: flake8 target runs same cmd as CI * flake8: fix E712 errors --- Makefile | 4 +--- autobahn/asyncio/rawsocket.py | 13 +++++++++++-- autobahn/asyncio/test/test_aio_rawsocket.py | 18 +++++++++--------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 28eb2b7b6..dadc0c5e5 100755 --- a/Makefile +++ b/Makefile @@ -302,9 +302,7 @@ autopep8: # This will run pep8, pyflakes and can skip lines that end with # noqa flake8: - flake8 --ignore=E402,E501,E722,E741,N801,N802,N803,N805,N806,N815 \ - --exclude "autobahn/wamp/message_fbs.py,autobahn/wamp/gen/*"\ - autobahn + tox -c tox.ini -e flake8 # run PyLint pylint: diff --git a/autobahn/asyncio/rawsocket.py b/autobahn/asyncio/rawsocket.py index df981f8e4..5ee37dc25 100644 --- a/autobahn/asyncio/rawsocket.py +++ b/autobahn/asyncio/rawsocket.py @@ -33,6 +33,7 @@ import txaio from autobahn.util import public, _LazyHexFormatter, hltype from autobahn.wamp.exception import ProtocolError, SerializationError, TransportLost +from autobahn.wamp.types import TransportDetails from autobahn.asyncio.util import get_serializers, create_transport_details, transport_channel_id __all__ = ( @@ -59,6 +60,13 @@ class PrefixProtocol(asyncio.Protocol): peer: Optional[str] = None is_server: Optional[bool] = None + @property + def transport_details(self) -> Optional[TransportDetails]: + """ + Implements :func:`autobahn.wamp.interfaces.ITransport.transport_details` + """ + return self._transport_details + def connection_made(self, transport): # asyncio networking framework entry point, called by asyncio # when the connection is established (either a client or a server) @@ -66,8 +74,9 @@ def connection_made(self, transport): self.transport = transport - # determine preliminary transport details (what is know at this point) + # determine preliminary transport details (what is known at this point) self._transport_details = create_transport_details(self.transport, self.is_server) + self._transport_details.channel_framing = TransportDetails.CHANNEL_FRAMING_RAWSOCKET # backward compatibility self.peer = self._transport_details.peer @@ -463,7 +472,7 @@ def __init__(self, factory, serializers=None): # when no serializers were requested specifically, then support # all that are available if serializers is None: - serializers = get_serializers() + serializers = [serializer() for serializer in get_serializers()] if not serializers: raise Exception("could not import any WAMP serializers") diff --git a/autobahn/asyncio/test/test_aio_rawsocket.py b/autobahn/asyncio/test/test_aio_rawsocket.py index 37f1606dd..726a62420 100644 --- a/autobahn/asyncio/test/test_aio_rawsocket.py +++ b/autobahn/asyncio/test/test_aio_rawsocket.py @@ -7,6 +7,7 @@ WampRawSocketClientFactory, WampRawSocketServerFactory from autobahn.asyncio.util import get_serializers from autobahn.wamp import message +from autobahn.wamp.types import TransportDetails @pytest.mark.skipif(not os.environ.get('USE_ASYNCIO', False), reason='test runs on asyncio only') @@ -179,12 +180,8 @@ def serializer_id(self): transport.close.assert_called_once_with() -# FIXME: tests below - - -@pytest.mark.asyncio @pytest.mark.skipif(not os.environ.get('USE_ASYNCIO', False), reason='test runs on asyncio only') -def _test_wamp_server(event_loop): +def test_wamp_server(event_loop): transport = Mock(spec_set=('abort', 'close', 'write', 'get_extra_info')) transport.write = Mock(side_effect=lambda m: messages.append(m)) server = Mock(spec=['onOpen', 'onMessage']) @@ -196,22 +193,23 @@ def fact_server(): proto = WampRawSocketServerFactory(fact_server)() proto.connection_made(transport) + assert proto.transport_details.is_server is True + assert proto.transport_details.channel_framing == TransportDetails.CHANNEL_FRAMING_RAWSOCKET assert proto.factory._serializers s = proto.factory._serializers[1].RAWSOCKET_SERIALIZER_ID proto.data_received(bytes(bytearray([0x7F, 0xF0 | s, 0, 0]))) assert proto._serializer server.onOpen.assert_called_once_with(proto) - proto.sendMessage(message.Abort('close')) + proto.send(message.Abort('close')) for d in messages[1:]: proto.data_received(d) assert server.onMessage.called assert isinstance(server.onMessage.call_args[0][0], message.Abort) -@pytest.mark.asyncio @pytest.mark.skipif(not os.environ.get('USE_ASYNCIO', False), reason='test runs on asyncio only') -def _test_wamp_client(event_loop): +def test_wamp_client(event_loop): transport = Mock(spec_set=('abort', 'close', 'write', 'get_extra_info')) transport.write = Mock(side_effect=lambda m: messages.append(m)) client = Mock(spec=['onOpen', 'onMessage']) @@ -223,12 +221,14 @@ def fact_client(): proto = WampRawSocketClientFactory(fact_client)() proto.connection_made(transport) + assert proto.transport_details.is_server is False + assert proto.transport_details.channel_framing == TransportDetails.CHANNEL_FRAMING_RAWSOCKET assert proto._serializer s = proto._serializer.RAWSOCKET_SERIALIZER_ID proto.data_received(bytes(bytearray([0x7F, 0xF0 | s, 0, 0]))) client.onOpen.assert_called_once_with(proto) - proto.sendMessage(message.Abort('close')) + proto.send(message.Abort('close')) for d in messages[1:]: proto.data_received(d) assert client.onMessage.called