仅允许改编的无类型ActorRefs

时间:2019-06-30 20:22:31

标签: scala akka akka-typed

我正尝试使用Actor discovery测试演员,如下所示:

class DetectorSpec extends BddSpec {


  private val sap = new SapMock()
    .withExposedPorts(8080)
    .waitingFor(Wait.forHttp("/"))

  private val kafka = new KafkaContainer("5.2.1")


  sap.start()
  kafka.start()


  override def afterAll(): Unit = {
    sap.stop()
    kafka.stop()
  }

  private def withKafkaAndSapOnline(testCode: TestInbox[ServerEvent] => Future[Assertion])
  : Future[Assertion] = {

    val config = ConfigFactory.parseString(
      s"""
         kafka {
           servers = "${kafka.getBootstrapServers}"
         }
         sap {
           server = "ws://${sap.getContainerIpAddress}:${sap.getMappedPort(8080)}"
         }""")

    val system = ActorSystem(DetectorSupervisor.create(), "testSystem1", config)
    val inbox = TestInbox[ServerEvent]()
    system.receptionist ! Receptionist.Register(ServerStateKey, inbox.ref)

    complete {
      testCode(inbox)
    } lastly {
      system.terminate()
    }

  }


  feature("Detect Kafka and SAP availability") {
    info("As a technical user, I want to be notified in real time, if Kafka and SAP is up and running or not.")
    scenario("SAP and Kafka are available") {
      withKafkaAndSapOnline { inbox =>
        Given("I am waiting for the current state message")
        When("I am receive the state message")
        Then("it should contain `SAP and Kafka are online`")
        Future {
          inbox.receiveMessage() should be(ServerOnlineApproved)
        }
      }
    }
  }
}

开始测试时,出现以下错误消息:

[ERROR] [06/30/2019 22:15:07.643] [testSystem3-akka.actor.default-dispatcher-3] [akka://testSystem3/system/receptionist] only adapted untyped ActorRefs permissible (Actor[akka.actor.typed.inbox://anonymous/inbox#-233829306] of class akka.actor.testkit.typed.internal.FunctionRef)
akka.actor.ActorInitializationException: akka://testSystem3/system/receptionist/$a: exception during creation
    at akka.actor.ActorInitializationException$.apply(Actor.scala:202)
    at akka.actor.ActorCell.create(ActorCell.scala:696)
    at akka.actor.ActorCell.invokeAll$1(ActorCell.scala:547)
    at akka.actor.ActorCell.systemInvoke(ActorCell.scala:569)
    at akka.dispatch.Mailbox.processAllSystemMessages(Mailbox.scala:293)
    at akka.dispatch.Mailbox.run(Mailbox.scala:228)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:241)
    at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.UnsupportedOperationException: only adapted untyped ActorRefs permissible (Actor[akka.actor.typed.inbox://anonymous/inbox#-233829306] of class akka.actor.testkit.typed.internal.FunctionRef)
    at akka.actor.typed.internal.adapter.ActorRefAdapter$.toUntyped(ActorRefAdapter.scala:55)
    at akka.actor.typed.internal.adapter.ActorContextAdapter.watch(ActorContextAdapter.scala:99)
    at akka.actor.typed.internal.receptionist.LocalReceptionist$.$anonfun$behavior$2(LocalReceptionist.scala:64)
    at akka.actor.typed.Behavior$DeferredBehavior$$anon$1.apply(Behavior.scala:264)
    at akka.actor.typed.Behavior$.start(Behavior.scala:331)
    at akka.actor.typed.internal.adapter.ActorAdapter.preStart(ActorAdapter.scala:238)
    at akka.actor.Actor.aroundPreStart(Actor.scala:550)
    at akka.actor.Actor.aroundPreStart$(Actor.scala:550)
    at akka.actor.typed.internal.adapter.ActorAdapter.aroundPreStart(ActorAdapter.scala:51)
    at akka.actor.ActorCell.create(ActorCell.scala:676)
    ... 9 more

问题是肯定的:

system.receptionist ! Receptionist.Register(ServerStateKey, inbox.ref)

但是我在做什么错了?

1 个答案:

答案 0 :(得分:1)

Inbox不是真正的演员(使用FunctionRef实现),无法注册到接待员。在测试中,请使用TestProbe[T]。这是可行的,因为这些人得到了真正演员的支持。