akka:如何测试演员被阻止 [英] akka: how to test that an actor was stopped

查看:87
本文介绍了akka:如何测试演员被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道测试演员是否已在阿卡(Akka)停留的规范方法是什么。这是我目前工作方式的一个例子。

I am wondering what the canonical way is of testing whether an actor has stopped in akka. Here is an example of how I am currently doing; I'm worried I'm over complicating it.

import akka.actor.{Terminated, Actor, Props, ActorSystem}
import akka.testkit.TestProbe

class MyActor extends Actor {
  import MyActor._
  override def receive: Receive = {
    case Stop => context.stop(self)
  }
}

object MyActor {
  def props = Props(new MyActor)
  case object Stop
}


object MyActorSpec {

  val system = ActorSystem()
  val myActor = system.actorOf(MyActor.props)
  val testProbe = TestProbe()

  case object MyActorStopped

  val watcher = system.actorOf(Props(new Actor {
    context.watch(myActor)
    override def receive: Actor.Receive = {
      case Terminated(`myActor`) => testProbe.ref ! MyActorStopped
    }
  }))

  myActor ! MyActor.Stop
  testProbe.expectMsg(MyActorStopped)
}


推荐答案

您可以摆脱单独的观察者演员,而直接从 testProbe 演员观看目标演员:

You can get rid of the separate watcher actor, and just watch the target actor directly from the testProbe actor:

val testProbe = TestProbe()
testProbe watch myActor
myActor ! MyActor.Stop
testProbe.expectTerminated(myActor)

请参见此处以获取Akka测试文档的相关部分。

See here for the relevant section of the Akka testing docs.

这篇关于akka:如何测试演员被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆