How to check subscriber acceptance method in Akka Cluster?

I have the following Subscriber base class:

 abstract class Subscriber(topics: Seq[String]) extends Actor with ActorLogging { import DistributedPubSubMediator.{ Subscribe, SubscribeAck } val mediator = DistributedPubSub(context.system).mediator // subscribe to each topic topics.foreach{mediator ! Subscribe(_, self)} def receive = { case SubscribeAck(Subscribe(name, None, `self`)) ⇒ log.info(s"Subscribing to $name") } } 

And I would like to check that he receives messages published on topics for which the subscribed subclass is subscribed. Simple pseudocode demonstrating the following:

 val topic = "foo" class FooSubscriber extends Subscriber(Seq(topic)) val fooSubActor = system.actorOf(Props[FooSubscriber]) val mediator = DistributedPubSub(system).mediator val msg = "This is a string" // Publish the msg to the "foo" topic. mediator ! Publish(topic, msg) fooSubActor.expectMsg(msg) 

The only way I know to make statements about messages that certain subjects receive is TestProbe , but I don’t know how I could make TestProbe extension of my class.

Usually Akka docs has a lot of code examples with corresponding test suites, but I could not find anything in the Akka docs cluster related to testing the receive method.

Does anyone have any suggestions?

+5
source share
1 answer

This is an example tutorial where Dependency Injection helps with testing.

If you get an intermediary for DistPubSub in the subscriber’s constructor, and not just request it, you can simply test the Suscriber actor separately, without having to use DistPubSub as part of your test fixture.

0
source

Source: https://habr.com/ru/post/1262146/


All Articles