fix: flaky logging mdc tests (#1410)

* fix: flaky logging mdc tests

* fix: use MDC as fallback

* remove ci change

* fix npe static mdc
This commit is contained in:
Andy(Jingzhang)Chen 2024-07-24 20:46:19 +08:00 committed by GitHub
parent f9ad446a58
commit 0930982b9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 7 deletions

View file

@ -16,12 +16,13 @@ package org.apache.pekko.actor.testkit.typed.internal
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.classic.spi.ThrowableProxy
import ch.qos.logback.core.AppenderBase
import org.slf4j.Marker
import org.slf4j.{ MDC, Marker }
import org.apache.pekko
import pekko.actor.testkit.typed.LoggingEvent
import pekko.annotation.InternalApi
import java.util.Collections
/**
* INTERNAL API
*
@ -90,6 +91,12 @@ import pekko.annotation.InternalApi
}
val marker: Option[Marker] = Option(event.getMarkerList).flatMap(_.asScala.headOption)
val mdc: Map[String, String] = Option(event.getMDCPropertyMap)
.filterNot(_.isEmpty)
.orElse(Option(MDC.getMDCAdapter.getCopyOfContextMap))
.getOrElse(Collections.emptyMap())
.asScala.toMap
val loggingEvent = LoggingEvent(
level = convertLevel(event.getLevel),
message = event.getFormattedMessage,
@ -98,7 +105,7 @@ import pekko.annotation.InternalApi
timeStamp = event.getTimeStamp,
marker = marker,
throwable = throwable,
mdc = event.getMDCPropertyMap.asScala.toMap)
mdc = mdc)
filter(loggingEvent)
}

View file

@ -28,12 +28,8 @@ import pekko.actor.typed.scaladsl.Behaviors
object ClusterActorLoggingSpec {
def config = ConfigFactory.parseString("""
pekko.actor.provider = cluster
pekko.remote.classic.netty.tcp.port = 0
pekko.remote.artery.canonical.port = 0
pekko.remote.artery.canonical.hostname = 127.0.0.1
# generous timeout for cluster forming probes
pekko.actor.testkit.typed.default-timeout = 10s
pekko.actor.testkit.typed.filter-leeway = 10s
""")
}
@ -58,8 +54,10 @@ class ClusterActorLoggingSpec
LoggingTestKit
.info("Starting")
.withCustom { event =>
event.mdc.contains(ActorMdc.PekkoAddressKey) &&
event.mdc(ActorMdc.PekkoAddressKey) == addressString
}
.withLoggerName("org.apache.pekko.cluster.typed.ClusterActorLoggingSpec")
.expect {
spawn(behavior)
}