8014045: test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java failing intermittently
authordfuchs
Mon, 01 Jul 2013 11:13:56 +0200
changeset 18586 36bbc241ad6e
parent 18585 820a67f42c4c
child 18587 d70aed7424f6
child 18772 ab43f18c524e
8014045: test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java failing intermittently Summary: this test was failing because it didn't take into account the fact that Loggers could be garbage collected. Reviewed-by: mchung
jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java
--- a/jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java	Sun Jun 30 17:15:47 2013 -0700
+++ b/jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java	Mon Jul 01 11:13:56 2013 +0200
@@ -36,12 +36,14 @@
 import java.util.logging.*;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
 
 public class LoggingMXBeanTest
 {
-    static String LOGGER_NAME_1 = "com.sun.management.Logger";
-    static String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
-    static String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
+    static final String LOGGER_NAME_1 = "com.sun.management.Logger";
+    static final String LOGGER_NAME_2 = "com.sun.management.Logger.Logger2";
+    static final String UNKNOWN_LOGGER_NAME = "com.sun.management.Unknown";
 
     // These instance variables prevent premature logger garbage collection
     // See getLogger() weak reference warnings.
@@ -213,23 +215,35 @@
                                         PlatformLoggingMXBean mxbean2) {
         // verify logger names
         List<String> loggers1 = mxbean1.getLoggerNames();
+        System.out.println("Loggers: " + loggers1);
+
+        // Retrieve the named loggers to prevent them from being
+        // spontaneously gc'ed.
+        Map<String, Logger> loggersMap = new HashMap<>();
+        for (String n : loggers1) {
+            loggersMap.put(n, Logger.getLogger(n));
+        }
+
         List<String> loggers2 = mxbean2.getLoggerNames();
 
+        // loggers1 and loggers2 should be identical - no new logger should
+        // have been created in between (at least no new logger name)
+        //
         if (loggers1.size() != loggers2.size())
             throw new RuntimeException("LoggerNames: unmatched number of entries");
-        List<String> loggers3 = new ArrayList<>(loggers1);
-        loggers3.removeAll(loggers2);
-        if (loggers3.size() != 0)
+        if (!loggers2.containsAll(loggersMap.keySet()))
             throw new RuntimeException("LoggerNames: unmatched loggers");
 
+
         // verify logger's level  and parent
         for (String logger : loggers1) {
-            if (!mxbean1.getLoggerLevel(logger)
-                    .equals(mxbean2.getLoggerLevel(logger)))
+            String level1 = mxbean1.getLoggerLevel(logger);
+            String level2 = mxbean2.getLoggerLevel(logger);
+            if (!java.util.Objects.equals(level1, level2)) {
                 throw new RuntimeException(
-                    "LoggerLevel: unmatched level for " + logger
-                    + ", " + mxbean1.getLoggerLevel(logger)
-                    + ", " + mxbean2.getLoggerLevel(logger));
+                        "LoggerLevel: unmatched level for " + logger
+                        + ", " + level1 + ", " + level2);
+            }
 
             if (!mxbean1.getParentLoggerName(logger)
                     .equals(mxbean2.getParentLoggerName(logger)))