Testing the effect of system.scheduler.scheduleOnce

466 views
Skip to first unread message

Elisabeth Anderson

unread,
Sep 9, 2015, 12:13:51 PM9/9/15
to Akka User List
I'm not sure if I'm approaching this in the correct way, but...

I have a client which, when called, will create a schedule, such as:

def schedule(item: Map[String, String], delay: FiniteDuration): Cancellable = {
  val job = ScheduledJob(item, delay)

  system.scheduler.scheduleOnce(delay) {
    jobRunnerActor ! job
  }
}

I'd like to write a test that checks that if this function is called that system.scheduler.scheduleOnce is called with the correct delay.

I've written the following test:

"Orchestration" should {
  "schedule a job once using the actor system" in new Actors {

    val item = Map(
      "type" -> "foo",
      "id" -> "bar"
    )

    val delay = FiniteDuration(1, "second")

    // DI the mock ActorSystem
    object MockOrchestrator extends Orchestration {
      val system = mock[ActorSystem]
    }

    // stubs
    val result: Cancellable = mock[Cancellable]
    MockOrchestrator.schedule(any[Map[String, String]], any[FiniteDuration]) returns result

    // run the scheduler
    MockOrchestrator.schedule(item, delay)

    val jobRunnerActor = system.actorOf(Props[JobRunnerActor])

    val job = {
      jobRunnerActor ! ScheduledJob(item, delay)
    }

    // system.scheduler.scheduleOnce should be run with the correct delay
    there was one(mockSystem.scheduler).scheduleOnce(delay)(job)
  }

With this test I'm setting up a MockOrchestrator as the 'system' is being passed to this using dependency injection (so I can pass in a mock actor system). Then I'm setting up a stub of the schedule function, running it, then ensuring that the scheduler was called with the delay and the function to run.

However, this isn't running. I think because of the (job) function I'm passing to the check for 'there was one'

Like I say, I'm not sure if this is the correct approach. I don't want to test the Akka scheduler, just that my code creates a proper scheduled job. How should I approach this?

Thanks,

Beth

Akka Team

unread,
Sep 10, 2015, 5:33:28 AM9/10/15
to Akka User List
Hi Elisabeth,

system.scheduler implements the interface Scheduler. You can stub that interface without mocking an ActorSystem. That will be enough to test that a proper scheduled job was created.

-Endre

--
>>>>>>>>>> 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.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Akka Team
Typesafe - Reactive apps on the JVM
Blog: letitcrash.com
Twitter: @akkateam

Viktor Klang

unread,
Sep 10, 2015, 5:36:28 AM9/10/15
to Akka User List
I'd even say:

"I'd like to write a test that checks that if this function is called that system.scheduler.scheduleOnce is called with the correct delay."

This is testing Akka, and is already tested within Akka. :)
Cheers,

Elisabeth Anderson

unread,
Sep 10, 2015, 6:12:42 AM9/10/15
to Akka User List


On Thursday, September 10, 2015 at 10:36:28 AM UTC+1, √ wrote:
This is testing Akka, and is already tested within Akka. :)

You misunderstand. I don't want to call system.schedule.scheduleOnce. I want to call my code which in it's implementation calls that function so my test is that my code calls system.schedule.scheduleOnce correctly.  

Elisabeth Anderson

unread,
Sep 10, 2015, 6:14:09 AM9/10/15
to Akka User List


On Thursday, September 10, 2015 at 10:33:28 AM UTC+1, Akka Team wrote:
system.scheduler implements the interface Scheduler. You can stub that interface without mocking an ActorSystem. That will be enough to test that a proper scheduled job was created.

I did try that, and can mock the Scheduler and inject it into my implementation fine, it's just how to assert that the scheduleOnce function was indeed called. My code (above) doesn't work.

Viktor Klang

unread,
Sep 10, 2015, 6:19:40 AM9/10/15
to Akka User List
Ah, sorry then I misunderstood.

If the effect you want executed in scheduleOnce is not observable then you cannot meaningfully test it.
Where can you observe the effect, and can you put the test in relation to that?

--
>>>>>>>>>> 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.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Christian Kitzmueller

unread,
Sep 11, 2015, 4:44:21 AM9/11/15
to Akka User List
Hi,

Can't you replace your jobRunnerActor in your test with a TestProbe and do expectMsg with the correct timeout?
Reply all
Reply to author
Forward
0 new messages