@@ -44,20 +44,79 @@ class PromiseTests extends MinimalScalaTest {
4444 }.getMessage mustBe (" br0ken" )
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 {
0 commit comments