Skip to content

Commit

Permalink
test: Adopt abstract_base_test in websocket_test.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bdarnell committed Jul 11, 2024
1 parent 0f5a6ef commit 5d2d43f
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions tornado/test/websocket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from tornado.netutil import Resolver
from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.template import DictLoader
from tornado.test.util import abstract_base_test
from tornado.testing import AsyncHTTPTestCase, gen_test, bind_unused_port, ExpectLog
from tornado.web import Application, RequestHandler

Expand Down Expand Up @@ -661,7 +662,8 @@ def test_native_coroutine(self):
self.assertEqual(res, "hello2")


class CompressionTestMixin:
@abstract_base_test
class CompressionTestMixin(WebSocketBaseTestCase):
MESSAGE = "Hello world. Testing 123 123"

def get_app(self):
Expand Down Expand Up @@ -698,7 +700,7 @@ def verify_wire_bytes(self, bytes_in: int, bytes_out: int) -> None:
raise NotImplementedError()

@gen_test
def test_message_sizes(self: typing.Any):
def test_message_sizes(self):
ws = yield self.ws_connect(
"/echo", compression_options=self.get_client_compression_options()
)
Expand All @@ -713,7 +715,7 @@ def test_message_sizes(self: typing.Any):
self.verify_wire_bytes(ws.protocol._wire_bytes_in, ws.protocol._wire_bytes_out)

@gen_test
def test_size_limit(self: typing.Any):
def test_size_limit(self):
ws = yield self.ws_connect(
"/limited", compression_options=self.get_client_compression_options()
)
Expand All @@ -728,31 +730,32 @@ def test_size_limit(self: typing.Any):
self.assertIsNone(response)


@abstract_base_test
class UncompressedTestMixin(CompressionTestMixin):
"""Specialization of CompressionTestMixin when we expect no compression."""

def verify_wire_bytes(self: typing.Any, bytes_in, bytes_out):
def verify_wire_bytes(self, bytes_in, bytes_out):
# Bytes out includes the 4-byte mask key per message.
self.assertEqual(bytes_out, 3 * (len(self.MESSAGE) + 6))
self.assertEqual(bytes_in, 3 * (len(self.MESSAGE) + 2))


class NoCompressionTest(UncompressedTestMixin, WebSocketBaseTestCase):
class NoCompressionTest(UncompressedTestMixin):
pass


# If only one side tries to compress, the extension is not negotiated.
class ServerOnlyCompressionTest(UncompressedTestMixin, WebSocketBaseTestCase):
class ServerOnlyCompressionTest(UncompressedTestMixin):
def get_server_compression_options(self):
return {}


class ClientOnlyCompressionTest(UncompressedTestMixin, WebSocketBaseTestCase):
class ClientOnlyCompressionTest(UncompressedTestMixin):
def get_client_compression_options(self):
return {}


class DefaultCompressionTest(CompressionTestMixin, WebSocketBaseTestCase):
class DefaultCompressionTest(CompressionTestMixin):
def get_server_compression_options(self):
return {}

Expand All @@ -766,7 +769,8 @@ def verify_wire_bytes(self, bytes_in, bytes_out):
self.assertEqual(bytes_out, bytes_in + 12)


class MaskFunctionMixin:
@abstract_base_test
class MaskFunctionMixin(unittest.TestCase):
# Subclasses should define self.mask(mask, data)
def mask(self, mask: bytes, data: bytes) -> bytes:
raise NotImplementedError()
Expand All @@ -789,13 +793,13 @@ def test_mask(self: typing.Any):
)


class PythonMaskFunctionTest(MaskFunctionMixin, unittest.TestCase):
class PythonMaskFunctionTest(MaskFunctionMixin):
def mask(self, mask, data):
return _websocket_mask_python(mask, data)


@unittest.skipIf(speedups is None, "tornado.speedups module not present")
class CythonMaskFunctionTest(MaskFunctionMixin, unittest.TestCase):
class CythonMaskFunctionTest(MaskFunctionMixin):
def mask(self, mask, data):
return speedups.websocket_mask(mask, data)

Expand Down

0 comments on commit 5d2d43f

Please sign in to comment.