Created
May 11, 2018 18:23
-
-
Save lregnier/6ccbb8fef251c21aff63bf833bc0bdab to your computer and use it in GitHub Desktop.
SynchronousVsAsynchronousSpec
This file contains 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.{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