8187522: test/sun/net/ftp/FtpURLConnectionLeak.java timed out
authorxyin
Mon, 15 Oct 2018 09:34:18 +0800
changeset 52113 f48838bdcc31
parent 52112 76c87b213fa0
child 52114 3b8994cb4481
8187522: test/sun/net/ftp/FtpURLConnectionLeak.java timed out Reviewed-by: chegar, vtewari
test/jdk/sun/net/ftp/FtpURLConnectionLeak.java
test/jdk/sun/net/www/ftptest/FtpCommandHandler.java
test/jdk/sun/net/www/ftptest/FtpServer.java
--- a/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java	Fri Oct 12 17:35:26 2018 -0400
+++ b/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java	Mon Oct 15 09:34:18 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,6 @@
  * @run main FtpURLConnectionLeak
  */
 import java.io.FileNotFoundException;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
@@ -44,27 +43,26 @@
         int port = server.getLocalPort();
         server.start();
         URL url = new URL("ftp://localhost:" + port + "/filedoesNotExist.txt");
-        for (int i = 0; i < 3; i++) {
-            try {
-                InputStream stream = url.openStream();
-            } catch (FileNotFoundException expectedFirstTimeAround) {
-                // should always reach this point since the path does not exist
-            } catch (IOException expected) {
-                System.out.println("caught expected " + expected);
-                int times = 1;
-                do {
-                    // give some time to close the connection...
-                    System.out.println("sleeping... " + times);
-                    Thread.sleep(times * 1000);
-                } while (server.activeClientsCount() > 0 && times++ < 5);
+        try (server) {
+            for (int i = 0; i < 3; i++) {
+                try {
+                    InputStream stream = url.openStream();
+                } catch (FileNotFoundException expected) {
+                    // should always reach this point since the path does not exist
+                    System.out.println("caught expected " + expected);
+                    int times = 1;
+                    do {
+                        // give some time to close the connection...
+                        System.out.println("sleeping... " + times);
+                        Thread.sleep(times * 1000);
+                    } while (server.activeClientsCount() > 0 && times++ < 5);
 
-                if (server.activeClientsCount() > 0) {
-                    server.killClients();
-                    throw new RuntimeException("URLConnection didn't close the" +
-                            " FTP connection on FileNotFoundException");
+                    if (server.activeClientsCount() > 0) {
+                        server.killClients();
+                        throw new RuntimeException("URLConnection didn't close the" +
+                                " FTP connection on FileNotFoundException");
+                    }
                 }
-            } finally {
-                server.terminate();
             }
         }
     }
--- a/test/jdk/sun/net/www/ftptest/FtpCommandHandler.java	Fri Oct 12 17:35:26 2018 -0400
+++ b/test/jdk/sun/net/www/ftptest/FtpCommandHandler.java	Mon Oct 15 09:34:18 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -442,8 +442,14 @@
             // cmd.setSoTimeout(2000);
             in = new BufferedReader(new InputStreamReader(cmd.getInputStream()));
             out = new PrintStream(cmd.getOutputStream(), true, "ISO8859_1");
+            // Below corrupted message style was intentional to test 8151586, please
+            // make sure each message line not broken ftp communication (such as for
+            // message line lenght >=4, the 4th char required '-' to allow
+            // implementation thinks that it has seen multi-line reply '###-'
+            // sequence), otherwise it will affect normal ftp tests which depends
+            // on this.
             out.println("---------------------------------\n220 Java FTP test server"
-                    + " (j2se 6.0) ready.\n \n                Please send commands\n"
+                    + " (j2se 6.0) ready.\n \n   -            Please send commands\n"
                     + "-----------------------------\n\n\n");
             out.flush();
             if (auth.authType() == 0) // No auth needed
--- a/test/jdk/sun/net/www/ftptest/FtpServer.java	Fri Oct 12 17:35:26 2018 -0400
+++ b/test/jdk/sun/net/www/ftptest/FtpServer.java	Mon Oct 15 09:34:18 2018 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -47,7 +47,7 @@
  *
  */
 
-public class FtpServer extends Thread {
+public class FtpServer extends Thread implements AutoCloseable {
     private ServerSocket listener = null;
     private FtpFileSystemHandler fsh = null;
     private FtpAuthHandler auth = null;
@@ -134,4 +134,13 @@
 
         }
     }
+
+    @Override
+    public void close() throws Exception {
+        terminate();
+        listener.close();
+        if (activeClientsCount() > 0) {
+            killClients();
+        }
+    }
 }