From 44eeb5f6b4a59b64788f7cfad9410ea6ae71195b Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 31 Mar 2015 15:08:52 +0900 Subject: [PATCH] Fix intermittent test failure in LoggingHandlerTest Motivation: LoggingHandlerTest sometimes failure due to unexpected log messages logged due to the automatic reclaimation of thread-local objects. Expectation failure on verify: Appender.doAppend([DEBUG] Freed 3 thread-local buffer(s) from thread: nioEventLoopGroup-23-0): expected: 1, actual: 0 Appender.doAppend([DEBUG] Freed 9 thread-local buffer(s) from thread: nioEventLoopGroup-23-1): expected: 1, actual: 0 Appender.doAppend([DEBUG] Freed 2 thread-local buffer(s) from thread: nioEventLoopGroup-23-2): expected: 1, actual: 0 Appender.doAppend([DEBUG] Freed 4 thread-local buffer(s) from thread: nioEventLoopGroup-26-0): expected: 1, actual: 0 Appender.doAppend(matchesLog(expected: ".+CLOSE$", got: "[id: 0xembedded, embedded => embedded] CLOSE")): expected: 1, actual: 0 Modifications: Add the mock appender to the related logger only Result: No more intermittent test failures --- .../handler/logging/LoggingHandlerTest.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/handler/src/test/java/io/netty/handler/logging/LoggingHandlerTest.java b/handler/src/test/java/io/netty/handler/logging/LoggingHandlerTest.java index 811698a198..b1dec3a9fe 100644 --- a/handler/src/test/java/io/netty/handler/logging/LoggingHandlerTest.java +++ b/handler/src/test/java/io/netty/handler/logging/LoggingHandlerTest.java @@ -39,16 +39,27 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import static org.easymock.EasyMock.*; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.reportMatcher; +import static org.easymock.EasyMock.verify; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.CoreMatchers.sameInstance; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.slf4j.Logger.ROOT_LOGGER_NAME; /** * Verifies the correct functionality of the {@link LoggingHandler}. */ public class LoggingHandlerTest { - private static final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); + private static final String LOGGER_NAME = LoggingHandler.class.getName(); + + private static final Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME); + private static final Logger logger = (Logger) LoggerFactory.getLogger(LOGGER_NAME); + private static final List> oldAppenders = new ArrayList>(); /** * Custom logback appender which gets used to match on log messages. @@ -57,10 +68,10 @@ public class LoggingHandlerTest { @BeforeClass public static void beforeClass() { - for (Iterator> i = root.iteratorForAppenders(); i.hasNext();) { + for (Iterator> i = rootLogger.iteratorForAppenders(); i.hasNext();) { Appender a = i.next(); oldAppenders.add(a); - root.detachAppender(a); + rootLogger.detachAppender(a); } Unpooled.buffer(); @@ -69,7 +80,7 @@ public class LoggingHandlerTest { @AfterClass public static void afterClass() { for (Appender a: oldAppenders) { - root.addAppender(a); + rootLogger.addAppender(a); } } @@ -77,12 +88,12 @@ public class LoggingHandlerTest { @SuppressWarnings("unchecked") public void setup() { appender = createNiceMock(Appender.class); - root.addAppender(appender); + logger.addAppender(appender); } @After public void teardown() { - root.detachAppender(appender); + logger.detachAppender(appender); } @Test(expected = NullPointerException.class) @@ -93,7 +104,7 @@ public class LoggingHandlerTest { @Test public void shouldApplyCustomLogLevel() { - LoggingHandler handler = new LoggingHandler("LoggingHandlerTest", LogLevel.INFO); + LoggingHandler handler = new LoggingHandler(LogLevel.INFO); assertEquals(LogLevel.INFO, handler.level()); }