@@ -896,39 +896,40 @@ class TestTimeouts(TestCase):
896896 def setUp (self ):
897897 self .evt = threading .Event ()
898898 self .sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
899- self .sock .settimeout (10 )
899+ self .sock .settimeout (20 )
900900 self .port = support .bind_port (self .sock )
901- threading .Thread (target = self .server , args = (self .evt ,self .sock )).start ()
901+ self .server_thread = threading .Thread (target = self .server )
902+ self .server_thread .start ()
902903 # Wait for the server to be ready.
903904 self .evt .wait ()
904905 self .evt .clear ()
906+ self .old_port = ftplib .FTP .port
905907 ftplib .FTP .port = self .port
906908
907909 def tearDown (self ):
908- self . evt . wait ()
909- self .sock . close ()
910+ ftplib . FTP . port = self . old_port
911+ self .server_thread . join ()
910912
911- def server (self , evt , serv ):
913+ def server (self ):
912914 # This method sets the evt 3 times:
913915 # 1) when the connection is ready to be accepted.
914916 # 2) when it is safe for the caller to close the connection
915917 # 3) when we have closed the socket
916- serv .listen (5 )
918+ self . sock .listen (5 )
917919 # (1) Signal the caller that we are ready to accept the connection.
918- evt .set ()
920+ self . evt .set ()
919921 try :
920- conn , addr = serv .accept ()
922+ conn , addr = self . sock .accept ()
921923 except socket .timeout :
922924 pass
923925 else :
924- conn .send (b"1 Hola mundo\n " )
926+ conn .sendall (b"1 Hola mundo\n " )
927+ conn .shutdown (socket .SHUT_WR )
925928 # (2) Signal the caller that it is safe to close the socket.
926- evt .set ()
929+ self . evt .set ()
927930 conn .close ()
928931 finally :
929- serv .close ()
930- # (3) Signal the caller that we are done.
931- evt .set ()
932+ self .sock .close ()
932933
933934 def testTimeoutDefault (self ):
934935 # default -- use global socket timeout
0 commit comments