changeset 10928:dadfc5d47479

8202623: [TEST_BUG] Some launcher tests will hang if underlying process exits too early Reviewed-by: kcr, aghaisas
author arapte
date Fri, 18 May 2018 17:25:08 +0530
parents 9e0e0e65e642
children 071b040b8736
files tests/system/src/test/java/test/javafx/css/imagecacheleaktest/Constants.java tests/system/src/test/java/test/javafx/css/imagecacheleaktest/ImageCacheLeakApp.java tests/system/src/test/java/test/javafx/css/imagecacheleaktest/ImageCacheLeakTest.java tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/Constants.java tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/MeshManagerCacheLeakApp.java tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/MeshManagerCacheLeakTest.java
diffstat 6 files changed, 91 insertions(+), 233 deletions(-) [+]
line wrap: on
line diff
--- a/tests/system/src/test/java/test/javafx/css/imagecacheleaktest/Constants.java	Thu May 17 12:37:18 2018 +0530
+++ b/tests/system/src/test/java/test/javafx/css/imagecacheleaktest/Constants.java	Fri May 18 17:25:08 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 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
@@ -29,13 +29,7 @@
     // Error exit codes. Note that 0 and 1 are reserved for normal exit and
     // failure to launch java, respectively
     public static final int ERROR_NONE = 2;
-    public static final int ERROR_SOCKET = 3;
-    public static final int ERROR_IMAGE_VIEW = 4;
-
-    // Socket handshake value used at initialization (8-bit value)
-    public static final int SOCKET_HANDSHAKE = 126;
-
-    public static final int STATUS_OK = 1;
-    public static final int STATUS_LEAK = 2;
-    public static final int STATUS_INCORRECT_GC = 3;
+    public static final int ERROR_LEAK = 3;
+    public static final int ERROR_INCORRECT_GC = 4;
+    public static final int ERROR_IMAGE_VIEW = 5;
 }
--- a/tests/system/src/test/java/test/javafx/css/imagecacheleaktest/ImageCacheLeakApp.java	Thu May 17 12:37:18 2018 +0530
+++ b/tests/system/src/test/java/test/javafx/css/imagecacheleaktest/ImageCacheLeakApp.java	Fri May 18 17:25:08 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 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
@@ -33,12 +33,6 @@
 import javafx.scene.image.ImageView;
 import java.lang.ref.WeakReference;
 
-import java.io.IOException;
-import java.io.OutputStream;
-import java.net.Socket;
-import java.util.concurrent.atomic.AtomicInteger;
-import javafx.application.Application;
-
 import static test.javafx.css.imagecacheleaktest.Constants.*;
 
 /*
@@ -53,42 +47,21 @@
  */
 public class ImageCacheLeakApp extends Application {
 
-    // Socket for communicating with ImageCacheLeakTest
-    private static Socket socket;
-    private static OutputStream out;
-    private static boolean statusWritten = false;
-
-    private static void initSocket(String[] args) throws Exception {
-        int port = Integer.parseInt(args[0]);
-        socket = new Socket((String)null, port);
-        out = socket.getOutputStream();
-        out.write(SOCKET_HANDSHAKE);
-        out.flush();
-    }
-
-    private synchronized static void writeStatus(int status) {
-        if (!statusWritten) {
-            statusWritten = true;
-            try {
-                out.write(status);
-                out.flush();
-            } catch (IOException ex) {
-                ex.printStackTrace(System.err);
-            }
-        }
-    }
+    WeakReference<Image> img1Ref;
+    WeakReference<Image> img2Ref;
+    int err = ERROR_NONE;
+    ImageView imageView;
+    Group root;
+    Scene scene;
 
     @Override
     public void start(Stage stage) throws Exception {
-        WeakReference<Image> img1Ref;
-        WeakReference<Image> img2Ref;
-        AtomicInteger err = new AtomicInteger(STATUS_OK);
 
         // 1. Create scene & add css stylesheet.
-        ImageView imageView = new ImageView();
-        Group root = new Group();
+        imageView = new ImageView();
+        root = new Group();
         root.getChildren().add(imageView);
-        Scene scene = new Scene(root);
+        scene = new Scene(root);
         stage.setScene(scene);
         scene.getStylesheets().add(ImageCacheLeakApp.class.getResource("css.css").toExternalForm());
         stage.show();
@@ -117,25 +90,18 @@
         } finally {
             // 4.1 Verify that unused image gets GCed.
             if (img1Ref.get() != null) {
-                err.set(STATUS_LEAK);
+                err = ERROR_LEAK;
             }
             // 4.2 Verify that image being used does not get GCed.
             if (img2Ref.get() == null) {
-                err.set(STATUS_INCORRECT_GC);
+                err = ERROR_INCORRECT_GC;
             }
-            writeStatus(err.get());
             stage.hide();
-            System.exit(ERROR_NONE);
+            System.exit(err);
         }
     }
 
     public static void main(String[] args) {
-        try {
-            initSocket(args);
-        } catch (Exception ex) {
-            ex.printStackTrace(System.err);
-            System.exit(ERROR_SOCKET);
-        }
         Application.launch(args);
     }
 }
--- a/tests/system/src/test/java/test/javafx/css/imagecacheleaktest/ImageCacheLeakTest.java	Thu May 17 12:37:18 2018 +0530
+++ b/tests/system/src/test/java/test/javafx/css/imagecacheleaktest/ImageCacheLeakTest.java	Fri May 18 17:25:08 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 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
@@ -25,9 +25,6 @@
 
 package test.javafx.css.imagecacheleaktest;
 
-import java.io.InputStream;
-import java.net.ServerSocket;
-import java.net.Socket;
 import java.util.ArrayList;
 
 import org.junit.Test;
@@ -45,66 +42,40 @@
     private static final String pkgName = className.substring(0, className.lastIndexOf("."));
     private final String testAppName = pkgName + "." + "ImageCacheLeakApp";
 
-    @Test
+    @Test (timeout = 15000)
     public void testImageCacheLeak() throws Exception {
-        // Initilaize the socket
-        final ServerSocket service = new ServerSocket(0);
-        final int port = service.getLocalPort();
+
         String[] jvmArgs = new String[1];
         jvmArgs[0] = new String("-Xmx16m");
 
         // Launch the test app
-        final ArrayList<String> cmd
-                = test.util.Util.createApplicationLaunchCommand(testAppName,
-                        null, null, jvmArgs);
-        // and add our argument
-        cmd.add(String.valueOf(port));
+        final ArrayList<String> cmd = test.util.Util.createApplicationLaunchCommand(
+                testAppName, null, null, jvmArgs);
         ProcessBuilder builder = new ProcessBuilder(cmd);
         builder.redirectError(ProcessBuilder.Redirect.INHERIT);
         builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
         Process process = builder.start();
 
-        // Accept a connection from the test app
-        final Socket socket = service.accept();
-        final InputStream in = socket.getInputStream();
-
-        // Read the "handshake" token
-        int handshake = in.read();
-        Assert.assertEquals("Socket handshake failed,", SOCKET_HANDSHAKE, handshake);
-
-        // Read the status code from the test app.
-        int status = in.read();
-        switch (status) {
-            case STATUS_OK:
-                break;
-            case STATUS_LEAK:
-                fail(testAppName
-                    + ": CSS styled image1 causes memory leak.");
-                break;
-            case STATUS_INCORRECT_GC:
-                fail(testAppName
-                    + ": CSS styled image2 is incorrectly GCed.");
-                break;
-            default:
-                fail(testAppName + ": Unexpected status: " + status);
-        }
-
         // Make sure that the process exited as expected
         int retVal = process.waitFor();
         switch (retVal) {
-            case ERROR_NONE:
-                break;
-
-            case ERROR_SOCKET:
-                fail(testAppName + ": Error connecting to socket");
-                break;
-
             case 0:
                 fail(testAppName + ": Unexpected exit 0");
                 break;
 
             case 1:
-                fail(testAppName + ": Unable to launch java application");
+                fail(testAppName + ": Unexpected exit 1, unable to launch java application");
+                break;
+
+            case ERROR_NONE:
+                break;
+
+            case ERROR_LEAK:
+                fail(testAppName + ": CSS styled image1 causes memory leak.");
+                break;
+
+            case ERROR_INCORRECT_GC:
+                fail(testAppName + ": CSS styled image2 is incorrectly GCed.");
                 break;
 
             case ERROR_IMAGE_VIEW:
--- a/tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/Constants.java	Thu May 17 12:37:18 2018 +0530
+++ b/tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/Constants.java	Fri May 18 17:25:08 2018 +0530
@@ -29,13 +29,6 @@
     // Error exit codes. Note that 0 and 1 are reserved for normal exit and
     // failure to launch java, respectively
     public static final int ERROR_NONE = 2;
-    public static final int ERROR_SOCKET = 3;
-    public static final int ERROR_OOM = 4;
-    public static final int ERROR_LAUNCH = 5;
-
-    // Socket handshake value used at initialization (8-bit value)
-    public static final int SOCKET_HANDSHAKE = 126;
-
-    public static final int STATUS_OK = 1;
-    public static final int STATUS_OOM = 2;
+    public static final int ERROR_OOM = 3;
+    public static final int ERROR_LAUNCH = 4;
 }
--- a/tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/MeshManagerCacheLeakApp.java	Thu May 17 12:37:18 2018 +0530
+++ b/tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/MeshManagerCacheLeakApp.java	Fri May 18 17:25:08 2018 +0530
@@ -37,11 +37,6 @@
 import javafx.scene.shape.Shape3D;
 import javafx.scene.shape.Sphere;
 import java.util.concurrent.TimeUnit;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.net.Socket;
-import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.CountDownLatch;
 
 import static test.javafx.scene.shape.meshmanagercacheleaktest.Constants.*;
@@ -56,13 +51,8 @@
  */
 public class MeshManagerCacheLeakApp {
 
-    // Socket for communicating with MeshManagerCacheLeakTest
-    private static Socket socket;
-    private static OutputStream out;
-    private static boolean statusWritten = false;
-
     static String shapeType;
-    static int numShapes = 0;
+    static int numShapes;
     CountDownLatch latch;
     static CountDownLatch startupLatch = new CountDownLatch(1);
     static Group container;
@@ -84,31 +74,10 @@
         }
     }
 
-    private static void initSocket(String[] args) throws Exception {
-        int port = Integer.parseInt(args[0]);
-        socket = new Socket((String)null, port);
-        out = socket.getOutputStream();
-        out.write(SOCKET_HANDSHAKE);
-        out.flush();
-    }
-
-    private synchronized static void writeStatus(int status) {
-        if (!statusWritten) {
-            statusWritten = true;
-            try {
-                out.write(status);
-                out.flush();
-            } catch (IOException ex) {
-                ex.printStackTrace(System.err);
-            }
-        }
-    }
-
     public void testOOM() {
         System.gc();
         System.runFinalization();
 
-        AtomicInteger err = new AtomicInteger(STATUS_OOM);
         try {
             // 2. Reserve maximum of available memory.
             byte[] mem = null;
@@ -143,11 +112,9 @@
                         container.getChildren().add(shp);
                         latch.countDown();
                     } catch (OutOfMemoryError e) {
-                        writeStatus(err.get());
-                        System.exit(0);
+                        System.exit(ERROR_OOM);
                     } catch (Exception e) {
-                        writeStatus(err.get());
-                        System.exit(0);
+                        System.exit(ERROR_OOM);
                     }
                 });
                 waitForLatch(latch, 5, -1);
@@ -165,16 +132,12 @@
                 waitForLatch(latch, 5, -1);
             }
         } catch (OutOfMemoryError e) {
-            writeStatus(err.get());
-            System.exit(0);
+            System.exit(ERROR_OOM);
         } catch (Exception e) {
-            writeStatus(err.get());
-            System.exit(0);
+            System.exit(ERROR_OOM);
         }
 
         // 4. Verify that, no OOM occurs.
-        err.set(STATUS_OK);
-        writeStatus(err.get());
         System.exit(ERROR_NONE);
     }
 
@@ -189,14 +152,8 @@
     }
 
     public static void main(String[] args) {
-        try {
-            initSocket(args);
-        } catch (Exception ex) {
-            ex.printStackTrace(System.err);
-            System.exit(ERROR_SOCKET);
-        }
-        shapeType = args[1];
-        numShapes = Integer.parseInt(args[2]);
+        shapeType = args[0];
+        numShapes = Integer.parseInt(args[1]);
 
         MeshManagerCacheLeakApp test = new MeshManagerCacheLeakApp();
         new Thread(() -> Application.launch(TestApp.class, (String[])null)).start();
--- a/tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/MeshManagerCacheLeakTest.java	Thu May 17 12:37:18 2018 +0530
+++ b/tests/system/src/test/java/test/javafx/scene/shape/meshmanagercacheleaktest/MeshManagerCacheLeakTest.java	Fri May 18 17:25:08 2018 +0530
@@ -25,9 +25,6 @@
 
 package test.javafx.scene.shape.meshmanagercacheleaktest;
 
-import java.io.InputStream;
-import java.net.ServerSocket;
-import java.net.Socket;
 import java.util.ArrayList;
 
 import javafx.application.ConditionalFeature;
@@ -46,8 +43,8 @@
  */
 public class MeshManagerCacheLeakTest {
 
-    private static final String className = MeshManagerCacheLeakTest.class.getName();
-    private static final String pkgName = className.substring(0, className.lastIndexOf("."));
+    private final String className = MeshManagerCacheLeakTest.class.getName();
+    private final String pkgName = className.substring(0, className.lastIndexOf("."));
     private final String testAppName = pkgName + "." + "MeshManagerCacheLeakApp";
 
     @Before
@@ -55,79 +52,59 @@
         assumeTrue(Platform.isSupported(ConditionalFeature.SCENE3D));
     }
 
-    @Test
-    public void testMeshManagerCacheLeak() throws Exception {
-        // Initilaize the socket
-        final ServerSocket service = new ServerSocket(0);
-        final int port = service.getLocalPort();
+    @Test (timeout = 15000)
+    public void testSphereCacheLeakTest() throws Exception {
+        testMeshManagerCacheLeak("Sphere", "10");
+    }
+
+    @Test (timeout = 15000)
+    public void testCylinderCacheLeakTest() throws Exception {
+        testMeshManagerCacheLeak("Cylinder", "25");
+    }
+
+    @Test (timeout = 20000)
+    public void testBoxCacheLeakTest() throws Exception {
+        testMeshManagerCacheLeak("Box", "350");
+    }
+
+    private void testMeshManagerCacheLeak(String shape, String count) throws Exception {
         String[] jvmArgs = {"-Xmx16m"};
-        String[] testArgs = { "Sphere", "Cylinder", "Box"};
-        String[] numShapes = { "10", "25", "350"};
+        // Launch the test app
+        final ArrayList<String> cmd = test.util.Util.createApplicationLaunchCommand(
+            testAppName, null, null, jvmArgs);
+        // and add our arguments
+        cmd.add(String.valueOf(shape));
+        cmd.add(String.valueOf(count));
+        ProcessBuilder builder = new ProcessBuilder(cmd);
+        builder.redirectError(ProcessBuilder.Redirect.INHERIT);
+        builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
+        Process process = builder.start();
 
-        for (int i = 0; i < testArgs.length; ++i) {
-            // Launch the test app
-            final ArrayList<String> cmd
-                    = test.util.Util.createApplicationLaunchCommand(testAppName,
-                            null, null, jvmArgs);
-            // and add our arguments
-            cmd.add(String.valueOf(port));
-            cmd.add(String.valueOf(testArgs[i]));
-            cmd.add(String.valueOf(numShapes[i]));
-            ProcessBuilder builder = new ProcessBuilder(cmd);
-            builder.redirectError(ProcessBuilder.Redirect.INHERIT);
-            builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
-            Process process = builder.start();
+        // Make sure that the process exited as expected
+        int retVal = process.waitFor();
+        switch (retVal) {
+            case 0:
+                fail(testAppName + ": Unexpected exit 0 with cache test of : " + shape);
+                break;
 
-            // Accept a connection from the test app
-            final Socket socket = service.accept();
-            final InputStream in = socket.getInputStream();
+            case 1:
+                fail(testAppName + ": Unable to launch java application with cache test of : " + shape);
+                break;
 
-            // Read the "handshake" token
-            int handshake = in.read();
-            Assert.assertEquals("Socket handshake failed,", SOCKET_HANDSHAKE, handshake);
+            case ERROR_NONE:
+                break;
 
-            // Read the status code from the test app.
-            int status = in.read();
-            switch (status) {
-                case STATUS_OK:
-                    break;
-                case STATUS_OOM:
-                    fail(testAppName
-                        + ": OOM occured with cache of : " + testArgs[i]);
-                    break;
-                default:
-                    fail(testAppName + ": Unexpected status: " + status + " with cache of : " + testArgs[i]);
-            }
+            case ERROR_OOM:
+                fail(testAppName + ": OOM occured with cache test of : " + shape);
+                break;
 
-            // Make sure that the process exited as expected
-            int retVal = process.waitFor();
-            switch (retVal) {
-                case ERROR_NONE:
-                    break;
+            case ERROR_LAUNCH:
+                fail(testAppName + ": Window was not shown for more than 10 secs, with cache test of : " + shape);
+                break;
 
-                case 0:
-                    fail(testAppName + ": Unexpected exit 0 with cache test of : " + testArgs[i]);
-                    break;
-
-                case 1:
-                    fail(testAppName + ": Unable to launch java application with cache test of : " + testArgs[i]);
-                    break;
-
-                case ERROR_SOCKET:
-                    fail(testAppName + ": Error connecting to socket with cache test of : " + testArgs[i]);
-                    break;
-
-                case ERROR_OOM:
-                    fail(testAppName + ": OOM occured with cache test of : " + testArgs[i]);
-                    break;
-
-                case ERROR_LAUNCH:
-                    fail(testAppName + ": Window was not shown for more than 10 secs, with cache test of : " + testArgs[i]);
-                    break;
-
-                default:
-                    fail(testAppName + ": Unexpected error exit: " + retVal + " with cache test of : " + testArgs[i]);
-            }
+            default:
+                fail(testAppName + ": Unexpected error exit: " + retVal + " with cache test of : " + shape);
+                break;
         }
     }
 }