Fix MailboxSelectorSpec race (#30429)

The comment already said there could be 2 dead letters, but the log
assertion failed when there was a second 'excess' one. Add a flag to
skip checking for excess log lines.
This commit is contained in:
Arnout Engelen 2021-07-30 11:54:42 +02:00 committed by GitHub
parent 42d85a3019
commit cca1c20d93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 4 deletions

View file

@ -0,0 +1,6 @@
# Extra methods on interfaces not for user extension:
ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.actor.testkit.typed.javadsl.LoggingTestKit.withCheckExcess")
ProblemFilters.exclude[ReversedMissingMethodProblem]("akka.actor.testkit.typed.scaladsl.LoggingTestKit.withCheckExcess")
# Impl
ProblemFilters.exclude[Problem]("akka.actor.testkit.typed.internal.LoggingTestKitImpl.*")

View file

@ -24,7 +24,8 @@ import akka.testkit.TestKit
* INTERNAL API
*/
@InternalApi private[akka] object LoggingTestKitImpl {
def empty: LoggingTestKitImpl = new LoggingTestKitImpl(1, None, None, None, None, None, None, Map.empty, None)
def empty: LoggingTestKitImpl =
new LoggingTestKitImpl(1, None, None, None, None, None, None, Map.empty, checkExcess = true, None)
}
/**
@ -39,6 +40,7 @@ import akka.testkit.TestKit
messageRegex: Option[Regex],
cause: Option[Class[_ <: Throwable]],
mdc: Map[String, String],
checkExcess: Boolean,
custom: Option[Function[LoggingEvent, Boolean]])
extends javadsl.LoggingTestKit
with scaladsl.LoggingTestKit {
@ -92,14 +94,14 @@ import akka.testkit.TestKit
try {
val result = code
// wait some more when occurrences=0 to find asynchronous exceess messages
// wait some more when occurrences=0 to find asynchronous excess messages
if (occurrences == 0)
awaitNoExcess(settings.ExpectNoMessageDefaultTimeout)
if (!awaitDone(settings.FilterLeeway))
if (todo > 0)
throw new AssertionError(s"Timeout (${settings.FilterLeeway}) waiting for $todo messages on $this.")
else
else if (checkExcess)
throw new AssertionError(s"Received ${-todo} excess messages on $this.")
result
} finally {
@ -152,6 +154,9 @@ import akka.testkit.TestKit
withMdc(newMdc.asScala.toMap)
}
override def withCheckExcess(check: Boolean): LoggingTestKitImpl =
copy(checkExcess = check)
override def withCustom(newCustom: Function[LoggingEvent, Boolean]): LoggingTestKitImpl =
copy(custom = Option(newCustom))

View file

@ -76,6 +76,11 @@ import akka.annotation.DoNotInherit
*/
def withMdc(newMdc: java.util.Map[String, String]): LoggingTestKit
/**
* After matching the expected number of hits, check for excess messages
*/
def withCheckExcess(check: Boolean): LoggingTestKit
/**
* Matching events for which the supplied function returns `true`.
*/

View file

@ -76,6 +76,11 @@ import akka.annotation.DoNotInherit
*/
def withMdc(newMdc: Map[String, String]): LoggingTestKit
/**
* After matching the expected number of hits, check for excess messages
*/
def withCheckExcess(checkExcess: Boolean): LoggingTestKit
/**
* Matching events for which the supplied function returns`true`.
*/

View file

@ -74,7 +74,7 @@ class MailboxSelectorSpec extends ScalaTestWithActorTestKit("""
}, MailboxSelector.bounded(2))
actor ! "one" // actor will block here
actor ! "two"
LoggingTestKit.deadLetters().expect {
LoggingTestKit.deadLetters().withCheckExcess(false).expect {
// one or both of these doesn't fit in mailbox
// depending on race with how fast actor consumes
actor ! "three"