2011-08-07 22 views
1

我正在測試我正在處理的新0123'如何處理意外消息。在這些情況下,我想斷言它會拋出GibberishException。這裏的測試也是迄今爲止實現:如何從Akka主管獲取異常詳情?

測試:

"""throw a GibberishException for unrecognized messages""" in { 
    //define a service that creates gibberish-speaking repositories 
    val stubs = new svcStub(
    actorOf(new Actor{ 
     def receive = { case _ => { 
      self.channel ! "you're savage with the cabbage" 
     } 
     } 
    }) 
) 
    val model = actorOf(new HomeModel(stubs.svc,stubs.store)) 
    val supervisor = Supervisor(
    SupervisorConfig(
     OneForOneStrategy(List(classOf[Exception]), 3, 1000), 
     Supervise(model,Permanent) :: Nil 
    ) 
) 
    try{ 
    intercept[GibberishException] { 
     supervisor.start 
     model !! "plan" 
    } 
    } finally { 
    supervisor.shutdown 
    } 
    stubs.store.plan should equal (null) 
    stubs.svcIsOpen should be (false) 
} 

實現:

class HomeModel(service: PlanService, store: HomeStore) 
extends Actor { 
    private val loaderRepo = service.getRepo() 
    private var view: Channel[Any] = null 

    override def postStop() = { 
    service.close() 
    } 

    def receive = { 
    case "plan" => { 
     view=self.channel 
     loaderRepo ! LoadRequest() 
    } 
    case p: Plan => { 
     store.plan=p 
     view ! store.plan 
    } 
    case _ => throw new GibberishException(_) 
    } 
} 

然而,當我運行測試,異常的詳細信息到我建立了Supervisor,但我不知道如何對他們做任何事情(如記錄他們或測試他們的類型)。我希望能夠從主管那裏獲得例外細節,這樣我就可以在我的測試中重新拋出並攔截它們。除了測試方法之外,如果您想在正在運行的應用程序的UI中報告異常的性質,我可以想象這很有用。發生這種情況時,是否有辦法從Supervisor中獲取?

回答

3

更改OneForOneStrategy只處理GibberishException,應該解決它。

+0

但是,如何登錄或以其他方式處理Supervisor內的例外情況?我沒有看到在哪裏添加錯誤處理代碼。我知道它會重新啓動演員,但如果我想做更多的事情,比如記錄異常或向用戶顯示消息,該怎麼辦? – traffichazard

+0

Akka已記錄異常 如果您想發送消息給用戶,請使用!!!代替 !你將在未來有例外。 –

+0

更改策略以僅處理GibberishException並更換!!在測試的攔截塊中用!!!隨後在未來獲得「獲得」,它返回給我同樣的結果。 'GibberishException'未被捕獲,轉儲到我的命令行,並且ScalaTest未能通過'Expected exception org.thimblus.model.GibberishException'拋出異常,但引發了akka.dispatch.FutureTimeoutException。'看起來像異常是被扔進與測試等待的線程不同的線程中。 – traffichazard