Skip to content

Commit 17c6540

Browse files
committed
Use custom EventFilter class with @volatile buffer to fix test race condition
1 parent e2f0da6 commit 17c6540

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

akka-http-tests/src/test/scala/akka/http/scaladsl/server/directives/MdcLoggingDirectivesSpec.scala

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import akka.event._
44
import akka.http.scaladsl.server.RoutingSpec
55
import akka.testkit.EventFilter
66

7-
import scala.collection.mutable.ListBuffer
8-
97
class MdcLoggingDirectivesSpec extends RoutingSpec {
108

119
"The `withMarkerLoggingAdapter` directive" should {
@@ -55,13 +53,17 @@ class MdcLoggingDirectivesSpec extends RoutingSpec {
5553
}
5654
}
5755
"include the entries in the LoggingEvents" in {
58-
val buf = ListBuffer.empty[Logging.Info2]
59-
val filter = EventFilter.custom {
60-
case e: Logging.Info2 =>
61-
buf.append(e)
62-
true
63-
case _ => false
56+
class EF(n: Int) extends EventFilter(n) {
57+
@volatile
58+
var buf = Vector.empty[Logging.Info2]
59+
override protected def matches(event: Logging.LogEvent): Boolean = event match {
60+
case e: Logging.Info2 =>
61+
buf :+= e
62+
true
63+
case _ => false
64+
}
6465
}
66+
val filter = new EF(3)
6567
filter.intercept {
6668
Get() ~> withMdcEntries("user_id" -> "1234", "request_id" -> "abcd") {
6769
extractLog { log1 =>
@@ -76,10 +78,10 @@ class MdcLoggingDirectivesSpec extends RoutingSpec {
7678
}
7779
} ~> check {
7880
response shouldEqual Ok
79-
buf.size shouldBe 3
80-
val l1 = buf(0)
81-
val l2 = buf(1)
82-
val l3 = buf(2)
81+
filter.buf.size shouldBe 3
82+
val l1 = filter.buf(0)
83+
val l2 = filter.buf(1)
84+
val l3 = filter.buf(2)
8385
l1.message shouldBe "test 1"
8486
l1.mdc shouldBe Map("user_id" -> "1234", "request_id" -> "abcd")
8587
l2.message shouldBe "test 2"

0 commit comments

Comments
 (0)