From ea81ab3314359c98986e6fac74c807fa1accdfb6 Mon Sep 17 00:00:00 2001 From: Heather Miller Date: Tue, 7 Aug 2012 18:36:36 +0200 Subject: Added tests, removal of unnecessary methods, fixes prepare --- src/library/scala/concurrent/ExecutionContext.scala | 8 +++----- src/library/scala/concurrent/impl/Promise.scala | 9 +++------ src/library/scala/util/Try.scala | 13 +------------ test/files/jvm/future-spec/TryTests.scala | 12 ++++++------ test/files/jvm/try-type-tests.scala | 14 ++++++++++++++ 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/library/scala/concurrent/ExecutionContext.scala b/src/library/scala/concurrent/ExecutionContext.scala index ac462ac9d2..82c1cff4b1 100644 --- a/src/library/scala/concurrent/ExecutionContext.scala +++ b/src/library/scala/concurrent/ExecutionContext.scala @@ -28,12 +28,10 @@ trait ExecutionContext { */ def reportFailure(t: Throwable): Unit - /** Prepares for the execution of callback `f`. Returns the prepared - * execution context which should be used to schedule the execution - * of the task associated with `f`. + /** Prepares for the execution of a task. Returns the prepared + * execution context. */ - def prepare[T, U](f: Try[T] => U): ExecutionContext = - this + def prepare(): ExecutionContext = this } diff --git a/src/library/scala/concurrent/impl/Promise.scala b/src/library/scala/concurrent/impl/Promise.scala index 9f30529dc8..b19bed004b 100644 --- a/src/library/scala/concurrent/impl/Promise.scala +++ b/src/library/scala/concurrent/impl/Promise.scala @@ -129,7 +129,7 @@ private[concurrent] object Promise { } def onComplete[U](func: Try[T] => U)(implicit executor: ExecutionContext): Unit = { - val preparedEC = executor.prepare(func) + val preparedEC = executor.prepare val runnable = new CallbackRunnable[T](preparedEC, func) @tailrec //Tries to add the callback, if already completed, it dispatches the callback to be executed @@ -156,16 +156,13 @@ private[concurrent] object Promise { def onComplete[U](func: Try[T] => U)(implicit executor: ExecutionContext): Unit = { val completedAs = value.get - val preparedEC = executor.prepare(func) + val preparedEC = executor.prepare (new CallbackRunnable(preparedEC, func)).executeWithValue(completedAs) } def ready(atMost: Duration)(implicit permit: CanAwait): this.type = this - def result(atMost: Duration)(implicit permit: CanAwait): T = value.get match { - case Failure(e) => throw e - case Success(r) => r - } + def result(atMost: Duration)(implicit permit: CanAwait): T = value.get.get } } diff --git a/src/library/scala/util/Try.scala b/src/library/scala/util/Try.scala index c834f6d514..f381a18b0c 100644 --- a/src/library/scala/util/Try.scala +++ b/src/library/scala/util/Try.scala @@ -54,7 +54,7 @@ import language.implicitConversions * * `Try` comes to the Scala standard library after years of use as an integral part of Twitter's stack. * - * @author based on Marius Eriksen's original implementation in com.twitter.util. + * @author based on Twitter's original implementation in com.twitter.util. * @since 2.10 */ sealed abstract class Try[+T] { @@ -116,17 +116,6 @@ sealed abstract class Try[+T] { */ def toOption = if (isSuccess) Some(get) else None - /** - * Returns an empty `Seq` (usually a `List`) if this is a `Failure` or a `Seq` containing the value if this is a `Success`. - */ - def toSeq = if (isSuccess) Seq(get) else Seq() - - /** - * Returns the given function applied to the value from this `Success` or returns this if this is a `Failure`. - * Alias for `flatMap`. - */ - def andThen[U](f: T => Try[U]): Try[U] = flatMap(f) - /** * Transforms a nested `Try`, ie, a `Try` of type `Try[Try[T]]`, * into an un-nested `Try`, ie, a `Try` of type `Try[T]`. diff --git a/test/files/jvm/future-spec/TryTests.scala b/test/files/jvm/future-spec/TryTests.scala index 47ce9c2e15..db0be0dfff 100644 --- a/test/files/jvm/future-spec/TryTests.scala +++ b/test/files/jvm/future-spec/TryTests.scala @@ -1,4 +1,4 @@ - +x // This is a port of the com.twitter.util Try spec. // -- // It lives in the future-spec directory simply because it requires a specs-like @@ -55,12 +55,12 @@ object TryTests extends MinimalScalaTest { Failure[Int](e) flatMap(x => Success(1 + x)) mustEqual Failure(e) } - // "when there is an exception" in { - // Success(1).flatMap[Int](_ => throw e) mustEqual Failure(e) + "when there is an exception" in { + Success(1).flatMap[Int](_ => throw e) mustEqual Failure(e) - // val e2 = new Exception - // Failure[Int](e).flatMap[Int](_ => throw e2) mustEqual Failure(e) - // } + val e2 = new Exception + Failure[Int](e).flatMap[Int](_ => throw e2) mustEqual Failure(e) + } } "flatten" in { diff --git a/test/files/jvm/try-type-tests.scala b/test/files/jvm/try-type-tests.scala index 9dece8d6d8..351f02b183 100644 --- a/test/files/jvm/try-type-tests.scala +++ b/test/files/jvm/try-type-tests.scala @@ -103,6 +103,20 @@ trait TryStandard { } } + def testSuccessTransform(): Unit = { + val s = Success(1) + val succ = (x: Int) => Success(x * 10) + val fail = (x: Throwable) => Success(0) + assert(s.transform(succ, fail).get == s.get) + } + + def testFailureTransform(): Unit = { + val f = Failure(new Exception("foo")) + val succ = (x: Int) => Success(x * 10) + val fail = (x: Throwable) => Success(0) + assert(f.transform(succ, fail).get == 0) + } + testForeachSuccess() testForeachFailure() testFlatMapSuccess() -- cgit v1.2.3