summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_socket.py
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2007-08-03 20:40:09 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2007-08-03 20:40:09 (GMT)
commit5accbdb8123a814d9a602e7bb640ad7f0e953f54 (patch)
tree469bbd951bbda9c7c542459a6dc76e2aea1bfd74 /Lib/test/test_socket.py
parentd2ef864f819ba9b3ef834e190491829bbb00b554 (diff)
downloadcpython-5accbdb8123a814d9a602e7bb640ad7f0e953f54.zip
cpython-5accbdb8123a814d9a602e7bb640ad7f0e953f54.tar.gz
cpython-5accbdb8123a814d9a602e7bb640ad7f0e953f54.tar.bz2
Make sure socket.close() doesn't interfere with socket.makefile().
If a makefile()-generated object is open and its parent socket is closed, the parent socket should remain open until the child is closed, too. The code to support this is moderately complex and requires one extra slots in the socket object. This change fixes httplib so that several urllib2net test cases pass again. Add SocketCloser class to socket.py, which encapsulates the refcounting logic for sockets after makefile() has been called. Move SocketIO class from io module to socket module. It's only use is to implement the raw I/O methods on top of a socket to support makefile(). Add unittests to test_socket to cover various patterns of close and makefile.
Diffstat (limited to 'Lib/test/test_socket.py')
-rw-r--r--Lib/test/test_socket.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index f2b74ee..a8b65c4 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -163,6 +163,11 @@ class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest):
self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
class SocketConnectedTest(ThreadedTCPSocketTest):
+ """Socket tests for client-server connection.
+
+ self.cli_conn is a client socket connected to the server. The
+ setUp() method guarantees that it is connected to the server.
+ """
def __init__(self, methodName='runTest'):
ThreadedTCPSocketTest.__init__(self, methodName=methodName)
@@ -618,6 +623,10 @@ class TCPCloserTest(ThreadedTCPSocketTest):
self.assertEqual(read, [sd])
self.assertEqual(sd.recv(1), b'')
+ # Calling close() many times should be safe.
+ conn.close()
+ conn.close()
+
def _testClose(self):
self.cli.connect((HOST, PORT))
time.sleep(1.0)
@@ -710,6 +719,16 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
self.cli.send(MSG)
class FileObjectClassTestCase(SocketConnectedTest):
+ """Unit tests for the object returned by socket.makefile()
+
+ self.serv_file is the io object returned by makefile() on
+ the client connection. You can read from this file to
+ get output from the server.
+
+ self.cli_file is the io object returned by makefile() on the
+ server connection. You can write to this file to send output
+ to the client.
+ """
bufsize = -1 # Use default buffer size
@@ -779,6 +798,26 @@ class FileObjectClassTestCase(SocketConnectedTest):
self.cli_file.write(MSG)
self.cli_file.flush()
+ def testCloseAfterMakefile(self):
+ # The file returned by makefile should keep the socket open.
+ self.cli_conn.close()
+ # read until EOF
+ msg = self.serv_file.read()
+ self.assertEqual(msg, MSG)
+
+ def _testCloseAfterMakefile(self):
+ self.cli_file.write(MSG)
+ self.cli_file.flush()
+
+ def testMakefileAfterMakefileClose(self):
+ self.serv_file.close()
+ msg = self.cli_conn.recv(len(MSG))
+ self.assertEqual(msg, MSG)
+
+ def _testMakefileAfterMakefileClose(self):
+ self.cli_file.write(MSG)
+ self.cli_file.flush()
+
def testClosedAttr(self):
self.assert_(not self.serv_file.closed)