2017-10-06 20 views
1

我試圖驗證StashOverflowException何時投入Actor with Stash。爲此,我已經設置了stash-capacity一些上限如下:Akka應該如何限制隱藏能力?

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

,並實現了一個簡單的演員是儲物箱,它收到的所有郵件。該演員然後通知發件人,如果積攢的成功與否:

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 

回答

0

在config鍵刪除deployment

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

酷,即固定問題。謝謝 :) – user2721628