changeset 9418:a011ff1e85a3

8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty Summary: avoid opening files excessively Reviewed-by: coleenp, mseledtsov
author iklam
date Thu, 02 Jul 2015 20:30:33 -0700
parents 964fb44d9669
children 6c8e38319913
files test/runtime/Metaspace/FragmentMetaspaceSimple.java
diffstat 1 files changed, 45 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/test/runtime/Metaspace/FragmentMetaspaceSimple.java	Thu Jul 02 14:20:36 2015 -0700
+++ b/test/runtime/Metaspace/FragmentMetaspaceSimple.java	Thu Jul 02 20:30:33 2015 -0700
@@ -23,12 +23,15 @@
 
 /**
  * @test
- * @library /runtime/testlibrary
  * @library classes
- * @build test.Empty ClassUnloadCommon
+ * @build test.Empty
  * @run main/othervm/timeout=200 FragmentMetaspaceSimple
  */
 
+import java.io.DataInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.util.ArrayList;
 
 /**
@@ -47,8 +50,14 @@
     private static void runSimple(long time) {
         long startTime = System.currentTimeMillis();
         ArrayList<ClassLoader> cls = new ArrayList<>();
-        for (int i = 0; System.currentTimeMillis() < startTime + time; ++i) {
-            ClassLoader ldr = ClassUnloadCommon.newClassLoader();
+        char sep = File.separatorChar;
+        String fileName = "classes" + sep + "test" + sep + "Empty.class";
+        File file = new File(System.getProperty("test.classes",".") + sep + fileName);
+        byte buff[] = read(file);
+
+        int i = 0;
+        for (i = 0; System.currentTimeMillis() < startTime + time; ++i) {
+            ClassLoader ldr = new MyClassLoader(buff);
             if (i % 1000 == 0) {
                 cls.clear();
             }
@@ -59,11 +68,43 @@
             Class<?> c = null;
             try {
                 c = ldr.loadClass("test.Empty");
+                c.getClass().getClassLoader(); // make sure we have a valid class.
             } catch (ClassNotFoundException ex) {
+                System.out.println("i=" + i + ", len" + buff.length);
                 throw new RuntimeException(ex);
             }
             c = null;
         }
         cls = null;
+        System.out.println("Finished " + i + " iterations in " +
+                           (System.currentTimeMillis() - startTime) + " ms");
+    }
+
+    private static byte[] read(File file) {
+        byte buff[] = new byte[(int)(file.length())];
+        try {
+            DataInputStream din = new DataInputStream(new FileInputStream(file));
+            din.readFully(buff);
+            din.close();
+        } catch (IOException ex) {
+            throw new RuntimeException(ex);
+        }
+        return buff;
+    }
+
+    static class MyClassLoader extends ClassLoader {
+        byte buff[];
+        MyClassLoader(byte buff[]) {
+            this.buff = buff;
+        }
+
+        public Class<?> loadClass() throws ClassNotFoundException {
+            String name = "test.Empty";
+            try {
+                return defineClass(name, buff, 0, buff.length);
+            } catch (Throwable e) {
+                throw new ClassNotFoundException(name, e);
+            }
+        }
     }
 }