Skip to content

Instantly share code, notes, and snippets.

@m0essy
Created February 13, 2018 16:55
Show Gist options
  • Save m0essy/4346e710924ad5d67c104acec7ecde90 to your computer and use it in GitHub Desktop.
Save m0essy/4346e710924ad5d67c104acec7ecde90 to your computer and use it in GitHub Desktop.
Akka Actor using faulty pipeTo
package com.snekyx.strange.actors
import akka.actor.{Actor, Props}
import akka.pattern.pipe
import com.bsh.hca.account.view.FaultyPipeToActor.SomeMsg
import scala.concurrent.Future
object FaultyPipeToActor {
def props: Props = Props(new FaultyPipeToActor())
case class SomeMsg(payload: Int)
}
class FaultyPipeToActor extends Actor {
import scala.concurrent.ExecutionContext.Implicits.global
override def receive: Receive = {
case msg: SomeMsg =>
if(msg.payload == 0) {
Future { msg.payload + 1 }
} else {
Future { msg.payload }
} pipeTo sender()
}
}
package com.snekyx.strange.actors
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
import com.bsh.hca.account.view.FaultyPipeToActor.IncreaseBy1
import org.specs2.mutable.SpecificationLike
import scala.concurrent.Await
import scala.concurrent.duration._
class FaultyPipeToActorTest extends TestKit(ActorSystem()) with SpecificationLike with ImplicitSender {
val actorUnderTest = system.actorOf(FaultyPipeToActor.props)
implicit val timeout = Timeout(2 seconds)
"FaultyPipeToActor" should {
"increase the payload by 1" in {
val result = Await.result(actorUnderTest ? IncreaseBy1(1), 2 seconds)
result === 2
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment