Thanks for your reply Patrik. I cannot believe I missed that. I will 
experiment with it also.

On Friday, 17 November 2017 19:32:01 UTC, Patrik Nordwall wrote:
>
> Hi Iain,
>
> Many things are missing for testing of Akka Typed, but did you check out: 
> https://github.com/akka/akka/blob/master/akka-typed-testkit/src/main/scala/akka/typed/testkit/scaladsl/TestProbe.scala
>
> /Patrik
>
> fre 17 nov. 2017 kl. 16:28 skrev Iain Hull <iain...@gmail.com 
> <javascript:>>:
>
>> Hi,
>>
>> I am been playing with akka typed and really missed TestProbe. I am not 
>> sure if this is missing, because its a work in progress or if there are 
>> better ways to test akka typed without using TestProbe.
>>
>> In the meantime for my own needs I have created a simple asynchronous 
>> test probe for akka typed. It is designed to work with scalatest async so 
>> is completely future based. I have found it does not need all the expect 
>> style methods on the regular TestProbe since it uses futures and 
>> asynchronous testing to can just use the scalatest matchers.
>>
>> The full code is on github 
>> https://github.com/IainHull/akka-typed-testprobe
>> A deeper discussion is in this blog post 
>> http://iainhull.github.io/2017/11/17/akka-typed-test-probes/
>>
>> I would love to know if this test probe is useful or going in the 
>> completely wrong direction for akka typed. 
>>
>> Thanks,
>> Iain.
>>
>> /**
>>   * An actor test probe, that returns its results asynchronously. Each 
>> message the actor receives completes a promise
>>   *
>>   * @tparam A
>>   */
>> class AsyncTestProbe[A] {
>>   /**
>>     * Queue of promises that are completed when a message is received.
>>     * New promises are added to queueIn and queueOut at the same time
>>     */
>>   private val queueIn = mutable.Queue[Promise[A]]()
>>
>>   /**
>>     * Queue of promises that are returned by `nextMessage`.
>>     */
>>   private val queueOut = mutable.Queue[Promise[A]]()
>>
>>   /**
>>     * Read the next promise from the queue or create a new promise in 
>> both the in and out queues.
>>     *
>>     * NOTE: This method is synchronized as the requests for the queueOut 
>> come from the callers thread,
>>     * while the requests for the queueIn come from the actor's thread.
>>     *
>>     * @param queue The queue to read from
>>     *
>>     * @return the next promise.
>>     */
>>   private def nextPromiseFrom(queue: mutable.Queue[Promise[A]]): 
>> Promise[A] = this.synchronized {
>>     if (queue.isEmpty) {
>>       val promise = Promise[A]()
>>       queueIn.enqueue(promise)
>>       queueOut.enqueue(promise)
>>     }
>>     queue.dequeue()
>>   }
>>
>>   /**
>>     * @return a future for the next message this test probe will receive
>>     */
>>   def nextMessage(): Future[A] = nextPromiseFrom(queueOut).future
>>
>>   /**
>>     * @return a future for the next n messages this probe will receive
>>     */
>>   def nextMessages(n: Int)(implicit executor: ExecutionContext): 
>> Future[Seq[A]] = this.synchronized {
>>     require(n >= 1, s"Can only ask for one or more messages, not $n")
>>
>>     val futures =  (0 to n) map { _ => nextPromiseFrom(queueOut).future }
>>     Future.sequence(futures)
>>   }
>>
>>   private val mutableBehavior = new Actor.MutableBehavior[A] {
>>     override def onMessage(msg: A) = {
>>       nextPromiseFrom(queueIn).success(msg)
>>       this
>>     }
>>   }
>>
>>   /**
>>     * @return the behavior of this test probe
>>     */
>>   def behavior: Behavior[A] = Actor.mutable { _ =>
>>     mutableBehavior
>>   }
>> }
>>
>> class QueueActorSpec extends fixture.AsyncFlatSpec with CompleteLastly 
>> with Matchers {
>>
>>   import QueueActor._
>>
>>
>>   // A fixture contains the actor as an ActorSystem and a single test 
>> probe
>>   case class Fixture(actor: ActorSystem[JobRequest], probe: 
>> AsyncTestProbe[JobRequest])
>>   type FixtureParam = Fixture
>>
>>   // Construct the ActorSystem run the test lastly terminate the 
>> ActorSystem
>>   override def withFixture(test: OneArgAsyncTest): FutureOutcome = {
>>     val testProbe = new AsyncTestProbe[JobRequest]
>>
>>     val actorSystem = ActorSystem(QueueActor(Map(), 0, _ => 
>> testProbe.behavior), "QueueActor")
>>
>>     complete {
>>       withFixture(test.toNoArgAsyncTest(Fixture(actorSystem, testProbe)))
>>     } lastly {
>>       Await.result(actorSystem.terminate(), 2.second)
>>       println("Terminated")
>>     }
>>   }
>>
>>   behavior of "QueueActor"
>>
>>   it should "create child actors on demand" in { param =>
>>     val Fixture(actor, testProbe) = param
>>     implicit val timeout = Timeout(2.seconds)
>>     implicit val sched = actor.scheduler
>>     val tenant = "sometenant"
>>     val job = Job("12345678", tenant)
>>
>>     // Now test
>>
>>     val futureMessage = testProbe.nextMessage()  // 1
>>     val futureResponse: Future[EnqueueJobResponse] = actor ? 
>> (EnqueueJob(job, _)) // 2
>>     for {
>>       EnqueueJob(job, sender) <- futureMessage // 3
>>       _ = sender ! JobEnqueued(job) // 4
>>       JobEnqueued(j) <- futureResponse // 5
>>     } yield {
>>       j shouldBe job // 6
>>     }
>>   }
>> }
>>
>> -- 
>> >>>>>>>>>> Read the docs: http://akka.io/docs/
>> >>>>>>>>>> Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>> >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Akka User List" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to akka-user+...@googlegroups.com <javascript:>.
>> To post to this group, send email to akka...@googlegroups.com 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/akka-user.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to