You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by ge...@apache.org on 2024/05/29 17:28:10 UTC

(spark) branch master updated: [MINOR][CORE][TESTS] Make SparkLoggerSuiteBase code more concise

This is an automated email from the ASF dual-hosted git repository.

gengliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new ed436139743a [MINOR][CORE][TESTS] Make SparkLoggerSuiteBase code more concise
ed436139743a is described below

commit ed436139743abcc503222dee1a0c775a76779f75
Author: panbingkun <pa...@baidu.com>
AuthorDate: Wed May 29 10:27:59 2024 -0700

    [MINOR][CORE][TESTS] Make SparkLoggerSuiteBase code more concise
    
    ### What changes were proposed in this pull request?
    The pr aims to make `SparkLoggerSuiteBase` code more concise.
    
    ### Why are the changes needed?
    Only for tests.
    
    ### Does this PR introduce _any_ user-facing change?
    No.
    
    ### How was this patch tested?
    - Manually test.
    - Pass GA.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    No.
    
    Closes #46748 from panbingkun/minor_SparkLoggerSuiteBase.
    
    Authored-by: panbingkun <pa...@baidu.com>
    Signed-off-by: Gengliang Wang <ge...@apache.org>
---
 .../apache/spark/util/SparkLoggerSuiteBase.java    | 101 +++++++--------------
 1 file changed, 35 insertions(+), 66 deletions(-)

diff --git a/common/utils/src/test/java/org/apache/spark/util/SparkLoggerSuiteBase.java b/common/utils/src/test/java/org/apache/spark/util/SparkLoggerSuiteBase.java
index 90677b521640..079899581dc0 100644
--- a/common/utils/src/test/java/org/apache/spark/util/SparkLoggerSuiteBase.java
+++ b/common/utils/src/test/java/org/apache/spark/util/SparkLoggerSuiteBase.java
@@ -55,6 +55,19 @@ public abstract class SparkLoggerSuiteBase {
     return newContent.substring(content.length());
   }
 
+  @FunctionalInterface
+  private interface ExpectedResult {
+    String apply(Level level) throws IOException;
+  }
+
+  private void checkLogOutput(Level level, Runnable func, ExpectedResult result) {
+    try {
+      assertTrue(captureLogOutput(func).matches(result.apply(level)));
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
   private String basicMsg() {
     return "This is a log message";
   }
@@ -111,14 +124,8 @@ public abstract class SparkLoggerSuiteBase {
         Pair.of(Level.WARN, warnFn),
         Pair.of(Level.INFO, infoFn),
         Pair.of(Level.DEBUG, debugFn),
-        Pair.of(Level.TRACE, traceFn)).forEach(pair -> {
-      try {
-        assertTrue(captureLogOutput(pair.getRight()).matches(
-          expectedPatternForBasicMsg(pair.getLeft())));
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
-    });
+        Pair.of(Level.TRACE, traceFn)).forEach(pair ->
+      checkLogOutput(pair.getLeft(), pair.getRight(), this::expectedPatternForBasicMsg));
   }
 
   @Test
@@ -134,14 +141,9 @@ public abstract class SparkLoggerSuiteBase {
         Pair.of(Level.WARN, warnFn),
         Pair.of(Level.INFO, infoFn),
         Pair.of(Level.DEBUG, debugFn),
-        Pair.of(Level.TRACE, traceFn)).forEach(pair -> {
-      try {
-        assertTrue(captureLogOutput(pair.getRight()).matches(
-          expectedPatternForBasicMsgWithException(pair.getLeft())));
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
-    });
+        Pair.of(Level.TRACE, traceFn)).forEach(pair ->
+      checkLogOutput(pair.getLeft(), pair.getRight(),
+        this::expectedPatternForBasicMsgWithException));
   }
 
   @Test
@@ -150,16 +152,10 @@ public abstract class SparkLoggerSuiteBase {
     Runnable warnFn = () -> logger().warn(msgWithMDC, executorIDMDC);
     Runnable infoFn = () -> logger().info(msgWithMDC, executorIDMDC);
     List.of(
-        Pair.of(Level.ERROR, errorFn),
-        Pair.of(Level.WARN, warnFn),
-        Pair.of(Level.INFO, infoFn)).forEach(pair -> {
-      try {
-        assertTrue(captureLogOutput(pair.getRight()).matches(
-          expectedPatternForMsgWithMDC(pair.getLeft())));
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
-    });
+       Pair.of(Level.ERROR, errorFn),
+       Pair.of(Level.WARN, warnFn),
+       Pair.of(Level.INFO, infoFn)).forEach(pair ->
+      checkLogOutput(pair.getLeft(), pair.getRight(), this::expectedPatternForMsgWithMDC));
   }
 
   @Test
@@ -170,14 +166,8 @@ public abstract class SparkLoggerSuiteBase {
     List.of(
         Pair.of(Level.ERROR, errorFn),
         Pair.of(Level.WARN, warnFn),
-        Pair.of(Level.INFO, infoFn)).forEach(pair -> {
-      try {
-        assertTrue(captureLogOutput(pair.getRight()).matches(
-          expectedPatternForMsgWithMDCs(pair.getLeft())));
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
-    });
+        Pair.of(Level.INFO, infoFn)).forEach(pair ->
+      checkLogOutput(pair.getLeft(), pair.getRight(), this::expectedPatternForMsgWithMDCs));
   }
 
   @Test
@@ -189,14 +179,10 @@ public abstract class SparkLoggerSuiteBase {
     List.of(
         Pair.of(Level.ERROR, errorFn),
         Pair.of(Level.WARN, warnFn),
-        Pair.of(Level.INFO, infoFn)).forEach(pair -> {
-      try {
-        assertTrue(captureLogOutput(pair.getRight()).matches(
-          expectedPatternForMsgWithMDCsAndException(pair.getLeft())));
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
-    });
+        Pair.of(Level.INFO, infoFn)).forEach(pair ->
+      checkLogOutput(pair.getLeft(), pair.getRight(),
+        this::expectedPatternForMsgWithMDCsAndException)
+    );
   }
 
   @Test
@@ -207,14 +193,9 @@ public abstract class SparkLoggerSuiteBase {
     List.of(
         Pair.of(Level.ERROR, errorFn),
         Pair.of(Level.WARN, warnFn),
-        Pair.of(Level.INFO, infoFn)).forEach(pair -> {
-      try {
-        assertTrue(captureLogOutput(pair.getRight()).matches(
-          expectedPatternForMsgWithMDCValueIsNull(pair.getLeft())));
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
-    });
+        Pair.of(Level.INFO, infoFn)).forEach(pair ->
+      checkLogOutput(pair.getLeft(), pair.getRight(),
+        this::expectedPatternForMsgWithMDCValueIsNull));
   }
 
   @Test
@@ -225,14 +206,8 @@ public abstract class SparkLoggerSuiteBase {
     List.of(
         Pair.of(Level.ERROR, errorFn),
         Pair.of(Level.WARN, warnFn),
-        Pair.of(Level.INFO, infoFn)).forEach(pair -> {
-      try {
-        assertTrue(captureLogOutput(pair.getRight()).matches(
-          expectedPatternForScalaCustomLogKey(pair.getLeft())));
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
-    });
+        Pair.of(Level.INFO, infoFn)).forEach(pair ->
+      checkLogOutput(pair.getLeft(), pair.getRight(), this::expectedPatternForScalaCustomLogKey));
   }
 
   @Test
@@ -243,14 +218,8 @@ public abstract class SparkLoggerSuiteBase {
     List.of(
         Pair.of(Level.ERROR, errorFn),
         Pair.of(Level.WARN, warnFn),
-        Pair.of(Level.INFO, infoFn)).forEach(pair -> {
-      try {
-        assertTrue(captureLogOutput(pair.getRight()).matches(
-          expectedPatternForJavaCustomLogKey(pair.getLeft())));
-      } catch (IOException e) {
-        throw new RuntimeException(e);
-      }
-    });
+        Pair.of(Level.INFO, infoFn)).forEach(pair ->
+      checkLogOutput(pair.getLeft(), pair.getRight(), this::expectedPatternForJavaCustomLogKey));
   }
 }
 


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