Created
May 27, 2019 01:15
-
-
Save winguse/ac212bd2d24ed81fe37a70c83d8ff234 to your computer and use it in GitHub Desktop.
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.{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