Instantly share code, notes, and snippets.

Embed
What would you like to do?
ReTry-test.scala
package com.codetinkerhack
import scala.collection.immutable.IndexedSeq
import scala.concurrent.blocking
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.FiniteDuration
import scala.util.Failure
import scala.util.Success
import scala.util.Try
import org.scalatest.BeforeAndAfterAll
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.ActorLogging
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.actorRef2Scala
import akka.testkit.ImplicitSender
import akka.testkit.TestKit
import akka.testkit.TestProbe
object MockWorker {
def props(): Props = Props(new MockWorker())
def taskProps(): Props = Props(new TaskRunner())
}
case class Task(i: FiniteDuration, aRef: ActorRef)
case class Work(delays: IndexedSeq[FiniteDuration])
class TaskRunner extends Actor with ActorLogging {
def receive = {
case Task(i, aRef) =>
blocking {
Thread.sleep(i.toMillis)
aRef ! Success("42") // the ultimate answer...
// log.info("Task executed for: " + i)
}
}
}
class MockWorker extends Actor {
var i = 0
def receive = {
case Work(delays) =>
if(delays.size > i) {
context.actorOf(MockWorker.taskProps()) ! Task(delays(i), sender)
i+=1
}
}
}
class RetryTest extends TestKit(ActorSystem("RetryTestSpec"))
with FunSuite
with BeforeAndAfterAll
with ShouldMatchers
with ImplicitSender {
override def afterAll(): Unit = {
system.shutdown()
}
test("case1: Testing mock worker success response") {
val client = TestProbe()
val mockWorker = system.actorOf(MockWorker.props(), "case1-genericMockWorkerTest")
val expectedResult = Success("42") // the ultimate answer...
client.send(mockWorker, Work(IndexedSeq(1.second)))
client.expectMsg(2.second, expectedResult)
}
test("case2: Testing mock worker with Retry proxy - Simple Success reply") {
val client = TestProbe()
val mockWorker = system.actorOf(MockWorker.props(), "case2-withRetryProxy")
val mockWorkerWrappedInProxy = system.actorOf(ReTry.props(tries = 1, retryTimeOut = 1000.millis, retryInterval = 100.millis, mockWorker))
val success = Success("42")
client.send(mockWorkerWrappedInProxy, Work(IndexedSeq(200.millis)))
client.expectMsg(2.seconds, success)
}
test("case3: Testing mock worker with Retry proxy - 1 try timeout - retries exceeded") {
val client = TestProbe()
val mockWorker = system.actorOf(MockWorker.props(), "case3-withRetryProxy")
val mockWorkerWrappedInProxy = system.actorOf(ReTry.props(tries = 1, retryTimeOut = 1000.millis, retryInterval = 100.millis, mockWorker))
client.send(mockWorkerWrappedInProxy, Work(IndexedSeq(1100.millis)))
client.expectMsgPF(2.seconds) { case Failure(e: Exception) if e.getMessage() == "Retries exceeded" => () }
}
test("case4: Testing mock worker with Retry proxy - 4 tries and all time out - retries exceeded") {
val client = TestProbe()
val mockWorker = system.actorOf(MockWorker.props(), "case4-withRetryProxy")
val mockWorkerWrappedInProxy = system.actorOf(ReTry.props(tries = 4, retryTimeOut = 1000.millis, retryInterval = 100.millis, mockWorker))
client.send(mockWorkerWrappedInProxy, Work(IndexedSeq(1100.millis, 1100.millis, 1100.millis, 1100.millis)))
client.expectMsgPF(5.seconds) { case Failure(e: Exception) => () }
}
test("case5: Testing mock worker with Retry proxy - 4 tries last successful - success") {
val client = TestProbe()
val mockWorker = system.actorOf(MockWorker.props(), "case5-withRetryProxy")
val mockWorkerWrappedInProxy = system.actorOf(ReTry.props(tries = 4, retryTimeOut = 1000.millis, retryInterval = 100.millis, mockWorker))
val success = Success("42")
client.send(mockWorkerWrappedInProxy, Work(IndexedSeq(1101.millis, 1102.millis, 1103.millis, 104.millis)))
client.expectMsg(5.seconds, success)
}
test("case6: Testing mock worker with Retry proxy - 4 tries first successful - success") {
val client = TestProbe()
val mockWorker = system.actorOf(MockWorker.props(), "case6-withRetryProxy")
val mockWorkerWrappedInProxy = system.actorOf(ReTry.props(tries = 4, retryTimeOut = 1000.millis, retryInterval = 100.millis, mockWorker))
val success = Success("42")
client.send(mockWorkerWrappedInProxy, Work(IndexedSeq(101.millis, 1102.millis, 1103.millis, 104.millis)))
client.expectMsg(5.seconds, success)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment