Index: Lib/test/test_socket.py =================================================================== --- Lib/test/test_socket.py.orig +++ Lib/test/test_socket.py @@ -31,27 +31,6 @@ except ImportError: # Size in bytes of the int type SIZEOF_INT = array.array("i").itemsize -class SocketTCPTest(unittest.TestCase): - - def setUp(self): - self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.port = support.bind_port(self.serv) - self.serv.listen(1) - - def tearDown(self): - self.serv.close() - self.serv = None - -class SocketUDPTest(unittest.TestCase): - - def setUp(self): - self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - self.port = support.bind_port(self.serv) - - def tearDown(self): - self.serv.close() - self.serv = None - class ThreadSafeCleanupTestCase(unittest.TestCase): """Subclass of unittest.TestCase with thread-safe cleanup methods. @@ -170,62 +149,6 @@ class ThreadableTest: self.done.set() thread.exit() -class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest): - - def __init__(self, methodName='runTest'): - SocketTCPTest.__init__(self, methodName=methodName) - ThreadableTest.__init__(self) - - def clientSetUp(self): - self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - def clientTearDown(self): - self.cli.close() - self.cli = None - ThreadableTest.clientTearDown(self) - -class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest): - - def __init__(self, methodName='runTest'): - SocketUDPTest.__init__(self, methodName=methodName) - ThreadableTest.__init__(self) - - def clientSetUp(self): - 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) - - def setUp(self): - ThreadedTCPSocketTest.setUp(self) - # Indicate explicitly we're ready for the client thread to - # proceed and then perform the blocking call to accept - self.serverExplicitReady() - conn, addr = self.serv.accept() - self.cli_conn = conn - - def tearDown(self): - self.cli_conn.close() - self.cli_conn = None - ThreadedTCPSocketTest.tearDown(self) - - def clientSetUp(self): - ThreadedTCPSocketTest.clientSetUp(self) - self.cli.connect((HOST, self.port)) - self.serv_conn = self.cli - - def clientTearDown(self): - self.serv_conn.close() - self.serv_conn = None - ThreadedTCPSocketTest.clientTearDown(self) - class SocketPairTest(unittest.TestCase, ThreadableTest): def __init__(self, methodName='runTest'): @@ -828,10 +751,7 @@ class GeneralModuleTests(unittest.TestCa @unittest.skipUnless(thread, 'Threading required for this test.') -class BasicTCPTest(SocketConnectedTest): - - def __init__(self, methodName='runTest'): - SocketConnectedTest.__init__(self, methodName=methodName) +class BasicTCPTest(ConnectedStreamTestMixin, TCPTestBase): def testRecv(self): # Testing large receive over TCP @@ -918,10 +838,7 @@ class BasicTCPTest(SocketConnectedTest): self.serv_conn.shutdown(2) @unittest.skipUnless(thread, 'Threading required for this test.') -class BasicUDPTest(ThreadedUDPSocketTest): - - def __init__(self, methodName='runTest'): - ThreadedUDPSocketTest.__init__(self, methodName=methodName) +class BasicUDPTest(ThreadedSocketTestMixin, UDPTestBase): def testSendtoAndRecv(self): # Testing sendto() and Recv() over UDP @@ -2645,7 +2562,8 @@ class RecvmsgIntoSCMRightsStreamTest(Rec @unittest.skipUnless(thread, 'Threading required for this test.') -class TCPCloserTest(ThreadedTCPSocketTest): +class TCPCloserTest(ThreadedSocketTestMixin, SocketListeningTestMixin, + TCPTestBase): def testClose(self): conn, addr = self.serv.accept() @@ -2685,10 +2603,8 @@ class BasicSocketPairTest(SocketPairTest self.assertEqual(msg, MSG) @unittest.skipUnless(thread, 'Threading required for this test.') -class NonBlockingTCPTests(ThreadedTCPSocketTest): - - def __init__(self, methodName='runTest'): - ThreadedTCPSocketTest.__init__(self, methodName=methodName) +class NonBlockingTCPTests(ThreadedSocketTestMixin, SocketListeningTestMixin, + TCPTestBase): def testSetBlocking(self): # Testing whether set blocking works @@ -2754,7 +2670,7 @@ class NonBlockingTCPTests(ThreadedTCPSoc self.cli.send(MSG) @unittest.skipUnless(thread, 'Threading required for this test.') -class FileObjectClassTestCase(SocketConnectedTest): +class FileObjectClassTestCase(ConnectedStreamTestMixin, TCPTestBase): """Unit tests for the object returned by socket.makefile() self.serv_file is the io object returned by makefile() on @@ -2768,28 +2684,25 @@ class FileObjectClassTestCase(SocketConn bufsize = -1 # Use default buffer size - def __init__(self, methodName='runTest'): - SocketConnectedTest.__init__(self, methodName=methodName) - def setUp(self): - SocketConnectedTest.setUp(self) + super().setUp() self.serv_file = self.cli_conn.makefile('rb', self.bufsize) def tearDown(self): self.serv_file.close() self.assertTrue(self.serv_file.closed) self.serv_file = None - SocketConnectedTest.tearDown(self) + super().tearDown() def clientSetUp(self): - SocketConnectedTest.clientSetUp(self) + super().clientSetUp() self.cli_file = self.serv_conn.makefile('wb') def clientTearDown(self): self.cli_file.close() self.assertTrue(self.cli_file.closed) self.cli_file = None - SocketConnectedTest.clientTearDown(self) + super().clientTearDown() def testSmallRead(self): # Performing small file read test @@ -3070,10 +2983,11 @@ class NetworkConnectionNoServer(unittest ) @unittest.skipUnless(thread, 'Threading required for this test.') -class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): +class NetworkConnectionAttributesTest(SocketListeningTestMixin, TCPTestBase, + ThreadableTest): def __init__(self, methodName='runTest'): - SocketTCPTest.__init__(self, methodName=methodName) + super().__init__(methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): @@ -3133,10 +3047,11 @@ class NetworkConnectionAttributesTest(So self.assertEqual(self.cli.gettimeout(), 30) @unittest.skipUnless(thread, 'Threading required for this test.') -class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): +class NetworkConnectionBehaviourTest(SocketListeningTestMixin, TCPTestBase, + ThreadableTest): def __init__(self, methodName='runTest'): - SocketTCPTest.__init__(self, methodName=methodName) + super().__init__(methodName=methodName) ThreadableTest.__init__(self) def clientSetUp(self): @@ -3163,7 +3078,7 @@ class NetworkConnectionBehaviourTest(Soc self.assertRaises(socket.timeout, lambda: sock.recv(5)) -class TCPTimeoutTest(SocketTCPTest): +class TCPTimeoutTest(SocketListeningTestMixin, TCPTestBase): def testTCPTimeout(self): def raise_timeout(*args, **kwargs): @@ -3220,7 +3135,7 @@ class TCPTimeoutTest(SocketTCPTest): # no alarm can be pending. Safe to restore old handler. signal.signal(signal.SIGALRM, old_alarm) -class UDPTimeoutTest(SocketTCPTest): +class UDPTimeoutTest(UDPTestBase): def testUDPTimeout(self): def raise_timeout(*args, **kwargs): @@ -3279,12 +3194,10 @@ class TestLinuxAbstractNamespace(unittes @unittest.skipUnless(thread, 'Threading required for this test.') -class BufferIOTest(SocketConnectedTest): +class BufferIOTest(ConnectedStreamTestMixin, TCPTestBase): """ Test the buffer versions of socket.recv() and socket.send(). """ - def __init__(self, methodName='runTest'): - SocketConnectedTest.__init__(self, methodName=methodName) def testRecvIntoArray(self): buf = bytearray(1024)