From 7addcb118b984362dd2fbd10fa8c9930a2bb70dd Mon Sep 17 00:00:00 2001 From: Daniel Fuchs Date: Mon, 1 Jul 2013 11:13:56 +0200 Subject: [PATCH] 8014045: test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java failing intermittently This test was failing because it didn't take into account the fact that Loggers could be garbage collected. Reviewed-by: mchung --- .../LoggingMXBeanTest.java | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java b/jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java index 26b3b661531..b88710c81a6 100644 --- a/jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java +++ b/jdk/test/java/lang/management/PlatformLoggingMXBean/LoggingMXBeanTest.java @@ -36,12 +36,14 @@ import javax.management.MBeanServer; 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 @@ public class LoggingMXBeanTest PlatformLoggingMXBean mxbean2) { // verify logger names List loggers1 = mxbean1.getLoggerNames(); + System.out.println("Loggers: " + loggers1); + + // Retrieve the named loggers to prevent them from being + // spontaneously gc'ed. + Map loggersMap = new HashMap<>(); + for (String n : loggers1) { + loggersMap.put(n, Logger.getLogger(n)); + } + List 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 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)))