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

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

This example Play Framework source code file (OrderedExecutionContextSpec.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, actorsystem, atomicinteger, concurrent, core, countdownlatch, httpexecutioncontext, int, mvc, orderedexecutioncontext, orderedexecutioncontextspec, play, play framework, runnable, seconds, specification

The OrderedExecutionContextSpec.scala Play Framework example source code

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

import akka.actor.ActorSystem
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit.SECONDS
import java.util.concurrent.atomic.AtomicInteger
import org.specs2.mutable.Specification
import play.mvc.Http
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext

object OrderedExecutionContextSpec extends Specification {

  "OrderedExecutionContext" should {

    "execute its tasks in order" in {
      val actorSystem = ActorSystem("OrderedExecutionContextSpec")
      val oec = new OrderedExecutionContext(actorSystem, 64)

      def run(id: Int): (CountDownLatch, AtomicInteger) = {
        val hec = new HttpExecutionContext(
          Thread.currentThread().getContextClassLoader(),
          new Http.Context(id, null, null, Map.empty.asJava, Map.empty.asJava, Map.empty.asJava),
          oec)
        val numTasks = 2000
        val taskCount = new AtomicInteger()
        val outOfOrderCount = new AtomicInteger()
        val ready = new CountDownLatch(numTasks)
        actorSystem.dispatcher.execute(new Runnable {
          def run() = {
            for (i <- 0 until numTasks) {
              hec.execute(new Runnable {
                def run() = {
                  val order = taskCount.getAndIncrement()
                  if (order != i) outOfOrderCount.incrementAndGet()
                  ready.countDown()
                }
              })
            }
          }
        })
        (ready, outOfOrderCount)
      }
      val results = for (i <- 0 until 100) yield run(i)
      for ((ready, outOfOrderCount) <- results) {
        ready.await(10, SECONDS) must beTrue
        outOfOrderCount.get() must equalTo(0)
      }

      actorSystem.shutdown()
      success
    }

  }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework OrderedExecutionContextSpec.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.