Akka 阿克卡的储藏容量应如何限定?

Akka 阿克卡的储藏容量应如何限定?,akka,Akka,我正在尝试验证何时将stashforflowexception抛出到带有Stash的参与者中。为此,我将存储容量设置为某个上限,如下所示: akka.actor.deployment.default-mailbox.stash-capacity = 10 并实现了一个简单的Actor,它隐藏了它接收到的所有消息。然后,如果隐藏成功与否,参与者向发送方发出信号: import akka.actor.{ Actor, ActorSystem, Props, Stash, StashOverflow

我正在尝试验证何时将
stashforflowexception
抛出到带有Stash的
参与者中。为此,我将
存储容量设置为某个上限,如下所示:

akka.actor.deployment.default-mailbox.stash-capacity = 10
并实现了一个简单的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
      }
  }
}
问题是,无论向该参与者发送多少条消息,都不会抛出
stasForFlowException

下面是一个简单的测试,试图验证这一点:

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

在配置键中删除
部署

akka.actor.default-mailbox.stash-capacity = 10

在配置键中删除
部署

akka.actor.default-mailbox.stash-capacity = 10

酷,这解决了问题。谢谢:)酷,这解决了问题。谢谢:)