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

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

This example Akka source code file (TestFSMRefSpec.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, akka, akkaspec, concurrent, duration, event, fsm, null, string, test, testfsmref, testfsmrefspec, testing, time

The TestFSMRefSpec.scala Akka example source code

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

package akka.testkit

import language.postfixOps

import org.scalatest.Matchers
import org.scalatest.{ BeforeAndAfterEach, WordSpec }
import akka.actor._
import scala.concurrent.duration._

@org.junit.runner.RunWith(classOf[org.scalatest.junit.JUnitRunner])
class TestFSMRefSpec extends AkkaSpec {

  "A TestFSMRef" must {

    "allow access to state data" in {
      val fsm = TestFSMRef(new Actor with FSM[Int, String] {
        startWith(1, "")
        when(1) {
          case Event("go", _)         ⇒ goto(2) using "go"
          case Event(StateTimeout, _) ⇒ goto(2) using "timeout"
        }
        when(2) {
          case Event("back", _) ⇒ goto(1) using "back"
        }
      }, "test-fsm-ref-1")
      fsm.stateName should be(1)
      fsm.stateData should be("")
      fsm ! "go"
      fsm.stateName should be(2)
      fsm.stateData should be("go")
      fsm.setState(stateName = 1)
      fsm.stateName should be(1)
      fsm.stateData should be("go")
      fsm.setState(stateData = "buh")
      fsm.stateName should be(1)
      fsm.stateData should be("buh")
      fsm.setState(timeout = 100 millis)
      within(80 millis, 500 millis) {
        awaitCond(fsm.stateName == 2 && fsm.stateData == "timeout")
      }
    }

    "allow access to timers" in {
      val fsm = TestFSMRef(new Actor with FSM[Int, Null] {
        startWith(1, null)
        when(1) {
          case x ⇒ stay
        }
      }, "test-fsm-ref-2")
      fsm.isTimerActive("test") should be(false)
      fsm.setTimer("test", 12, 10 millis, true)
      fsm.isTimerActive("test") should be(true)
      fsm.cancelTimer("test")
      fsm.isTimerActive("test") should be(false)
    }
  }
}

Other Akka source code examples

Here is a short list of links related to this Akka TestFSMRefSpec.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.