@@ -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
0 commit comments