7067691: java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java failing intermittently
authormchung
Thu, 17 Nov 2011 15:47:34 -0800
changeset 11024 a35b10c0eec3
parent 11023 b1bf8d3682f8
child 11025 d7f6c062c454
7067691: java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java failing intermittently Reviewed-by: alanb, mchung Contributed-by: gary.adams@oracle.com
jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java
jdk/test/java/lang/management/PlatformLoggingMXBean/PlatformLoggingMXBeanTest.java
--- a/jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java	Wed Nov 16 12:23:53 2011 -0800
+++ b/jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java	Thu Nov 17 15:47:34 2011 -0800
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 7024172
+ * @bug 7024172 7067691
  * @summary Test if proxy for PlatformLoggingMXBean is equivalent
  *          to proxy for LoggingMXBean
  *
@@ -43,6 +43,13 @@
     static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
     static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
 
+    // These instance variables prevent premature logger garbage collection
+    // See getLogger() weak reference warnings.
+    Logger logger1;
+    Logger logger2;
+
+    static LoggingMXBeanTest test;
+
     public static void main(String[] argv) throws Exception {
         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
         LoggingMXBean proxy =
@@ -51,7 +58,7 @@
                 LoggingMXBean.class);
 
         // test LoggingMXBean proxy
-        LoggingMXBeanTest p = new LoggingMXBeanTest(proxy);
+        test = new LoggingMXBeanTest(proxy);
 
         // check if the attributes implemented by PlatformLoggingMXBean
         // and LoggingMXBean return the same value
@@ -64,9 +71,9 @@
     // same verification as in java/util/logging/LoggingMXBeanTest2
     public LoggingMXBeanTest(LoggingMXBean mbean) throws Exception {
 
-        Logger logger1 = Logger.getLogger( LOGGER_NAME_1 );
+        logger1 = Logger.getLogger( LOGGER_NAME_1 );
         logger1.setLevel(Level.FINE);
-        Logger logger2 = Logger.getLogger( LOGGER_NAME_2 );
+        logger2 = Logger.getLogger( LOGGER_NAME_2 );
         logger2.setLevel(null);
 
         /*
@@ -207,6 +214,7 @@
         // verify logger names
         List<String> loggers1 = mxbean1.getLoggerNames();
         List<String> loggers2 = mxbean2.getLoggerNames();
+
         if (loggers1.size() != loggers2.size())
             throw new RuntimeException("LoggerNames: unmatched number of entries");
         List<String> loggers3 = new ArrayList<>(loggers1);
@@ -219,7 +227,10 @@
             if (!mxbean1.getLoggerLevel(logger)
                     .equals(mxbean2.getLoggerLevel(logger)))
                 throw new RuntimeException(
-                    "LoggerLevel: unmatched level for " + logger);
+                    "LoggerLevel: unmatched level for " + logger
+                    + ", " + mxbean1.getLoggerLevel(logger)
+                    + ", " + mxbean2.getLoggerLevel(logger));
+
             if (!mxbean1.getParentLoggerName(logger)
                     .equals(mxbean2.getParentLoggerName(logger)))
                 throw new RuntimeException(
--- a/jdk/test/java/lang/management/PlatformLoggingMXBean/PlatformLoggingMXBeanTest.java	Wed Nov 16 12:23:53 2011 -0800
+++ b/jdk/test/java/lang/management/PlatformLoggingMXBean/PlatformLoggingMXBeanTest.java	Thu Nov 17 15:47:34 2011 -0800
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug     6876135 7024172
+ * @bug     6876135 7024172 7067691
  *
  * @summary Test PlatformLoggingMXBean
  *          This test performs similar testing as
@@ -41,11 +41,15 @@
 
 public class PlatformLoggingMXBeanTest
 {
-
     ObjectName objectName = null;
     static String LOGGER_NAME_1 = "com.sun.management.Logger1";
     static String LOGGER_NAME_2 = "com.sun.management.Logger2";
 
+    // Use Logger instance variables to prevent premature garbage collection
+    // of weak references.
+    Logger logger1;
+    Logger logger2;
+
     public PlatformLoggingMXBeanTest() throws Exception {
     }
 
@@ -135,8 +139,8 @@
         System.out.println( "*********** Phase 3 ***********" );
         System.out.println( "*******************************" );
         System.out.println( " Create and test new Loggers" );
-        Logger logger1 = Logger.getLogger( LOGGER_NAME_1 );
-        Logger logger2 = Logger.getLogger( LOGGER_NAME_2 );
+        logger1 = Logger.getLogger( LOGGER_NAME_1 );
+        logger2 = Logger.getLogger( LOGGER_NAME_2 );
 
         // check that Level object are returned properly
         try {
@@ -187,6 +191,7 @@
         System.out.println( " Set and Check the Logger Level" );
         log1 = false;
         log2 = false;
+
         try {
             // Set the level of logger1 to ALL
             params = new Object[2];