1
StashOverflowException
がActor with Stash
にスローされたことを確認しようとしています。隠し容量はAkkaでどのように制限されるべきですか?
akka.actor.deployment.default-mailbox.stash-capacity = 10
と、それが受信するすべてのメッセージをスタッシュシンプルな俳優を実装している。そのために、以下のように、私はいくつかの上限にstash-capacity
を設定しています。スタッシュが成功したかされていない場合、俳優はその後、送信者を知らせる:
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
クール問題。ありがとう:) – user2721628