2016-12-27 2 views
0

は、私は、その責任それが話題を適切な外部インタフェース(コマンドライン、利用者など)から受信したメッセージを転送することである俳優を持っています。私は、これらのメッセージを正しく公開しているかどうかテストしたい。は、どのように私はアッカクラスタ内DistributedPubSubに出版社をテストすることができますか?

私は、特定のトピックに発行されたメッセージを期待し、それが受信したメッセージに関するアサーションを行いますいくつかのダミー加入者を作成する必要があります。ここで

は、私はそれを実現しようとした私のコードです:

Messages.scala

case class Foo(foo: String) 

InterfaceForwardingActor.scala

import akka.actor.{Actor, ActorLogging} 
import akka.cluster.pubsub.{DistributedPubSub, DistributedPubSubMediator} 
import com.typesafe.config.Config 

/** Actor responsible for forwarding stimuli external to the system. 
    * For instance, messages from the command-line interface or from a UI. 
    * 
    */ 
class InterfaceForwardingActor extends Actor with ActorLogging { 
    import DistributedPubSubMediator.Publish 

    protected val mediator = DistributedPubSub(context.system).mediator 

    log.info(s"Hello from interface forwarder.") 

    final val topic = "info" 

    def receive = { 
    case foo: Foo => { 
     log.info("Forwarding a Foo message") 
     mediator ! Publish(topic, foo) 
    } 
    } 
} 

とテストコード

InterfaceForwardingActorTest.scala

import akka.actor.{ActorSystem, Props} 
import akka.cluster.client.ClusterClient.Publish 
import akka.cluster.pubsub.DistributedPubSub 
import akka.testkit.{ImplicitSender, TestKit, TestProbe} 
import com.typesafe.config.ConfigFactory 
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike} 

class InterfaceForwardingActorTest extends 
    TestKit(ActorSystem("InterfaceForwardingActorSpec")) with ImplicitSender with 
    WordSpecLike with Matchers with BeforeAndAfterAll { 

    override def afterAll { 
    TestKit.shutdownActorSystem(system) 
    } 

    "An InterfaceForwardingActor" must { 
    val interfaceForwardingActor = system.actorOf(Props[InterfaceForwardingActor]) 

    val probe = TestProbe() 
    val mediator = DistributedPubSub(system).mediator 

    // subscribe the test probe to the "info" topic 
    mediator ! Publish("info", probe.ref) 

    "publish a Foo message" in { 
     val msg = Foo("test") 
     interfaceForwardingActor ! msg 
     probe.expectMsg(msg) 
    } 
    } 
} 

私は何を見つけることinfoトピックを購読しているprobeは、デフォルトのタイムアウト期間内にメッセージを受信しないということです3秒間アサーションが失敗します。興味深い部分は、しかし、私はインターフェイスの転送俳優が実際のFooメッセージを転送していることを示すログメッセージが表示されないということです。

私のテストで何が間違っていますか?

+0

答えを見た後、私は単純なタイプミスであったので閉じようとしています...「公開」は「購読」だったはずです。不注意な間違い。 :) – erip

答えて

1

TestProbeがテストコードのトピックにサブスクライブする必要があります

mediator ! Subscribe("info", probe.ref)

代わりに分散パブ・サブ

mediator ! Publish("info", probe.ref)

マニュアルページ参照のため、hereあります。

+0

ああ、うわー... :(それはばかげている。 – erip

関連する問題