Skip to content

Instantly share code, notes, and snippets.

@winguse
Created May 27, 2019 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save winguse/ac212bd2d24ed81fe37a70c83d8ff234 to your computer and use it in GitHub Desktop.
Save winguse/ac212bd2d24ed81fe37a70c83d8ff234 to your computer and use it in GitHub Desktop.
import java.util.{Timer, TimerTask}
import akka.actor.{Actor, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import scala.concurrent.{Future, Promise}
import scala.util.Try
class SomeActorTest()
extends TestKit(ActorSystem("MySpec"))
with ImplicitSender
with WordSpecLike
with Matchers
with BeforeAndAfterAll {
override def afterAll: Unit = {
TestKit.shutdownActorSystem(system)
}
"An actor" must {
"begin process another message before previous future complete" in {
class SomeActor extends Actor {
import context.dispatcher
def delay(delay: Long): Future[Long] = {
val promise = Promise[Long]()
val t = new Timer()
t.schedule(new TimerTask {
override def run(): Unit = {
promise.complete(Try(delay))
}
}, delay)
promise.future
}
override def receive: Receive = {
case "first" =>
val s = sender()
delay(1000).map { _ =>
s ! "first"
}
case "second" =>
val s = sender()
delay(500).map { _ =>
s ! "second"
}
}
}
val a = system.actorOf(Props(new SomeActor()))
a ! "first"
a ! "second"
expectMsg("second")
expectMsg("first")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment