Skip to content

Commit

Permalink
[SPARK-48490][CORE][TESTS][FOLLOWUP] Add some UT for the Windows path…
Browse files Browse the repository at this point in the history
…s scene

### What changes were proposed in this pull request?
The pr is followup #47050

### Why are the changes needed?
Add some UT for the Windows paths scene.

### Does this PR introduce _any_ user-facing change?
No.

### How was this patch tested?
Pass GA.

### Was this patch authored or co-authored using generative AI tooling?
No.

Closes #47051 from panbingkun/SPARK-48490_FOLLOWUP_TESTS.

Authored-by: panbingkun <[email protected]>
Signed-off-by: Gengliang Wang <[email protected]>
  • Loading branch information
panbingkun authored and gengliangwang committed Jun 21, 2024
1 parent 0bc38ac commit 9414211
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class PatternLoggingSuite extends LoggingSuiteBase with BeforeAndAfterAll {
s""".*$level $className: This is a log message\nThis is a new line \t other msg\n"""
}

override def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String = {
s""".*$level $className: The first message\nthe first new line\tthe first other msg\n""" +
s"""[\\s\\S]*The second message\nthe second new line\tthe second other msg\n"""
}

override def expectedPatternForBasicMsgWithException(level: Level): String = {
s""".*$level $className: This is a log message\n[\\s\\S]*"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ trait LoggingSuiteBase
def basicMsgWithEscapeCharMDC: LogEntry =
log"This is a log message\nThis is a new line \t other msg"

// scalastyle:off line.size.limit
def msgWithMDCAndEscapeChar: LogEntry =
log"The first message\nthe first new line\tthe first other msg\n${MDC(LogKeys.PATHS, "C:\\Users\\run-all_1.R\nC:\\Users\\run-all_2.R")}\nThe second message\nthe second new line\tthe second other msg"
// scalastyle:on line.size.limit

def msgWithMDC: LogEntry = log"Lost executor ${MDC(LogKeys.EXECUTOR_ID, "1")}."

def msgWithMDCValueIsNull: LogEntry = log"Lost executor ${MDC(LogKeys.EXECUTOR_ID, null)}."
Expand All @@ -77,6 +82,9 @@ trait LoggingSuiteBase
// test for basic message (with escape char mdc)
def expectedPatternForBasicMsgWithEscapeCharMDC(level: Level): String

// test for message (with mdc and escape char)
def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String

// test for basic message and exception
def expectedPatternForBasicMsgWithException(level: Level): String

Expand Down Expand Up @@ -130,6 +138,19 @@ trait LoggingSuiteBase
}
}

test("Logging with MDC and escape char") {
Seq(
(Level.ERROR, () => logError(msgWithMDCAndEscapeChar)),
(Level.WARN, () => logWarning(msgWithMDCAndEscapeChar)),
(Level.INFO, () => logInfo(msgWithMDCAndEscapeChar)),
(Level.DEBUG, () => logDebug(msgWithMDCAndEscapeChar)),
(Level.TRACE, () => logTrace(msgWithMDCAndEscapeChar))
).foreach { case (level, logFunc) =>
val logOutput = captureLogOutput(logFunc)
assert(expectedPatternForMsgWithMDCAndEscapeChar(level).r.matches(logOutput))
}
}

test("Basic logging with Exception") {
val exception = new RuntimeException("OOM")
Seq(
Expand Down Expand Up @@ -218,6 +239,7 @@ class StructuredLoggingSuite extends LoggingSuiteBase {
jsonMapper.readTree(json).toString.
replace("<timestamp>", """[^"]+""").
replace(""""<stacktrace>"""", """.*""").
replace("<windows_paths>", """.*""").
replace("{", """\{""") + "\n"
}

Expand Down Expand Up @@ -254,6 +276,22 @@ class StructuredLoggingSuite extends LoggingSuiteBase {
}""")
}

override def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String = {
// scalastyle:off line.size.limit
compactAndToRegexPattern(
s"""
{
"ts": "<timestamp>",
"level": "$level",
"msg": "The first message\\\\nthe first new line\\\\tthe first other msg\\\\n<windows_paths>\\\\nThe second message\\\\nthe second new line\\\\tthe second other msg",
"context": {
"paths": "<windows_paths>"
},
"logger": "$className"
}""")
// scalastyle:on line.size.limit
}

override def expectedPatternForBasicMsgWithException(level: Level): String = {
compactAndToRegexPattern(
s"""
Expand Down

0 comments on commit 9414211

Please sign in to comment.