Harden LoggingReceiveSpec, #23140
* It ended up in endless loop because receiveOne may return null * I think the match on the watching was wrong
This commit is contained in:
parent
8528deb306
commit
c9c92026a9
1 changed files with 15 additions and 8 deletions
|
|
@ -12,6 +12,7 @@ import org.scalatest.WordSpec
|
||||||
import com.typesafe.config.ConfigFactory
|
import com.typesafe.config.ConfigFactory
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
import akka.actor._
|
import akka.actor._
|
||||||
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
object LoggingReceiveSpec {
|
object LoggingReceiveSpec {
|
||||||
class TestLogActor extends Actor {
|
class TestLogActor extends Actor {
|
||||||
|
|
@ -229,10 +230,9 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterAll {
|
||||||
val sname = supervisor.path.toString
|
val sname = supervisor.path.toString
|
||||||
val sclass = classOf[TestLogActor]
|
val sclass = classOf[TestLogActor]
|
||||||
|
|
||||||
expectMsgAllPF(messages = 3) {
|
expectMsgAllPF(messages = 2) {
|
||||||
case Logging.Debug(`sname`, `sclass`, msg: String) if msg startsWith "started" ⇒ 0
|
case Logging.Debug(`sname`, `sclass`, msg: String) if msg startsWith "started" ⇒ 0
|
||||||
case Logging.Debug(_, _, msg: String) if msg startsWith "now supervising" ⇒ 1
|
case Logging.Debug(_, _, msg: String) if msg startsWith "now supervising" ⇒ 1
|
||||||
case Logging.Debug(_, _, msg: String) if msg startsWith "now watched by" ⇒ 2
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val actor = TestActorRef[TestLogActor](Props[TestLogActor], supervisor, "none")
|
val actor = TestActorRef[TestLogActor](Props[TestLogActor], supervisor, "none")
|
||||||
|
|
@ -260,16 +260,23 @@ class LoggingReceiveSpec extends WordSpec with BeforeAndAfterAll {
|
||||||
Logging.Debug(sname, sclass, "stopped"))
|
Logging.Debug(sname, sclass, "stopped"))
|
||||||
}
|
}
|
||||||
|
|
||||||
def expectMsgAllPF(messages: Int)(matchers: PartialFunction[AnyRef, Int]) = {
|
def expectMsgAllPF(messages: Int)(matchers: PartialFunction[AnyRef, Int]): Set[Int] = {
|
||||||
def receiveNMatching(remaining: Int): Set[Int] =
|
val max = remainingOrDefault
|
||||||
if (remaining == 0) Set.empty
|
@tailrec def receiveNMatching(gotMatching: Set[Int], unknown: Vector[Any]): Set[Int] = {
|
||||||
|
if (unknown.size >= 20)
|
||||||
|
throw new IllegalStateException(s"Got too many unknown messages: [${unknown.mkString(", ")}]")
|
||||||
|
else if (gotMatching.size == messages) gotMatching
|
||||||
else {
|
else {
|
||||||
val msg = receiveOne(remainingOrDefault)
|
val msg = receiveOne(remainingOrDefault)
|
||||||
if (matchers.isDefinedAt(msg)) receiveNMatching(remaining - 1) + matchers(msg)
|
assert(msg ne null, s"timeout ($max) during expectMsgAllPF, got matching " +
|
||||||
else receiveNMatching(remaining) // unknown message, just ignore
|
s"[${gotMatching.mkString(", ")}], got unknown: [${unknown.mkString(", ")}]")
|
||||||
|
if (matchers.isDefinedAt(msg)) receiveNMatching(gotMatching + matchers(msg), Vector.empty)
|
||||||
|
else receiveNMatching(gotMatching, unknown :+ msg) // unknown message, just ignore
|
||||||
}
|
}
|
||||||
val set = receiveNMatching(messages)
|
}
|
||||||
|
val set = receiveNMatching(Set.empty, Vector.empty)
|
||||||
assert(set == (0 until messages).toSet)
|
assert(set == (0 until messages).toSet)
|
||||||
|
set
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue