Skip to content

Commit

Permalink
Fix intermittent test failure in LoggingHandlerTest
Browse files Browse the repository at this point in the history
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
  • Loading branch information
trustin committed Mar 31, 2015
1 parent f1f7f92 commit d796920
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,27 @@
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<Appender<ILoggingEvent>> oldAppenders = new ArrayList<Appender<ILoggingEvent>>();
/**
* Custom logback appender which gets used to match on log messages.
Expand All @@ -57,10 +68,10 @@ public class LoggingHandlerTest {

@BeforeClass
public static void beforeClass() {
for (Iterator<Appender<ILoggingEvent>> i = root.iteratorForAppenders(); i.hasNext();) {
for (Iterator<Appender<ILoggingEvent>> i = rootLogger.iteratorForAppenders(); i.hasNext();) {
Appender<ILoggingEvent> a = i.next();
oldAppenders.add(a);
root.detachAppender(a);
rootLogger.detachAppender(a);
}

Unpooled.buffer();
Expand All @@ -69,20 +80,20 @@ public static void beforeClass() {
@AfterClass
public static void afterClass() {
for (Appender<ILoggingEvent> a: oldAppenders) {
root.addAppender(a);
rootLogger.addAppender(a);
}
}

@Before
@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)
Expand All @@ -93,7 +104,7 @@ public void shouldNotAcceptNullLogLevel() {

@Test
public void shouldApplyCustomLogLevel() {
LoggingHandler handler = new LoggingHandler("LoggingHandlerTest", LogLevel.INFO);
LoggingHandler handler = new LoggingHandler(LogLevel.INFO);
assertEquals(LogLevel.INFO, handler.level());
}

Expand Down

0 comments on commit d796920

Please sign in to comment.