Skip to content

Commit f8ed666

Browse files
committed
Eliminate the risk of a busy loop in tests.
If these tests reached the point where websockets tried to send a second ping, it would loop on trying to find a payload not sent yet.
1 parent 80d8a3b commit f8ed666

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

tests/asyncio/test_connection.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import contextlib
3+
import itertools
34
import logging
45
import socket
56
import sys
@@ -903,9 +904,10 @@ async def test_wait_closed(self):
903904

904905
# Test ping.
905906

906-
@patch("random.getrandbits", return_value=1918987876)
907+
@patch("random.getrandbits")
907908
async def test_ping(self, getrandbits):
908909
"""ping sends a ping frame with a random payload."""
910+
getrandbits.side_effect = itertools.count(1918987876)
909911
await self.connection.ping()
910912
getrandbits.assert_called_once_with(32)
911913
await self.assertFrameSent(Frame(Opcode.PING, b"rand"))
@@ -1014,9 +1016,10 @@ async def test_pong_unsupported_type(self):
10141016

10151017
# Test keepalive.
10161018

1017-
@patch("random.getrandbits", return_value=1918987876)
1019+
@patch("random.getrandbits")
10181020
async def test_keepalive(self, getrandbits):
10191021
"""keepalive sends pings at ping_interval and measures latency."""
1022+
getrandbits.side_effect = itertools.count(1918987876)
10201023
self.connection.ping_interval = 3 * MS
10211024
self.connection.start_keepalive()
10221025
self.assertIsNotNone(self.connection.keepalive_task)
@@ -1035,9 +1038,10 @@ async def test_disable_keepalive(self):
10351038
self.connection.start_keepalive()
10361039
self.assertIsNone(self.connection.keepalive_task)
10371040

1038-
@patch("random.getrandbits", return_value=1918987876)
1041+
@patch("random.getrandbits")
10391042
async def test_keepalive_times_out(self, getrandbits):
10401043
"""keepalive closes the connection if ping_timeout elapses."""
1044+
getrandbits.side_effect = itertools.count(1918987876)
10411045
self.connection.ping_interval = 4 * MS
10421046
self.connection.ping_timeout = 2 * MS
10431047
async with self.drop_frames_rcvd():
@@ -1050,9 +1054,10 @@ async def test_keepalive_times_out(self, getrandbits):
10501054
# 7 ms: check that the connection is closed.
10511055
self.assertEqual(self.connection.state, State.CLOSED)
10521056

1053-
@patch("random.getrandbits", return_value=1918987876)
1057+
@patch("random.getrandbits")
10541058
async def test_keepalive_ignores_timeout(self, getrandbits):
10551059
"""keepalive ignores timeouts if ping_timeout isn't set."""
1060+
getrandbits.side_effect = itertools.count(1918987876)
10561061
self.connection.ping_interval = 4 * MS
10571062
self.connection.ping_timeout = None
10581063
async with self.drop_frames_rcvd():

tests/sync/test_connection.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
import itertools
23
import logging
34
import socket
45
import sys
@@ -643,9 +644,10 @@ def fragments():
643644

644645
# Test ping.
645646

646-
@patch("random.getrandbits", return_value=1918987876)
647+
@patch("random.getrandbits")
647648
def test_ping(self, getrandbits):
648649
"""ping sends a ping frame with a random payload."""
650+
getrandbits.side_effect = itertools.count(1918987876)
649651
self.connection.ping()
650652
getrandbits.assert_called_once_with(32)
651653
self.assertFrameSent(Frame(Opcode.PING, b"rand"))
@@ -737,9 +739,10 @@ def test_pong_unsupported_type(self):
737739

738740
# Test keepalive.
739741

740-
@patch("random.getrandbits", return_value=1918987876)
742+
@patch("random.getrandbits")
741743
def test_keepalive(self, getrandbits):
742744
"""keepalive sends pings at ping_interval and measures latency."""
745+
getrandbits.side_effect = itertools.count(1918987876)
743746
self.connection.ping_interval = 4 * MS
744747
self.connection.start_keepalive()
745748
self.assertIsNotNone(self.connection.keepalive_thread)
@@ -758,9 +761,10 @@ def test_disable_keepalive(self):
758761
self.connection.start_keepalive()
759762
self.assertIsNone(self.connection.keepalive_thread)
760763

761-
@patch("random.getrandbits", return_value=1918987876)
764+
@patch("random.getrandbits")
762765
def test_keepalive_times_out(self, getrandbits):
763766
"""keepalive closes the connection if ping_timeout elapses."""
767+
getrandbits.side_effect = itertools.count(1918987876)
764768
self.connection.ping_interval = 4 * MS
765769
self.connection.ping_timeout = 2 * MS
766770
with self.drop_frames_rcvd():
@@ -774,9 +778,10 @@ def test_keepalive_times_out(self, getrandbits):
774778
# 7 ms: check that the connection is closed.
775779
self.assertEqual(self.connection.state, State.CLOSED)
776780

777-
@patch("random.getrandbits", return_value=1918987876)
781+
@patch("random.getrandbits")
778782
def test_keepalive_ignores_timeout(self, getrandbits):
779783
"""keepalive ignores timeouts if ping_timeout isn't set."""
784+
getrandbits.side_effect = itertools.count(1918987876)
780785
self.connection.ping_interval = 4 * MS
781786
self.connection.ping_timeout = None
782787
with self.drop_frames_rcvd():

0 commit comments

Comments
 (0)