org.scalatest.concurrent.ScalaFutures.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.11.0-RC2 Show documentation
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest.concurrent
import org.scalatest.time.Span
import org.scalatest.exceptions.StackDepthExceptionHelper.getStackDepthFun
import org.scalatest.Suite.anExceptionThatShouldCauseAnAbort
import org.scalatest.Resources
import org.scalatest.exceptions.{TestPendingException, TestFailedException, TimeoutField}
import org.scalatest.exceptions.TestCanceledException
import scala.util.Success
import scala.util.Failure
/**
* Provides an implicit conversion from scala.concurrent.Future[T]
to
* FutureConcept[T]
.
*
*
* This trait enables you to invoke the methods defined on FutureConcept
on a Scala Future
, as well as to pass a Scala future
* to the whenReady
methods of supertrait Futures
.
* See the documentation for supertrait Futures
for the details on the syntax this trait provides
* for testing with Scala futures.
*
*
* @author Bill Venners
*/
trait ScalaFutures extends Futures {
import scala.language.implicitConversions
/**
* Implicitly converts a scala.concurrent.Future[T]
to
* FutureConcept[T]
, allowing you to invoke the methods
* defined on FutureConcept
on a Scala Future
, as well as to pass a Scala future
* to the whenReady
methods of supertrait Futures
.
*
*
* See the documentation for supertrait Futures
for the details on the syntax this trait provides
* for testing with Java futures.
*
*
*
* If the eitherValue
method of the underlying Scala future returns a scala.Some
containing a
* scala.util.Failure
containing a java.util.concurrent.ExecutionException
, and this
* exception contains a non-null
cause, that cause will be included in the TestFailedException
as its cause. The
* ExecutionException
will be be included as the TestFailedException
's cause only if the
* ExecutionException
's cause is null
.
*
*
*
* The isExpired
method of the returned FutureConcept
will always return false
, because
* the underlying type, scala.concurrent.Future
, does not support the notion of expiration. Likewise, the isCanceled
* method of the returned FutureConcept
will always return false
, because
* the underlying type, scala.concurrent.Future
, does not support the notion of cancelation.
*
*
* @param scalaFuture a scala.concurrent.Future[T]
to convert
* @return a FutureConcept[T]
wrapping the passed scala.concurrent.Future[T]
*/
implicit def convertScalaFuture[T](scalaFuture: scala.concurrent.Future[T]): FutureConcept[T] =
new FutureConcept[T] {
def eitherValue: Option[Either[Throwable, T]] =
scalaFuture.value.map {
case Success(o) => Right(o)
case Failure(e) => Left(e)
}
def isExpired: Boolean = false // Scala Futures themselves don't support the notion of a timeout
def isCanceled: Boolean = false // Scala Futures don't seem to be cancelable either
/*
def futureValue(implicit config: PatienceConfig): T = {
try Await.ready(scalaFuture, Duration.fromNanos(config.timeout.totalNanos))
catch {
case e: TimeoutException =>
}
}
*/
}
}
/**
* Companion object that facilitates the importing of ScalaFutures
members as
* an alternative to mixing in the trait. One use case is to import ScalaFutures
's members so you can use
* them in the Scala interpreter.
*/
object ScalaFutures extends ScalaFutures