Revert "HADOOP-13470. GenericTestUtils$LogCapturer is flaky. (Contributed by Mingliang Liu)"
This reverts commit 9336a0495f
.
This commit is contained in:
parent
4b689e7a75
commit
27a6e09c4e
@ -42,10 +42,10 @@
|
||||
import org.apache.hadoop.fs.FileUtil;
|
||||
import org.apache.hadoop.util.StringUtils;
|
||||
import org.apache.hadoop.util.Time;
|
||||
import org.apache.log4j.Layout;
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.log4j.PatternLayout;
|
||||
import org.apache.log4j.WriterAppender;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Assume;
|
||||
@ -275,35 +275,36 @@ public static class LogCapturer {
|
||||
private StringWriter sw = new StringWriter();
|
||||
private WriterAppender appender;
|
||||
private Logger logger;
|
||||
|
||||
|
||||
public static LogCapturer captureLogs(Log l) {
|
||||
Logger logger = ((Log4JLogger)l).getLogger();
|
||||
return new LogCapturer(logger);
|
||||
}
|
||||
|
||||
public static LogCapturer captureLogs(org.slf4j.Logger logger) {
|
||||
return new LogCapturer(toLog4j(logger));
|
||||
LogCapturer c = new LogCapturer(logger);
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
private LogCapturer(Logger logger) {
|
||||
this.logger = logger;
|
||||
this.appender = new WriterAppender(new PatternLayout(), sw);
|
||||
logger.addAppender(appender);
|
||||
Layout layout = Logger.getRootLogger().getAppender("stdout").getLayout();
|
||||
WriterAppender wa = new WriterAppender(layout, sw);
|
||||
logger.addAppender(wa);
|
||||
}
|
||||
|
||||
|
||||
public String getOutput() {
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
|
||||
public void stopCapturing() {
|
||||
logger.removeAppender(appender);
|
||||
|
||||
}
|
||||
|
||||
public void clearOutput() {
|
||||
sw.getBuffer().setLength(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Mockito answer helper that triggers one latch as soon as the
|
||||
* method is called, then waits on another before continuing.
|
||||
|
@ -18,16 +18,8 @@
|
||||
|
||||
package org.apache.hadoop.test;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestGenericTestUtils extends GenericTestUtils {
|
||||
|
||||
@Test
|
||||
@ -83,40 +75,4 @@ public String toString() {
|
||||
}
|
||||
}
|
||||
|
||||
@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
Block a user