diff options
author | Brian Curtin <brian.curtin@gmail.com> | 2010-11-02 02:07:09 (GMT) |
---|---|---|
committer | Brian Curtin <brian.curtin@gmail.com> | 2010-11-02 02:07:09 (GMT) |
commit | 21ebbb7d3918db6101c3c49ee04cac95d156003d (patch) | |
tree | 0c83c89234851afbdc25e263e0009d1e6c3eec72 /Lib | |
parent | be647e288dc9061f8c8b3010fb5379e197407af1 (diff) | |
download | cpython-21ebbb7d3918db6101c3c49ee04cac95d156003d.zip cpython-21ebbb7d3918db6101c3c49ee04cac95d156003d.tar.gz cpython-21ebbb7d3918db6101c3c49ee04cac95d156003d.tar.bz2 |
Clean up ResourceWarnings due to unclosed sockets.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_socket.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index e20364d..704d46e 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -259,6 +259,7 @@ class GeneralModuleTests(unittest.TestCase): def test_repr(self): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.addCleanup(s.close) self.assertTrue(repr(s).startswith("<socket.socket object")) def test_weakref(self): @@ -532,6 +533,7 @@ class GeneralModuleTests(unittest.TestCase): # Testing getsockname() port = self._get_unused_port() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.addCleanup(sock.close) sock.bind(("0.0.0.0", port)) name = sock.getsockname() # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate @@ -545,12 +547,14 @@ class GeneralModuleTests(unittest.TestCase): # Testing getsockopt() # We know a socket should start without reuse==0 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.addCleanup(sock.close) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) self.assertFalse(reuse != 0, "initial mode is reuse") def testSetSockOpt(self): # Testing setsockopt() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.addCleanup(sock.close) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) self.assertFalse(reuse == 0, "failed to set reuse mode") @@ -784,6 +788,7 @@ class BasicTCPTest(SocketConnectedTest): # Testing fromfd() fd = self.cli_conn.fileno() sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) + self.addCleanup(sock.close) self.assertIsInstance(sock, socket.socket) msg = sock.recv(1024) self.assertEqual(msg, MSG) @@ -794,6 +799,7 @@ class BasicTCPTest(SocketConnectedTest): def testDup(self): # Testing dup() sock = self.cli_conn.dup() + self.addCleanup(sock.close) msg = sock.recv(1024) self.assertEqual(msg, MSG) @@ -824,6 +830,7 @@ class BasicTCPTest(SocketConnectedTest): # ...but we can create another socket using the (still open) # file descriptor sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f) + self.addCleanup(sock.close) msg = sock.recv(1024) self.assertEqual(msg, MSG) @@ -970,6 +977,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest): read, write, err = select.select([self.serv], [], []) if self.serv in read: conn, addr = self.serv.accept() + conn.close() else: self.fail("Error trying to do accept after select.") @@ -980,6 +988,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest): def testConnect(self): # Testing non-blocking connect conn, addr = self.serv.accept() + conn.close() def _testConnect(self): self.cli.settimeout(10) @@ -998,6 +1007,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest): read, write, err = select.select([conn], [], []) if conn in read: msg = conn.recv(len(MSG)) + conn.close() self.assertEqual(msg, MSG) else: self.fail("Error during select call to non-blocking socket.") @@ -1441,6 +1451,7 @@ class NetworkConnectionNoServer(unittest.TestCase): def test_connect(self): port = support.find_unused_port() cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.addCleanup(cli.close) with self.assertRaises(socket.error) as cm: cli.connect((HOST, port)) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) @@ -1478,16 +1489,19 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): def _justAccept(self): conn, addr = self.serv.accept() + conn.close() testFamily = _justAccept def _testFamily(self): self.cli = socket.create_connection((HOST, self.port), timeout=30) + self.addCleanup(self.cli.close) self.assertEqual(self.cli.family, 2) testSourceAddress = _justAccept def _testSourceAddress(self): self.cli = socket.create_connection((HOST, self.port), timeout=30, source_address=('', self.source_port)) + self.addCleanup(self.cli.close) self.assertEqual(self.cli.getsockname()[1], self.source_port) # The port number being used is sufficient to show that the bind() # call happened. @@ -1499,6 +1513,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): socket.setdefaulttimeout(42) try: self.cli = socket.create_connection((HOST, self.port)) + self.addCleanup(self.cli.close) finally: socket.setdefaulttimeout(None) self.assertEquals(self.cli.gettimeout(), 42) @@ -1510,6 +1525,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): socket.setdefaulttimeout(30) try: self.cli = socket.create_connection((HOST, self.port), timeout=None) + self.addCleanup(self.cli.close) finally: socket.setdefaulttimeout(None) self.assertEqual(self.cli.gettimeout(), None) @@ -1522,6 +1538,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): testTimeoutValueNonamed = _justAccept def _testTimeoutValueNonamed(self): self.cli = socket.create_connection((HOST, self.port), 30) + self.addCleanup(self.cli.close) self.assertEqual(self.cli.gettimeout(), 30) @unittest.skipUnless(thread, 'Threading required for this test.') @@ -1541,6 +1558,7 @@ class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): def testInsideTimeout(self): conn, addr = self.serv.accept() + self.addCleanup(conn.close) time.sleep(3) conn.send(b"done!") testOutsideTimeout = testInsideTimeout @@ -1834,6 +1852,7 @@ class ContextManagersTest(ThreadedTCPSocketTest): def testCreateConnectionBase(self): conn, addr = self.serv.accept() + self.addCleanup(conn.close) data = conn.recv(1024) conn.sendall(data) @@ -1847,6 +1866,7 @@ class ContextManagersTest(ThreadedTCPSocketTest): def testCreateConnectionClose(self): conn, addr = self.serv.accept() + self.addCleanup(conn.close) data = conn.recv(1024) conn.sendall(data) |