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

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

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

api, concurrent, exception, library, nosuchelementexception, play, play framework, playspecification, promise, promisespec, redeemed, test, thrown, withapplication

The PromiseSpec.scala Play Framework example source code

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

import play.api.test._
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.libs.concurrent.Promise


class PromiseSpec extends PlaySpecification {

  "Promise" can {

    "Redeemed values" in new WithApplication() {
      val p = Promise.timeout(42, 100)
      await(p.filter(_ == 42)) must equalTo (42)
    }

    "Redeemed values not matching the predicate" in new WithApplication() {
      val p = Promise.timeout(42, 100)
      await(p.filter(_ != 42)) must throwA [NoSuchElementException]
    }

    "Thrown values" in new WithApplication() {
      val p = Promise.timeout(42, 100).map[Int]{ _ => throw new Exception("foo") }
      await(p.filter(_ => true)) must throwAn [Exception](message = "foo")
    }

  }

  "Promise timeouts" should {

    "yield their message" in new WithApplication() {
      val future = Promise.timeout("hello", 10)
      await(future) must_== "hello"
    }

    "yield any exceptions thrown when generating a message" in new WithApplication() {
      val future = Promise.timeout[Unit](throw new Exception("error!"), 10)
      await(future) must throwAn[Exception](message = "error!")
    }

  }

}

Other Play Framework source code examples

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