Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3cd863c

Browse files
committed
asyncio/tests: Fix ResourceWarnings related to unclosed transports
1 parent 5f68ca6 commit 3cd863c

1 file changed

Lines changed: 29 additions & 18 deletions

File tree

Lib/test/test_asyncio/test_base_events.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,21 +1157,28 @@ def getaddrinfo(*args, **kw):
11571157
self.loop.add_writer = mock.Mock()
11581158
self.loop.add_writer._is_coroutine = False
11591159

1160-
coro = self.loop.create_connection(MyProto, '1.2.3.4', 80)
1161-
self.loop.run_until_complete(coro)
1162-
sock.connect.assert_called_with(('1.2.3.4', 80))
1163-
m_socket.socket.assert_called_with(family=m_socket.AF_INET,
1164-
proto=m_socket.IPPROTO_TCP,
1165-
type=m_socket.SOCK_STREAM)
1160+
coro = self.loop.create_connection(asyncio.Protocol, '1.2.3.4', 80)
1161+
t, p = self.loop.run_until_complete(coro)
1162+
try:
1163+
sock.connect.assert_called_with(('1.2.3.4', 80))
1164+
m_socket.socket.assert_called_with(family=m_socket.AF_INET,
1165+
proto=m_socket.IPPROTO_TCP,
1166+
type=m_socket.SOCK_STREAM)
1167+
finally:
1168+
t.close()
1169+
test_utils.run_briefly(self.loop) # allow transport to close
11661170

11671171
sock.family = socket.AF_INET6
1168-
coro = self.loop.create_connection(MyProto, '::2', 80)
1169-
1170-
self.loop.run_until_complete(coro)
1171-
sock.connect.assert_called_with(('::2', 80))
1172-
m_socket.socket.assert_called_with(family=m_socket.AF_INET6,
1173-
proto=m_socket.IPPROTO_TCP,
1174-
type=m_socket.SOCK_STREAM)
1172+
coro = self.loop.create_connection(asyncio.Protocol, '::2', 80)
1173+
t, p = self.loop.run_until_complete(coro)
1174+
try:
1175+
sock.connect.assert_called_with(('::2', 80))
1176+
m_socket.socket.assert_called_with(family=m_socket.AF_INET6,
1177+
proto=m_socket.IPPROTO_TCP,
1178+
type=m_socket.SOCK_STREAM)
1179+
finally:
1180+
t.close()
1181+
test_utils.run_briefly(self.loop) # allow transport to close
11751182

11761183
@patch_socket
11771184
def test_create_connection_ip_addr(self, m_socket):
@@ -1559,11 +1566,15 @@ def getaddrinfo(*args, **kw):
15591566
reuse_address=False,
15601567
reuse_port=reuseport_supported)
15611568

1562-
self.loop.run_until_complete(coro)
1563-
bind.assert_called_with(('1.2.3.4', 0))
1564-
m_socket.socket.assert_called_with(family=m_socket.AF_INET,
1565-
proto=m_socket.IPPROTO_UDP,
1566-
type=m_socket.SOCK_DGRAM)
1569+
t, p = self.loop.run_until_complete(coro)
1570+
try:
1571+
bind.assert_called_with(('1.2.3.4', 0))
1572+
m_socket.socket.assert_called_with(family=m_socket.AF_INET,
1573+
proto=m_socket.IPPROTO_UDP,
1574+
type=m_socket.SOCK_DGRAM)
1575+
finally:
1576+
t.close()
1577+
test_utils.run_briefly(self.loop) # allow transport to close
15671578

15681579
def test_accept_connection_retry(self):
15691580
sock = mock.Mock()

0 commit comments

Comments
 (0)