8000 Merge pull request #4357 from retronym/merge/2.10.x-to-2.11.x-20150224 · scala/scala-dev@092690e · GitHub
[go: up one dir, main page]

Skip to content

Commit 092690e

Browse files
committed
Merge pull request #4357 from retronym/merge/2.10.x-to-2.11.x-20150224
Merge 2.10.x to 2.11.x
2 parents dff4799 + ce68a97 commit 092690e

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

src/library/scala/concurrent/Promise.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ trait Promise[T] {
6666
*
6767
* @return This promise
6868
*/
69-
final def completeWith(other: Future[T]): this.type = {
70-
other onComplete { this complete _ }
71-
this
72-
}
69+
final def completeWith(other: Future[T]): this.type = tryCompleteWith(other)
7370

7471
/** Attempts to complete this promise with the specified future, once that future is completed.
7572
*

test/files/jvm/future-spec/PromiseTests.scala

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,79 @@ class PromiseTests extends MinimalScalaTest {
4444
}.getMessage mustBe ("br0ken" 8000 ;)
4545
}
4646

47+
"be completable with a completed Promise" in {
48+
{
49+
val p = Promise[String]()
50+
p.tryCompleteWith(Promise[String]().success("foo").future)
51+
Await.result(p.future, defaultTimeout) mustBe ("foo")
52+
}
53+
{
54+
val p = Promise[String]()
55+
p.completeWith(Promise[String]().success("foo").future)
56+
Await.result(p.future, defaultTimeout) mustBe ("foo")
57+
}
58+
{
59+
val p = Promise[String]()
60+
p.tryCompleteWith(Promise[String]().failure(new RuntimeException("br0ken")).future)
61+
intercept[RuntimeException] {
62+
Await.result(p.future, defaultTimeout)
63+
}.getMessage mustBe ("br0ken")
64+
}
65+
{
66+
val p = Promise[String]()
67+
p.tryCompleteWith(Promise[String]().failure(new RuntimeException("br0ken")).future)
68+
intercept[RuntimeException] {
69+
Await.result(p.future, defaultTimeout)
70+
}.getMessage mustBe ("br0ken")
71+
}
72+
}
4773
}
4874

4975
"A successful Promise" should {
50-
val result = "test value"
51-
val promise = Promise[String]().complete(Success(result))
52-
promise.isCompleted mustBe (true)
53-
futureWithResult(_(promise.future, result))
76+
"be completed" in {
77+
val result = "test value"
78+
val promise = Promise[String]().complete(Success(result))
79+
promise.isCompleted mustBe (true)
80+
futureWithResult(_(promise.future, result))
81+
}
82+
83+
"not be completable with a completed Promise" in {
84+
{
85+
val p = Promise.successful("bar")
86+
p.tryCompleteWith(Promise[String]().success("foo").future)
87+
Await.result(p.future, defaultTimeout) mustBe ("bar")
88+
}
89+
{
90+
val p = Promise.successful("bar")
91+
p.completeWith(Promise[String]().success("foo").future)
92+
Await.result(p.future, defaultTimeout) mustBe ("bar")
93+
}
94+
}
5495
}
5596

5697
"A failed Promise" should {
57-
val message = "Expected Exception"
58-
val promise = Promise[String]().complete(Failure(new RuntimeException(message)))
59-
promise.isCompleted mustBe (true)
60-
futureWithException[RuntimeException](_(promise.future, message))
98+
"be completed" in {
99+
val message = "Expected Exception"
100+
val promise = Promise[String]().complete(Failure(new RuntimeException(message)))
101+
promise.isCompleted mustBe (true)
102+
futureWithException[RuntimeException](_(promise.future, message))
103+
}
104+
"not be completable with a completed Promise" in {
105+
{
106+
val p = Promise[String]().failure(new RuntimeException("unbr0ken"))
107+
p.tryCompleteWith(Promise[String].failure(new Exception("br0ken")).future)
108+
intercept[RuntimeException] {
109+
Await.result(p.future, defaultTimeout)
110+
}.getMessage mustBe ("unbr0ken")
111+
}
112+
{
113+
val p = Promise[String]().failure(new RuntimeException("unbr0ken"))
114+
p.completeWith(Promise[String]().failure(new Exception("br0ken")).future)
115+
intercept[RuntimeException] {
116+
Await.result(p.future, defaultTimeout)
117+
}.getMessage mustBe ("unbr0ken")
118+
}
119+
}
61120
}
62121

63122
"An interrupted Promise" should {

test/files/jvm/t8689.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
success

test/files/jvm/t8689.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
import scala.concurrent._
4+
import ExecutionContext.Implicits.global
5+
val source1 = Promise[Int]()
6+
val source2 = Promise[Int]()
7+
source2.completeWith(source1.future).future.onComplete {
8+
case _ => print("success")
9+
}
10+
source2.tryFailure(new TimeoutException)
11+
source1.success(123)
12+
}
13+
}

0 commit comments

Comments
 (0)
0