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

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

This example Play Framework source code file (IterateeSpecification.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

a, a*, concurrent, cont, duration, enumerator, executioncontext, future, illegalargumentexception, iterate, iteratee, library, play, string, time

The IterateeSpecification.scala Play Framework example source code

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

import play.api.libs.iteratee.internal.executeFuture
import scala.concurrent.{ Await, ExecutionContext, Future, Promise }
import scala.concurrent.duration.{ Duration, SECONDS, MILLISECONDS }
import org.specs2.mutable.SpecificationLike
import scala.util.Try

/**
 * Common functionality for iteratee tests.
 */
trait IterateeSpecification extends NoConcurrentExecutionContext {
  self: org.specs2.mutable.SpecificationLike =>

  val waitTime = Duration(5, SECONDS)
  def await[A](f: Future[A]): A = Await.result(f, waitTime)
  def ready[A](f: Future[A]): Future[A] = Await.ready(f, waitTime)

  def mustTransformTo[E, A](in: E*)(out: A*)(e: Enumeratee[E, A]) = {
    val f = Future(Enumerator(in: _*) |>>> e &>> Iteratee.getChunks[A])(Execution.defaultExecutionContext).flatMap[List[A]](x => x)(Execution.defaultExecutionContext)
    Await.result(f, Duration.Inf) must equalTo(List(out: _*))
  }

  def enumeratorChunks[E](e: Enumerator[E]): Future[List[E]] = {
    executeFuture(e |>>> Iteratee.getChunks[E])(Execution.defaultExecutionContext)
  }

  def mustEnumerateTo[E, A](out: A*)(e: Enumerator[E]) = {
    Await.result(enumeratorChunks(e), Duration.Inf) must equalTo(List(out: _*))
  }

  def mustPropagateFailure[E](e: Enumerator[E]) = {
    Try(Await.result(
      e(Cont { case _ => throw new RuntimeException() }),
      Duration.Inf
    )) must beAFailedTry
  }

  /**
   * Convenience function for creating a Done Iteratee that returns the given value
   */
  def done(value: String): Iteratee[String, String] = Done[String, String](value)
  
  /**
   * Convenience function for an Error Iteratee that contains the given error message
   */
  def error(msg: String): Iteratee[String, String] = Error[String](msg, Input.Empty)
  
  /**
   * Convenience function for creating a Cont Iteratee that feeds its input to the given function
   */
  def cont(f: String => Iteratee[String, String]): Iteratee[String, String] = {
    Cont[String, String]({
      case Input.El(input: String) => f(input)
      case unrecognized => throw new IllegalArgumentException(s"Unexpected input for Cont iteratee: $unrecognized")
    })
  }
  
  /**
   * Convenience function for creating the given Iteratee after the given delay 
   */
  def delayed(it: => Iteratee[String, String], delay: Duration = Duration(5, MILLISECONDS))(implicit ec: ExecutionContext): Iteratee[String, String] = {
    Iteratee.flatten(timeout(it, delay))
  }
  
  val timer = new java.util.Timer
  def timeout[A](a: => A, d: Duration)(implicit e: ExecutionContext): Future[A] = {
    val p = Promise[A]()
    timer.schedule(new java.util.TimerTask {
      def run() {
        p.complete(Try(a))
      }
    }, d.toMillis)
    p.future
  }  
}

Other Play Framework source code examples

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