Skip to content

Commit 5ce814b

Browse files
committed
Simplify tests of safety net in connection classes.
1 parent 5b3332f commit 5ce814b

File tree

2 files changed

+4
-28
lines changed

2 files changed

+4
-28
lines changed

tests/asyncio/test_connection.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,34 +1250,22 @@ async def test_writing_in_send_context_fails(self):
12501250

12511251
# Test safety nets — catching all exceptions in case of bugs.
12521252

1253-
# Inject a fault in a random call in data_received().
1254-
# This test is tightly coupled to the implementation.
12551253
@patch("websockets.protocol.Protocol.events_received", side_effect=AssertionError)
12561254
async def test_unexpected_failure_in_data_received(self, events_received):
12571255
"""Unexpected internal error in data_received() is correctly reported."""
1258-
# Receive a message to trigger the fault.
12591256
await self.remote_connection.send("😀")
1260-
12611257
with self.assertRaises(ConnectionClosedError) as raised:
12621258
await self.connection.recv()
1259+
self.assertIsInstance(raised.exception.__cause__, AssertionError)
12631260

1264-
exc = raised.exception
1265-
self.assertEqual(str(exc), "no close frame received or sent")
1266-
self.assertIsInstance(exc.__cause__, AssertionError)
1267-
1268-
# Inject a fault in a random call in send_context().
1269-
# This test is tightly coupled to the implementation.
12701261
@patch("websockets.protocol.Protocol.send_text", side_effect=AssertionError)
12711262
async def test_unexpected_failure_in_send_context(self, send_text):
12721263
"""Unexpected internal error in send_context() is correctly reported."""
12731264
# Send a message to trigger the fault.
12741265
# The connection closed exception reports the injected fault.
12751266
with self.assertRaises(ConnectionClosedError) as raised:
12761267
await self.connection.send("😀")
1277-
1278-
exc = raised.exception
1279-
self.assertEqual(str(exc), "no close frame received or sent")
1280-
self.assertIsInstance(exc.__cause__, AssertionError)
1268+
self.assertIsInstance(raised.exception.__cause__, AssertionError)
12811269

12821270
# Test broadcast.
12831271

tests/sync/test_connection.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -960,34 +960,22 @@ def test_writing_in_send_context_fails(self):
960960

961961
# Test safety nets — catching all exceptions in case of bugs.
962962

963-
# Inject a fault in a random call in recv_events().
964-
# This test is tightly coupled to the implementation.
965963
@patch("websockets.protocol.Protocol.events_received", side_effect=AssertionError)
966964
def test_unexpected_failure_in_recv_events(self, events_received):
967965
"""Unexpected internal error in recv_events() is correctly reported."""
968-
# Receive a message to trigger the fault.
969966
self.remote_connection.send("😀")
970-
971967
with self.assertRaises(ConnectionClosedError) as raised:
972968
self.connection.recv()
969+
self.assertIsInstance(raised.exception.__cause__, AssertionError)
973970

974-
exc = raised.exception
975-
self.assertEqual(str(exc), "no close frame received or sent")
976-
self.assertIsInstance(exc.__cause__, AssertionError)
977-
978-
# Inject a fault in a random call in send_context().
979-
# This test is tightly coupled to the implementation.
980971
@patch("websockets.protocol.Protocol.send_text", side_effect=AssertionError)
981972
def test_unexpected_failure_in_send_context(self, send_text):
982973
"""Unexpected internal error in send_context() is correctly reported."""
983974
# Send a message to trigger the fault.
984975
# The connection closed exception reports the injected fault.
985976
with self.assertRaises(ConnectionClosedError) as raised:
986977
self.connection.send("😀")
987-
988-
exc = raised.exception
989-
self.assertEqual(str(exc), "no close frame received or sent")
990-
self.assertIsInstance(exc.__cause__, AssertionError)
978+
self.assertIsInstance(raised.exception.__cause__, AssertionError)
991979

992980

993981
class ServerConnectionTests(ClientConnectionTests):

0 commit comments

Comments
 (0)