Merged revisions 86105 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r86105 | brian.curtin | 2010-11-01 21:07:09 -0500 (Mon, 01 Nov 2010) | 2 lines

  Clean up ResourceWarnings due to unclosed sockets.
  Added testDup which was not previously covered in 2.x.
........
This commit is contained in:
Brian Curtin 2010-11-02 02:59:55 +00:00
parent e5dc24e874
commit 62c20b61cd
1 changed files with 26 additions and 0 deletions

View File

@ -512,6 +512,7 @@ class GeneralModuleTests(unittest.TestCase):
# Testing getsockname() # Testing getsockname()
port = self._get_unused_port() port = self._get_unused_port()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.addCleanup(sock.close)
sock.bind(("0.0.0.0", port)) sock.bind(("0.0.0.0", port))
name = sock.getsockname() name = sock.getsockname()
# XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
@ -525,12 +526,14 @@ class GeneralModuleTests(unittest.TestCase):
# Testing getsockopt() # Testing getsockopt()
# We know a socket should start without reuse==0 # We know a socket should start without reuse==0
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.addCleanup(sock.close)
reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
self.assertFalse(reuse != 0, "initial mode is reuse") self.assertFalse(reuse != 0, "initial mode is reuse")
def testSetSockOpt(self): def testSetSockOpt(self):
# Testing setsockopt() # Testing setsockopt()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.addCleanup(sock.close)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
self.assertFalse(reuse == 0, "failed to set reuse mode") self.assertFalse(reuse == 0, "failed to set reuse mode")
@ -712,12 +715,24 @@ class BasicTCPTest(SocketConnectedTest):
return # On Windows, this doesn't exist return # On Windows, this doesn't exist
fd = self.cli_conn.fileno() fd = self.cli_conn.fileno()
sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
self.addCleanup(sock.close)
self.assertIsInstance(sock, socket.socket)
msg = sock.recv(1024) msg = sock.recv(1024)
self.assertEqual(msg, MSG) self.assertEqual(msg, MSG)
def _testFromFd(self): def _testFromFd(self):
self.serv_conn.send(MSG) self.serv_conn.send(MSG)
def testDup(self):
# Testing dup()
sock = self.cli_conn.dup()
self.addCleanup(sock.close)
msg = sock.recv(1024)
self.assertEqual(msg, MSG)
def _testDup(self):
self.serv_conn.send(MSG)
def testShutdown(self): def testShutdown(self):
# Testing shutdown() # Testing shutdown()
msg = self.cli_conn.recv(1024) msg = self.cli_conn.recv(1024)
@ -828,6 +843,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
read, write, err = select.select([self.serv], [], []) read, write, err = select.select([self.serv], [], [])
if self.serv in read: if self.serv in read:
conn, addr = self.serv.accept() conn, addr = self.serv.accept()
conn.close()
else: else:
self.fail("Error trying to do accept after select.") self.fail("Error trying to do accept after select.")
@ -838,6 +854,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
def testConnect(self): def testConnect(self):
# Testing non-blocking connect # Testing non-blocking connect
conn, addr = self.serv.accept() conn, addr = self.serv.accept()
conn.close()
def _testConnect(self): def _testConnect(self):
self.cli.settimeout(10) self.cli.settimeout(10)
@ -856,6 +873,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
read, write, err = select.select([conn], [], []) read, write, err = select.select([conn], [], [])
if conn in read: if conn in read:
msg = conn.recv(len(MSG)) msg = conn.recv(len(MSG))
conn.close()
self.assertEqual(msg, MSG) self.assertEqual(msg, MSG)
else: else:
self.fail("Error during select call to non-blocking socket.") self.fail("Error during select call to non-blocking socket.")
@ -1105,6 +1123,7 @@ class NetworkConnectionNoServer(unittest.TestCase):
def test_connect(self): def test_connect(self):
port = test_support.find_unused_port() port = test_support.find_unused_port()
cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.addCleanup(cli.close)
with self.assertRaises(socket.error) as cm: with self.assertRaises(socket.error) as cm:
cli.connect((HOST, port)) cli.connect((HOST, port))
self.assertEqual(cm.exception.errno, errno.ECONNREFUSED) self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
@ -1142,16 +1161,19 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
def _justAccept(self): def _justAccept(self):
conn, addr = self.serv.accept() conn, addr = self.serv.accept()
conn.close()
testFamily = _justAccept testFamily = _justAccept
def _testFamily(self): def _testFamily(self):
self.cli = socket.create_connection((HOST, self.port), timeout=30) self.cli = socket.create_connection((HOST, self.port), timeout=30)
self.addCleanup(self.cli.close)
self.assertEqual(self.cli.family, 2) self.assertEqual(self.cli.family, 2)
testSourceAddress = _justAccept testSourceAddress = _justAccept
def _testSourceAddress(self): def _testSourceAddress(self):
self.cli = socket.create_connection((HOST, self.port), timeout=30, self.cli = socket.create_connection((HOST, self.port), timeout=30,
source_address=('', self.source_port)) source_address=('', self.source_port))
self.addCleanup(self.cli.close)
self.assertEqual(self.cli.getsockname()[1], self.source_port) self.assertEqual(self.cli.getsockname()[1], self.source_port)
# The port number being used is sufficient to show that the bind() # The port number being used is sufficient to show that the bind()
# call happened. # call happened.
@ -1163,6 +1185,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
socket.setdefaulttimeout(42) socket.setdefaulttimeout(42)
try: try:
self.cli = socket.create_connection((HOST, self.port)) self.cli = socket.create_connection((HOST, self.port))
self.addCleanup(self.cli.close)
finally: finally:
socket.setdefaulttimeout(None) socket.setdefaulttimeout(None)
self.assertEquals(self.cli.gettimeout(), 42) self.assertEquals(self.cli.gettimeout(), 42)
@ -1174,6 +1197,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
socket.setdefaulttimeout(30) socket.setdefaulttimeout(30)
try: try:
self.cli = socket.create_connection((HOST, self.port), timeout=None) self.cli = socket.create_connection((HOST, self.port), timeout=None)
self.addCleanup(self.cli.close)
finally: finally:
socket.setdefaulttimeout(None) socket.setdefaulttimeout(None)
self.assertEqual(self.cli.gettimeout(), None) self.assertEqual(self.cli.gettimeout(), None)
@ -1186,6 +1210,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
testTimeoutValueNonamed = _justAccept testTimeoutValueNonamed = _justAccept
def _testTimeoutValueNonamed(self): def _testTimeoutValueNonamed(self):
self.cli = socket.create_connection((HOST, self.port), 30) self.cli = socket.create_connection((HOST, self.port), 30)
self.addCleanup(self.cli.close)
self.assertEqual(self.cli.gettimeout(), 30) self.assertEqual(self.cli.gettimeout(), 30)
@unittest.skipUnless(thread, 'Threading required for this test.') @unittest.skipUnless(thread, 'Threading required for this test.')
@ -1205,6 +1230,7 @@ class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
def testInsideTimeout(self): def testInsideTimeout(self):
conn, addr = self.serv.accept() conn, addr = self.serv.accept()
self.addCleanup(conn.close)
time.sleep(3) time.sleep(3)
conn.send("done!") conn.send("done!")
testOutsideTimeout = testInsideTimeout testOutsideTimeout = testInsideTimeout