Skip to content

Commit 37a8f1b

Browse files
committed
Close iterators in connection tests.
1 parent 7d5b63c commit 37a8f1b

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed

tests/asyncio/test_connection.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -124,41 +124,46 @@ async def test_exit_with_exception(self):
124124

125125
async def test_aiter_text(self):
126126
"""__aiter__ yields text messages."""
127-
aiterator = aiter(self.connection)
128-
await self.remote_connection.send("😀")
129-
self.assertEqual(await anext(aiterator), "😀")
130-
await self.remote_connection.send("😀")
131-
self.assertEqual(await anext(aiterator), "😀")
127+
iterator = aiter(self.connection)
128+
async with contextlib.aclosing(iterator):
129+
await self.remote_connection.send("😀")
130+
self.assertEqual(await anext(iterator), "😀")
131+
await self.remote_connection.send("😀")
132+
self.assertEqual(await anext(iterator), "😀")
132133

133134
async def test_aiter_binary(self):
134135
"""__aiter__ yields binary messages."""
135-
aiterator = aiter(self.connection)
136-
await self.remote_connection.send(b"\x01\x02\xfe\xff")
137-
self.assertEqual(await anext(aiterator), b"\x01\x02\xfe\xff")
138-
await self.remote_connection.send(b"\x01\x02\xfe\xff")
139-
self.assertEqual(await anext(aiterator), b"\x01\x02\xfe\xff")
136+
iterator = aiter(self.connection)
137+
async with contextlib.aclosing(iterator):
138+
await self.remote_connection.send(b"\x01\x02\xfe\xff")
139+
self.assertEqual(await anext(iterator), b"\x01\x02\xfe\xff")
140+
await self.remote_connection.send(b"\x01\x02\xfe\xff")
141+
self.assertEqual(await anext(iterator), b"\x01\x02\xfe\xff")
140142

141143
async def test_aiter_mixed(self):
142144
"""__aiter__ yields a mix of text and binary messages."""
143-
aiterator = aiter(self.connection)
144-
await self.remote_connection.send("😀")
145-
self.assertEqual(await anext(aiterator), "😀")
146-
await self.remote_connection.send(b"\x01\x02\xfe\xff")
147-
self.assertEqual(await anext(aiterator), b"\x01\x02\xfe\xff")
145+
iterator = aiter(self.connection)
146+
async with contextlib.aclosing(iterator):
147+
await self.remote_connection.send("😀")
148+
self.assertEqual(await anext(iterator), "😀")
149+
await self.remote_connection.send(b"\x01\x02\xfe\xff")
150+
self.assertEqual(await anext(iterator), b"\x01\x02\xfe\xff")
148151

149152
async def test_aiter_connection_closed_ok(self):
150153
"""__aiter__ terminates after a normal closure."""
151-
aiterator = aiter(self.connection)
152-
await self.remote_connection.close()
153-
with self.assertRaises(StopAsyncIteration):
154-
await anext(aiterator)
154+
iterator = aiter(self.connection)
155+
async with contextlib.aclosing(iterator):
156+
await self.remote_connection.close()
157+
with self.assertRaises(StopAsyncIteration):
158+
await anext(iterator)
155159

156160
async def test_aiter_connection_closed_error(self):
157161
"""__aiter__ raises ConnectionClosedError after an error."""
158-
aiterator = aiter(self.connection)
159-
await self.remote_connection.close(code=CloseCode.INTERNAL_ERROR)
160-
with self.assertRaises(ConnectionClosedError):
161-
await anext(aiterator)
162+
iterator = aiter(self.connection)
163+
async with contextlib.aclosing(iterator):
164+
await self.remote_connection.close(code=CloseCode.INTERNAL_ERROR)
165+
with self.assertRaises(ConnectionClosedError):
166+
await anext(iterator)
162167

163168
# Test recv.
164169

tests/sync/test_connection.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,40 +107,45 @@ def test_exit_with_exception(self):
107107
def test_iter_text(self):
108108
"""__iter__ yields text messages."""
109109
iterator = iter(self.connection)
110-
self.remote_connection.send("😀")
111-
self.assertEqual(next(iterator), "😀")
112-
self.remote_connection.send("😀")
113-
self.assertEqual(next(iterator), "😀")
110+
with contextlib.closing(iterator):
111+
self.remote_connection.send("😀")
112+
self.assertEqual(next(iterator), "😀")
113+
self.remote_connection.send("😀")
114+
self.assertEqual(next(iterator), "😀")
114115

115116
def test_iter_binary(self):
116117
"""__iter__ yields binary messages."""
117118
iterator = iter(self.connection)
118-
self.remote_connection.send(b"\x01\x02\xfe\xff")
119-
self.assertEqual(next(iterator), b"\x01\x02\xfe\xff")
120-
self.remote_connection.send(b"\x01\x02\xfe\xff")
121-
self.assertEqual(next(iterator), b"\x01\x02\xfe\xff")
119+
with contextlib.closing(iterator):
120+
self.remote_connection.send(b"\x01\x02\xfe\xff")
121+
self.assertEqual(next(iterator), b"\x01\x02\xfe\xff")
122+
self.remote_connection.send(b"\x01\x02\xfe\xff")
123+
self.assertEqual(next(iterator), b"\x01\x02\xfe\xff")
122124

123125
def test_iter_mixed(self):
124126
"""__iter__ yields a mix of text and binary messages."""
125127
iterator = iter(self.connection)
126-
self.remote_connection.send("😀")
127-
self.assertEqual(next(iterator), "😀")
128-
self.remote_connection.send(b"\x01\x02\xfe\xff")
129-
self.assertEqual(next(iterator), b"\x01\x02\xfe\xff")
128+
with contextlib.closing(iterator):
129+
self.remote_connection.send("😀")
130+
self.assertEqual(next(iterator), "😀")
131+
self.remote_connection.send(b"\x01\x02\xfe\xff")
132+
self.assertEqual(next(iterator), b"\x01\x02\xfe\xff")
130133

131134
def test_iter_connection_closed_ok(self):
132135
"""__iter__ terminates after a normal closure."""
133136
iterator = iter(self.connection)
134-
self.remote_connection.close()
135-
with self.assertRaises(StopIteration):
136-
next(iterator)
137+
with contextlib.closing(iterator):
138+
self.remote_connection.close()
139+
with self.assertRaises(StopIteration):
140+
next(iterator)
137141

138142
def test_iter_connection_closed_error(self):
139143
"""__iter__ raises ConnectionClosedError after an error."""
140144
iterator = iter(self.connection)
141-
self.remote_connection.close(code=CloseCode.INTERNAL_ERROR)
142-
with self.assertRaises(ConnectionClosedError):
143-
next(iterator)
145+
with contextlib.closing(iterator):
146+
self.remote_connection.close(code=CloseCode.INTERNAL_ERROR)
147+
with self.assertRaises(ConnectionClosedError):
148+
next(iterator)
144149

145150
# Test recv.
146151

0 commit comments

Comments
 (0)