Skip to content

Commit 80d8a3b

Browse files
committed
Actually check for waits in connection tests.
1 parent cf7179f commit 80d8a3b

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

tests/asyncio/test_connection.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ class ClientConnectionTests(unittest.IsolatedAsyncioTestCase):
3232
REMOTE = SERVER
3333

3434
async def asyncSetUp(self):
35-
loop = asyncio.get_running_loop()
35+
self.loop = asyncio.get_running_loop()
3636
socket_, remote_socket = socket.socketpair()
37-
self.transport, self.connection = await loop.create_connection(
37+
self.transport, self.connection = await self.loop.create_connection(
3838
lambda: Connection(Protocol(self.LOCAL), close_timeout=2 * MS),
3939
sock=socket_,
4040
)
41-
self.remote_transport, self.remote_connection = await loop.create_connection(
41+
_remote_transport, self.remote_connection = await self.loop.create_connection(
4242
lambda: InterceptingConnection(RecordingProtocol(self.REMOTE)),
4343
sock=remote_socket,
4444
)
@@ -710,9 +710,15 @@ async def test_close_explicit_code_reason(self):
710710
await self.assertFrameSent(Frame(Opcode.CLOSE, b"\x03\xe9bye!"))
711711

712712
async def test_close_waits_for_close_frame(self):
713-
"""close waits for a close frame (then EOF) before returning."""
713+
"""close waits for a close frame then EOF before returning."""
714+
t0 = self.loop.time()
714715
async with self.delay_frames_rcvd(MS), self.delay_eof_rcvd(MS):
715716
await self.connection.close()
717+
t1 = self.loop.time()
718+
719+
self.assertEqual(self.connection.state, State.CLOSED)
720+
self.assertEqual(self.connection.close_code, CloseCode.NORMAL_CLOSURE)
721+
self.assertGreater(t1 - t0, MS)
716722

717723
with self.assertRaises(ConnectionClosedOK) as raised:
718724
await self.connection.recv()
@@ -726,8 +732,14 @@ async def test_close_waits_for_connection_closed(self):
726732
if self.LOCAL is SERVER:
727733
self.skipTest("only relevant on the client-side")
728734

735+
t0 = self.loop.time()
729736
async with self.delay_eof_rcvd(MS):
730737
await self.connection.close()
738+
t1 = self.loop.time()
739+
740+
self.assertEqual(self.connection.state, State.CLOSED)
741+
self.assertEqual(self.connection.close_code, CloseCode.NORMAL_CLOSURE)
742+
self.assertGreater(t1 - t0, MS)
731743

732744
with self.assertRaises(ConnectionClosedOK) as raised:
733745
await self.connection.recv()
@@ -737,11 +749,17 @@ async def test_close_waits_for_connection_closed(self):
737749
self.assertIsNone(exc.__cause__)
738750

739751
async def test_close_no_timeout_waits_for_close_frame(self):
740-
"""close without timeout waits for a close frame (then EOF) before returning."""
752+
"""close without timeout waits for a close frame then EOF before returning."""
741753
self.connection.close_timeout = None
742754

755+
t0 = self.loop.time()
743756
async with self.delay_frames_rcvd(MS), self.delay_eof_rcvd(MS):
744757
await self.connection.close()
758+
t1 = self.loop.time()
759+
760+
self.assertEqual(self.connection.state, State.CLOSED)
761+
self.assertEqual(self.connection.close_code, CloseCode.NORMAL_CLOSURE)
762+
self.assertGreater(t1 - t0, MS)
745763

746764
with self.assertRaises(ConnectionClosedOK) as raised:
747765
await self.connection.recv()
@@ -757,8 +775,14 @@ async def test_close_no_timeout_waits_for_connection_closed(self):
757775

758776
self.connection.close_timeout = None
759777

778+
t0 = self.loop.time()
760779
async with self.delay_eof_rcvd(MS):
761780
await self.connection.close()
781+
t1 = self.loop.time()
782+
783+
self.assertEqual(self.connection.state, State.CLOSED)
784+
self.assertEqual(self.connection.close_code, CloseCode.NORMAL_CLOSURE)
785+
self.assertGreater(t1 - t0, MS)
762786

763787
with self.assertRaises(ConnectionClosedOK) as raised:
764788
await self.connection.recv()
@@ -769,8 +793,14 @@ async def test_close_no_timeout_waits_for_connection_closed(self):
769793

770794
async def test_close_timeout_waiting_for_close_frame(self):
771795
"""close times out if no close frame is received."""
796+
t0 = self.loop.time()
772797
async with self.drop_eof_rcvd(), self.drop_frames_rcvd():
773798
await self.connection.close()
799+
t1 = self.loop.time()
800+
801+
self.assertEqual(self.connection.state, State.CLOSED)
802+
self.assertEqual(self.connection.close_code, CloseCode.ABNORMAL_CLOSURE)
803+
self.assertGreater(t1 - t0, 2 * MS)
774804

775805
with self.assertRaises(ConnectionClosedError) as raised:
776806
await self.connection.recv()
@@ -784,8 +814,14 @@ async def test_close_timeout_waiting_for_connection_closed(self):
784814
if self.LOCAL is SERVER:
785815
self.skipTest("only relevant on the client-side")
786816

817+
t0 = self.loop.time()
787818
async with self.drop_eof_rcvd():
788819
await self.connection.close()
820+
t1 = self.loop.time()
821+
822+
self.assertEqual(self.connection.state, State.CLOSED)
823+
self.assertEqual(self.connection.close_code, CloseCode.NORMAL_CLOSURE)
824+
self.assertGreater(t1 - t0, 2 * MS)
789825

790826
with self.assertRaises(ConnectionClosedOK) as raised:
791827
await self.connection.recv()

tests/sync/test_connection.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,15 @@ def test_close_explicit_code_reason(self):
477477
self.assertFrameSent(Frame(Opcode.CLOSE, b"\x03\xe9bye!"))
478478

479479
def test_close_waits_for_close_frame(self):
480-
"""close waits for a close frame (then EOF) before returning."""
480+
"""close waits for a close frame then EOF before returning."""
481+
t0 = time.time()
481482
with self.delay_frames_rcvd(MS):
482483
self.connection.close()
484+
t1 = time.time()
485+
486+
self.assertEqual(self.connection.state, State.CLOSED)
487+
self.assertEqual(self.connection.close_code, CloseCode.NORMAL_CLOSURE)
488+
self.assertGreater(t1 - t0, MS)
483489

484490
with self.assertRaises(ConnectionClosedOK) as raised:
485491
self.connection.recv()
@@ -493,8 +499,14 @@ def test_close_waits_for_connection_closed(self):
493499
if self.LOCAL is SERVER:
494500
self.skipTest("only relevant on the client-side")
495501

502+
t0 = time.time()
496503
with self.delay_eof_rcvd(MS):
497504
self.connection.close()
505+
t1 = time.time()
506+
507+
self.assertEqual(self.connection.state, State.CLOSED)
508+
self.assertEqual(self.connection.close_code, CloseCode.NORMAL_CLOSURE)
509+
self.assertGreater(t1 - t0, MS)
498510

499511
with self.assertRaises(ConnectionClosedOK) as raised:
500512
self.connection.recv()
@@ -505,8 +517,14 @@ def test_close_waits_for_connection_closed(self):
505517

506518
def test_close_timeout_waiting_for_close_frame(self):
507519
"""close times out if no close frame is received."""
520+
t0 = time.time()
508521
with self.drop_frames_rcvd(), self.drop_eof_rcvd():
509522
self.connection.close()
523+
t1 = time.time()
524+
525+
self.assertEqual(self.connection.state, State.CLOSED)
526+
self.assertEqual(self.connection.close_code, CloseCode.ABNORMAL_CLOSURE)
527+
self.assertGreater(t1 - t0, 2 * MS)
510528

511529
with self.assertRaises(ConnectionClosedError) as raised:
512530
self.connection.recv()
@@ -520,8 +538,14 @@ def test_close_timeout_waiting_for_connection_closed(self):
520538
if self.LOCAL is SERVER:
521539
self.skipTest("only relevant on the client-side")
522540

541+
t0 = time.time()
523542
with self.drop_eof_rcvd():
524543
self.connection.close()
544+
t1 = time.time()
545+
546+
self.assertEqual(self.connection.state, State.CLOSED)
547+
self.assertEqual(self.connection.close_code, CloseCode.NORMAL_CLOSURE)
548+
self.assertGreater(t1 - t0, 2 * MS)
525549

526550
with self.assertRaises(ConnectionClosedOK) as raised:
527551
self.connection.recv()

0 commit comments

Comments
 (0)