Skip to content

Commit 76f6f57

Browse files
committed
Factor out backport of assertNoLogs.
Fix previous commit on Python 3.9.
1 parent cdeb882 commit 76f6f57

File tree

5 files changed

+41
-48
lines changed

5 files changed

+41
-48
lines changed

tests/asyncio/test_connection.py

+4-21
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from websockets.protocol import CLIENT, SERVER, Protocol, State
2020

2121
from ..protocol import RecordingProtocol
22-
from ..utils import MS
22+
from ..utils import MS, AssertNoLogsMixin
2323
from .connection import InterceptingConnection
2424
from .utils import alist
2525

@@ -28,7 +28,7 @@
2828
# All tests run on the client side and the server side to validate this.
2929

3030

31-
class ClientConnectionTests(unittest.IsolatedAsyncioTestCase):
31+
class ClientConnectionTests(AssertNoLogsMixin, unittest.IsolatedAsyncioTestCase):
3232
LOCAL = CLIENT
3333
REMOTE = SERVER
3434

@@ -48,23 +48,6 @@ async def asyncTearDown(self):
4848
await self.remote_connection.close()
4949
await self.connection.close()
5050

51-
if sys.version_info[:2] < (3, 10): # pragma: no cover
52-
53-
@contextlib.contextmanager
54-
def assertNoLogs(self, logger=None, level=None):
55-
"""
56-
No message is logged on the given logger with at least the given level.
57-
58-
"""
59-
with self.assertLogs(logger, level) as logs:
60-
# We want to test that no log message is emitted
61-
# but assertLogs expects at least one log message.
62-
logging.getLogger(logger).log(level, "dummy")
63-
yield
64-
65-
level_name = logging.getLevelName(level)
66-
self.assertEqual(logs.output, [f"{level_name}:{logger}:dummy"])
67-
6851
# Test helpers built upon RecordingProtocol and InterceptingConnection.
6952

7053
async def assertFrameSent(self, frame):
@@ -1277,7 +1260,7 @@ async def test_broadcast_skips_closed_connection(self):
12771260
await self.connection.close()
12781261
await self.assertFrameSent(Frame(Opcode.CLOSE, b"\x03\xe8"))
12791262

1280-
with self.assertNoLogs():
1263+
with self.assertNoLogs("websockets", logging.WARNING):
12811264
broadcast([self.connection], "😀")
12821265
await self.assertNoFrameSent()
12831266

@@ -1288,7 +1271,7 @@ async def test_broadcast_skips_closing_connection(self):
12881271
await asyncio.sleep(0)
12891272
await self.assertFrameSent(Frame(Opcode.CLOSE, b"\x03\xe8"))
12901273

1291-
with self.assertNoLogs():
1274+
with self.assertNoLogs("websockets", logging.WARNING):
12921275
broadcast([self.connection], "😀")
12931276
await self.assertNoFrameSent()
12941277

tests/asyncio/test_server.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
CLIENT_CONTEXT,
2222
MS,
2323
SERVER_CONTEXT,
24+
AssertNoLogsMixin,
2425
temp_unix_socket_path,
2526
)
2627
from .server import (
@@ -32,7 +33,7 @@
3233
)
3334

3435

35-
class ServerTests(EvalShellMixin, unittest.IsolatedAsyncioTestCase):
36+
class ServerTests(EvalShellMixin, AssertNoLogsMixin, unittest.IsolatedAsyncioTestCase):
3637
async def test_connection(self):
3738
"""Server receives connection from client and the handshake succeeds."""
3839
async with serve(*args) as server:

tests/legacy/test_protocol.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ def test_answer_ping_does_not_crash_if_connection_closing(self):
938938
self.receive_frame(Frame(True, OP_PING, b"test"))
939939
self.run_loop_once()
940940

941-
with self.assertNoLogs():
941+
with self.assertNoLogs("websockets", logging.ERROR):
942942
self.loop.run_until_complete(self.protocol.close())
943943

944944
self.loop.run_until_complete(close_task) # cleanup
@@ -951,7 +951,7 @@ def test_answer_ping_does_not_crash_if_connection_closed(self):
951951
self.receive_eof()
952952
self.run_loop_once()
953953

954-
with self.assertNoLogs():
954+
with self.assertNoLogs("websockets", logging.ERROR):
955955
self.loop.run_until_complete(self.protocol.close())
956956

957957
def test_ignore_pong(self):
@@ -1028,7 +1028,7 @@ def test_acknowledge_aborted_ping(self):
10281028
pong_waiter.result()
10291029

10301030
# transfer_data doesn't crash, which would be logged.
1031-
with self.assertNoLogs():
1031+
with self.assertNoLogs("websockets", logging.ERROR):
10321032
# Unclog incoming queue.
10331033
self.loop.run_until_complete(self.protocol.recv())
10341034
self.loop.run_until_complete(self.protocol.recv())
@@ -1375,7 +1375,7 @@ def test_remote_close_and_connection_lost(self):
13751375
self.receive_eof()
13761376
self.run_loop_once()
13771377

1378-
with self.assertNoLogs():
1378+
with self.assertNoLogs("websockets", logging.ERROR):
13791379
self.loop.run_until_complete(self.protocol.close(reason="oh noes!"))
13801380

13811381
self.assertConnectionClosed(CloseCode.NORMAL_CLOSURE, "close")
@@ -1500,14 +1500,14 @@ def test_broadcast_two_clients(self):
15001500
def test_broadcast_skips_closed_connection(self):
15011501
self.close_connection()
15021502

1503-
with self.assertNoLogs():
1503+
with self.assertNoLogs("websockets", logging.ERROR):
15041504
broadcast([self.protocol], "café")
15051505
self.assertNoFrameSent()
15061506

15071507
def test_broadcast_skips_closing_connection(self):
15081508
close_task = self.half_close_connection_local()
15091509

1510-
with self.assertNoLogs():
1510+
with self.assertNoLogs("websockets", logging.ERROR):
15111511
broadcast([self.protocol], "café")
15121512
self.assertNoFrameSent()
15131513

tests/legacy/utils.py

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import asyncio
2-
import contextlib
32
import functools
4-
import logging
53
import sys
64
import unittest
75

6+
from ..utils import AssertNoLogsMixin
87

9-
class AsyncioTestCase(unittest.TestCase):
8+
9+
class AsyncioTestCase(AssertNoLogsMixin, unittest.TestCase):
1010
"""
1111
Base class for tests that sets up an isolated event loop for each test.
1212
@@ -56,23 +56,6 @@ def run_loop_once(self):
5656
self.loop.call_soon(self.loop.stop)
5757
self.loop.run_forever()
5858

59-
if sys.version_info[:2] < (3, 10): # pragma: no cover
60-
61-
@contextlib.contextmanager
62-
def assertNoLogs(self, logger="websockets", level=logging.ERROR):
63-
"""
64-
No message is logged on the given logger with at least the given level.
65-
66-
"""
67-
with self.assertLogs(logger, level) as logs:
68-
# We want to test that no log message is emitted
69-
# but assertLogs expects at least one log message.
70-
logging.getLogger(logger).log(level, "dummy")
71-
yield
72-
73-
level_name = logging.getLevelName(level)
74-
self.assertEqual(logs.output, [f"{level_name}:{logger}:dummy"])
75-
7659
def assertDeprecationWarnings(self, recorded_warnings, expected_warnings):
7760
"""
7861
Check recorded deprecation warnings match a list of expected messages.

tests/utils.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import contextlib
22
import email.utils
3+
import logging
34
import os
45
import pathlib
56
import platform
67
import ssl
8+
import sys
79
import tempfile
810
import time
911
import unittest
@@ -113,6 +115,30 @@ def assertDeprecationWarning(self, message):
113115
self.assertEqual(str(warning.message), message)
114116

115117

118+
class AssertNoLogsMixin:
119+
"""
120+
Backport of assertNoLogs for Python 3.9.
121+
122+
"""
123+
124+
if sys.version_info[:2] < (3, 10): # pragma: no cover
125+
126+
@contextlib.contextmanager
127+
def assertNoLogs(self, logger=None, level=None):
128+
"""
129+
No message is logged on the given logger with at least the given level.
130+
131+
"""
132+
with self.assertLogs(logger, level) as logs:
133+
# We want to test that no log message is emitted
134+
# but assertLogs expects at least one log message.
135+
logging.getLogger(logger).log(level, "dummy")
136+
yield
137+
138+
level_name = logging.getLevelName(level)
139+
self.assertEqual(logs.output, [f"{level_name}:{logger}:dummy"])
140+
141+
116142
@contextlib.contextmanager
117143
def temp_unix_socket_path():
118144
with tempfile.TemporaryDirectory() as temp_dir:

0 commit comments

Comments
 (0)