Skip to content

Commit

Permalink
fix: flaky logging mdc tests (#1410)
Browse files Browse the repository at this point in the history
* fix: flaky logging mdc tests

* fix: use MDC as fallback

* remove ci change

* fix npe static mdc
  • Loading branch information
Roiocam authored Jul 24, 2024
1 parent f9ad446 commit 0930982
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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,
Expand All @@ -98,7 +105,7 @@ import pekko.annotation.InternalApi
timeStamp = event.getTimeStamp,
marker = marker,
throwable = throwable,
mdc = event.getMDCPropertyMap.asScala.toMap)
mdc = mdc)

filter(loggingEvent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
""")
}

Expand All @@ -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)
}
Expand Down

0 comments on commit 0930982

Please sign in to comment.