hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aengin...@apache.org
Subject [25/50] [abbrv] hadoop git commit: HADOOP-13470. GenericTestUtils$LogCapturer is flaky. (Contributed by Mingliang Liu)
Date Fri, 19 Aug 2016 20:33:40 GMT
HADOOP-13470. GenericTestUtils$LogCapturer is flaky. (Contributed by Mingliang Liu)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/23532716
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/23532716
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/23532716

Branch: refs/heads/HDFS-7240
Commit: 23532716fcd3f7e5e20b8f9fc66188041638510a
Parents: 3808876
Author: Mingliang Liu <liuml07@apache.org>
Authored: Tue Aug 16 16:30:43 2016 -0700
Committer: Mingliang Liu <liuml07@apache.org>
Committed: Tue Aug 16 17:33:04 2016 -0700

----------------------------------------------------------------------
 .../apache/hadoop/test/GenericTestUtils.java    | 31 ++++++++------
 .../hadoop/test/TestGenericTestUtils.java       | 44 ++++++++++++++++++++
 2 files changed, 63 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/23532716/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
index 116a111..0b73cf5 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/GenericTestUtils.java
@@ -42,10 +42,12 @@ import org.apache.commons.logging.impl.Log4JLogger;
 import org.apache.hadoop.fs.FileUtil;
 import org.apache.hadoop.util.StringUtils;
 import org.apache.hadoop.util.Time;
+import org.apache.log4j.Appender;
 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,36 +277,41 @@ public abstract class GenericTestUtils {
     private StringWriter sw = new StringWriter();
     private WriterAppender appender;
     private Logger logger;
-    
+
     public static LogCapturer captureLogs(Log l) {
       Logger logger = ((Log4JLogger)l).getLogger();
-      LogCapturer c = new LogCapturer(logger);
-      return c;
+      return new LogCapturer(logger);
+    }
+
+    public static LogCapturer captureLogs(org.slf4j.Logger logger) {
+      return new LogCapturer(toLog4j(logger));
     }
-    
 
     private LogCapturer(Logger logger) {
       this.logger = logger;
-      Layout layout = Logger.getRootLogger().getAppender("stdout").getLayout();
-      WriterAppender wa = new WriterAppender(layout, sw);
-      logger.addAppender(wa);
+      Appender defaultAppender = Logger.getRootLogger().getAppender("stdout");
+      if (defaultAppender == null) {
+        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() {
       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.

http://git-wip-us.apache.org/repos/asf/hadoop/blob/23532716/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
index 8a7b5f6..86df5d5 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/test/TestGenericTestUtils.java
@@ -18,8 +18,16 @@
 
 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
@@ -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());
+  }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org


Mime
View raw message