alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (ReliableProxyDocSpec.scala)

This example Akka source code file (ReliableProxyDocSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

a, actor, actorpath, akka, akkaspec, concurrent, none, proxyparent, reliableproxydocspec, some, test, testing, testkit, testprobe, time, watchingproxyparent

The ReliableProxyDocSpec.scala Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */

package akka.contrib.pattern

import akka.testkit.AkkaSpec
import akka.actor._
import akka.testkit.ImplicitSender
import scala.concurrent.duration._
import akka.testkit.TestProbe

object ReliableProxyDocSpec {

  //#demo
  import akka.contrib.pattern.ReliableProxy

  class ProxyParent(targetPath: ActorPath) extends Actor {
    val proxy = context.actorOf(ReliableProxy.props(targetPath, 100.millis))

    def receive = {
      case "hello" ⇒ proxy ! "world!"
    }
  }
  //#demo

  //#demo-transition
  class ProxyTransitionParent(targetPath: ActorPath) extends Actor {
    val proxy = context.actorOf(ReliableProxy.props(targetPath, 100.millis))
    proxy ! FSM.SubscribeTransitionCallBack(self)

    var client: ActorRef = _

    def receive = {
      case "go" ⇒
        proxy ! 42
        client = sender()
      case FSM.CurrentState(`proxy`, initial) ⇒
      case FSM.Transition(`proxy`, from, to) ⇒
        if (to == ReliableProxy.Idle)
          client ! "done"
    }
  }
  //#demo-transition

  class WatchingProxyParent(targetPath: ActorPath) extends Actor {
    val proxy = context.watch(context.actorOf(
      ReliableProxy.props(targetPath, 100.millis, reconnectAfter = 500.millis, maxReconnects = 3)))

    var client: Option[ActorRef] = None

    def receive = {
      case "hello" ⇒
        proxy ! "world!"
        client = Some(sender())
      case Terminated(`proxy`) ⇒
        client foreach { _ ! "terminated" }
    }
  }
}

class ReliableProxyDocSpec extends AkkaSpec {

  import ReliableProxyDocSpec._

  "A ReliableProxy" must {

    "show usage" in {
      val probe = TestProbe()
      val a = system.actorOf(Props(classOf[ProxyParent], probe.ref.path))
      a.tell("hello", probe.ref)
      probe.expectMsg("world!")
    }

    "show state transitions" in {
      val target = TestProbe().ref
      val probe = TestProbe()
      val a = system.actorOf(Props(classOf[ProxyTransitionParent], target.path))
      a.tell("go", probe.ref)
      probe.expectMsg("done")
    }

    "show terminated after maxReconnects" in {
      val target = system.deadLetters
      val probe = TestProbe()
      val a = system.actorOf(Props(classOf[WatchingProxyParent], target.path))
      a.tell("hello", probe.ref)
      probe.expectMsg("terminated")
    }

  }

}

Other Akka source code examples

Here is a short list of links related to this Akka ReliableProxyDocSpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.