Created
July 10, 2020 04:45
-
-
Save TJC/b64ed88634d7e6474fa14436cff0b1a1 to your computer and use it in GitHub Desktop.
How to test Akka Actor has stopped, when using scheduler?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit | |
import akka.actor.typed.Behavior | |
import akka.actor.typed.scaladsl.{Behaviors, TimerScheduler} | |
import scala.concurrent.duration._ | |
class ButterflyActor( | |
timers: TimerScheduler[TriggerMessage], | |
) { | |
def initialState() = { | |
scheduleTrigger(10.seconds) | |
Behaviors.receiveMessage[TriggerMessage] { _ => | |
Behaviors.stopped | |
} | |
} | |
private def scheduleTrigger(delay: Long): Unit = { | |
val delayDuration = FiniteDuration(delay, TimeUnit.MILLISECONDS) | |
timers.startSingleTimer(TriggerMessage(), delayDuration) | |
} | |
} | |
case class TriggerMessage() | |
object ButterflyActor { | |
def apply = { | |
Behaviors.setup { context => | |
Behaviors.withTimers { timers => | |
new ButterflyActor(timers).initialState() | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.testkit.typed.scaladsl.{ManualTime, ScalaTestWithActorTestKit} | |
import org.scalatest.funspec.AnyFunSpecLike | |
import org.scalatest.matchers.should.Matchers | |
import org.scalamock.scalatest.MockFactory | |
import scala.concurrent.duration._ | |
class DelayedMessagingActorSpec | |
extends ScalaTestWithActorTestKit(ManualTime.config) | |
with AnyFunSpecLike | |
with Matchers | |
{ | |
val manualTime: ManualTime = ManualTime() | |
it("stops after 10 seconds") { | |
val actor = testKit.spawn(ButterflyActor()) | |
manualTime.timePasses(10.seconds) | |
// TODO: How do we check it stopped? | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer: Use TestProbes