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

Play Framework/Scala example source code file (AkkaSpec.scala)

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

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

Play Framework tags/keywords

actor, akkaplugin, akkaspec, api, block, blockingactor, concurrent, fakeapplication, lib, library, long, play, play framework, specification

The AkkaSpec.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.api.libs.concurrent

import org.specs2.mutable._
import play.api.libs._
import play.api.FakeApplication
import akka.actor.{Props, Actor}
import java.util.Date

object AkkaSpec extends Specification {
  
  // returns the time it took to stop the Akka plugin in milliseconds
  def testBlockingActor(app: play.api.Application): Long = {
    val plugin = new AkkaPlugin(app)

    val blockingActor = plugin.applicationSystem.actorOf(Props[BlockingActor])

    blockingActor ! Block

    val startTime = System.nanoTime()

    plugin.onStop()

    val endTime = System.nanoTime()

    (endTime - startTime) / 1000 / 1000
  }

  "AkkaPlugin with a blocking Actor" should {
    "by default wait until the blocking actor is terminated" in {
      val totalTime = testBlockingActor(FakeApplication())
      
      // the actor blocks shutdown for 10 seconds
      totalTime must be greaterThanOrEqualTo 10000
    }
    "be able to be restarted when the Actor is blocking if the config is provided" in {
      val app = FakeApplication(Map("play.akka.shutdown-timeout" -> "500ms"))
      val totalTime = testBlockingActor(app)

      // the actor blocks for 10 seconds but we should be back here in around 500ms
      totalTime must be between (500, 10000)
    }
  }

}

class BlockingActor extends Actor {
  
  def receive = {
    case Block =>
      context.system.log.info("BlockingActor is now blocking for 10 seconds")
      Thread.sleep(10000)
      context.system.log.info("BlockingActor is done blocking")
  }
  
}

case object Block

Other Play Framework source code examples

Here is a short list of links related to this Play Framework AkkaSpec.scala source code file:

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.