HADOOP-13470. GenericTestUtils$LogCapturer is flaky. (Contributed by Mingliang Liu)
This commit is contained in:
parent
ac779f2554
commit
00eb790622
|
@ -43,10 +43,12 @@ import org.apache.hadoop.fs.FileUtil;
|
||||||
import org.apache.hadoop.util.NativeCodeLoader;
|
import org.apache.hadoop.util.NativeCodeLoader;
|
||||||
import org.apache.hadoop.util.StringUtils;
|
import org.apache.hadoop.util.StringUtils;
|
||||||
import org.apache.hadoop.util.Time;
|
import org.apache.hadoop.util.Time;
|
||||||
|
import org.apache.log4j.Appender;
|
||||||
import org.apache.log4j.Layout;
|
import org.apache.log4j.Layout;
|
||||||
import org.apache.log4j.Level;
|
import org.apache.log4j.Level;
|
||||||
import org.apache.log4j.LogManager;
|
import org.apache.log4j.LogManager;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
import org.apache.log4j.PatternLayout;
|
||||||
import org.apache.log4j.WriterAppender;
|
import org.apache.log4j.WriterAppender;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Assume;
|
import org.junit.Assume;
|
||||||
|
@ -200,16 +202,23 @@ public abstract class GenericTestUtils {
|
||||||
|
|
||||||
public static LogCapturer captureLogs(Log l) {
|
public static LogCapturer captureLogs(Log l) {
|
||||||
Logger logger = ((Log4JLogger)l).getLogger();
|
Logger logger = ((Log4JLogger)l).getLogger();
|
||||||
LogCapturer c = new LogCapturer(logger);
|
return new LogCapturer(logger);
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static LogCapturer captureLogs(org.slf4j.Logger logger) {
|
||||||
|
return new LogCapturer(toLog4j(logger));
|
||||||
|
}
|
||||||
|
|
||||||
private LogCapturer(Logger logger) {
|
private LogCapturer(Logger logger) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
Layout layout = Logger.getRootLogger().getAppender("stdout").getLayout();
|
Appender defaultAppender = Logger.getRootLogger().getAppender("stdout");
|
||||||
WriterAppender wa = new WriterAppender(layout, sw);
|
if (defaultAppender == null) {
|
||||||
logger.addAppender(wa);
|
defaultAppender = Logger.getRootLogger().getAppender("console");
|
||||||
|
}
|
||||||
|
final Layout layout = (defaultAppender == null) ? new PatternLayout() :
|
||||||
|
defaultAppender.getLayout();
|
||||||
|
this.appender = new WriterAppender(layout, sw);
|
||||||
|
logger.addAppender(this.appender);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getOutput() {
|
public String getOutput() {
|
||||||
|
@ -218,7 +227,6 @@ public abstract class GenericTestUtils {
|
||||||
|
|
||||||
public void stopCapturing() {
|
public void stopCapturing() {
|
||||||
logger.removeAppender(appender);
|
logger.removeAppender(appender);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearOutput() {
|
public void clearOutput() {
|
||||||
|
@ -226,7 +234,6 @@ public abstract class GenericTestUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mockito answer helper that triggers one latch as soon as the
|
* Mockito answer helper that triggers one latch as soon as the
|
||||||
* method is called, then waits on another before continuing.
|
* method is called, then waits on another before continuing.
|
||||||
|
|
|
@ -18,8 +18,16 @@
|
||||||
|
|
||||||
package org.apache.hadoop.test;
|
package org.apache.hadoop.test;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class TestGenericTestUtils extends GenericTestUtils {
|
public class TestGenericTestUtils extends GenericTestUtils {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -75,4 +83,40 @@ public class TestGenericTestUtils extends GenericTestUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 10000)
|
||||||
|
public void testLogCapturer() {
|
||||||
|
final Log log = LogFactory.getLog(TestGenericTestUtils.class);
|
||||||
|
LogCapturer logCapturer = LogCapturer.captureLogs(log);
|
||||||
|
final String infoMessage = "info message";
|
||||||
|
// test get output message
|
||||||
|
log.info(infoMessage);
|
||||||
|
assertTrue(logCapturer.getOutput().endsWith(
|
||||||
|
String.format(infoMessage + "%n")));
|
||||||
|
// test clear output
|
||||||
|
logCapturer.clearOutput();
|
||||||
|
assertTrue(logCapturer.getOutput().isEmpty());
|
||||||
|
// test stop capturing
|
||||||
|
logCapturer.stopCapturing();
|
||||||
|
log.info(infoMessage);
|
||||||
|
assertTrue(logCapturer.getOutput().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 10000)
|
||||||
|
public void testLogCapturerSlf4jLogger() {
|
||||||
|
final Logger logger = LoggerFactory.getLogger(TestGenericTestUtils.class);
|
||||||
|
LogCapturer logCapturer = LogCapturer.captureLogs(logger);
|
||||||
|
final String infoMessage = "info message";
|
||||||
|
// test get output message
|
||||||
|
logger.info(infoMessage);
|
||||||
|
assertTrue(logCapturer.getOutput().endsWith(
|
||||||
|
String.format(infoMessage + "%n")));
|
||||||
|
// test clear output
|
||||||
|
logCapturer.clearOutput();
|
||||||
|
assertTrue(logCapturer.getOutput().isEmpty());
|
||||||
|
// test stop capturing
|
||||||
|
logCapturer.stopCapturing();
|
||||||
|
logger.info(infoMessage);
|
||||||
|
assertTrue(logCapturer.getOutput().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue