我正在尝试验证何时在Actor with Stash
中抛出StashOverflowException
。为此,我将stash-capacity
设置为某个上限,如下所示:
akka.actor.deployment.default-mailbox.stash-capacity = 10
并实现了一个简单的Actor,用于存储它收到的所有消息。然后,Actor 向发送者发出信号,如果存储成功与否:
import akka.actor.{ Actor, ActorSystem, Props, Stash, StashOverflowException }
object StashExample {
val SUCCESS = "success"
val FAILURE = "failure"
val STASH_CAPACITY = 10
}
final class StashExample extends Actor with Stash {
import StashExample._
var count = 0
override def receive = {
case _: String =>
count += 1
System.out.println(s"Received ${count} messages.")
try {
stash()
sender ! SUCCESS
} catch {
case _: StashOverflowException => sender ! FAILURE
}
}
}
问题是,无论向该参与者发送多少条消息,都不会抛出StashOverflowException
。
下面是一个尝试验证这一点的简单测试:
import akka.testkit.{ ImplicitSender, TestKit }
import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpecLike }
final class Test
extends TestKit(ActorSystem("Test"))
with ImplicitSender
with WordSpecLike
with Matchers
with BeforeAndAfterAll {
import StashExample._
"stash operation" should {
"throw overflow exception when the stash is full" in {
val actorRef = system.actorOf(Props(new StashExample()))
// ensure stash-capacity is configured as expected.
system.settings.config
.getInt("akka.actor.deployment.default-mailbox.stash-capacity") shouldBe STASH_CAPACITY
// make the stash full.
(0 until STASH_CAPACITY).foreach(_ => {
actorRef ! "ping"
expectMsg(SUCCESS)
})
actorRef ! "ping"
expectMsg(FAILURE)
}
}
}
下面是测试失败:
Received 1 messages.
Received 2 messages.
Received 3 messages.
Received 4 messages.
Received 5 messages.
Received 6 messages.
Received 7 messages.
Received 8 messages.
Received 9 messages.
Received 10 messages.
Received 11 messages.
assertion failed: expected failure, found success
java.lang.AssertionError: assertion failed: expected failure, found success
删除配置键中的deployment
:
akka.actor.default-mailbox.stash-capacity = 10