Skip to content

Instantly share code, notes, and snippets.

@lregnier
Created May 11, 2018 18:23
Show Gist options
  • Save lregnier/6ccbb8fef251c21aff63bf833bc0bdab to your computer and use it in GitHub Desktop.
Save lregnier/6ccbb8fef251c21aff63bf833bc0bdab to your computer and use it in GitHub Desktop.
SynchronousVsAsynchronousSpec
import akka.actor.{Actor, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestActorRef, TestKit}
import org.scalatest.WordSpecLike
import scala.concurrent.duration._
class SynchronousVsAsynchronousSpec
extends TestKit(ActorSystem("synchronous-vs-asynchronous-spec"))
with ImplicitSender
with WordSpecLike {
val maxTimeOut = 3 seconds // If not provided, 3 seconds is the default timeout value
trait SynchronousScope {
val echoActor = TestActorRef(EchoActor.props())
}
trait AsynchronousScope {
val echoActor = system.actorOf(EchoActor.props())
}
"When a receiving a msg, the EchoActor" should {
"respond back with the same msg in synchronous scope" in new SynchronousScope {
val msg = "some-msg"
echoActor ! msg
expectMsg(maxTimeOut, msg)
}
"respond back with the same msg in asynchronous scope" in new AsynchronousScope {
val msg = "some-msg"
echoActor ! msg
expectMsg(maxTimeOut, msg)
}
}
}
object EchoActor {
def props(): Props = Props(new EchoActor())
}
class EchoActor extends Actor {
override def receive: Receive = {
case msg =>
Thread.sleep(5000) // Simulates test latency
sender ! msg
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment