Skip to content

Commit 1d60cd0

Browse files
committed
Make toJava(scalaFuture).toCF.obtrude* unsupported
The status quo was to allow the wrapper and the wrapped to diverge; the as shown by: ``` @test public void testToJavaToCompletableFutureJavaObtrudeCalledAfterScalaComplete() throws Exception { final Promise<String> p = promise(); Future<String> sf = p.future(); final CompletionStage<String> cs = toJava(sf); CompletableFuture<String> cf = cs.toCompletableFuture(); assertEquals("notyet", cf.getNow("notyet")); p.success("scaladone"); assertEquals("scaladone", cf.get()); cf.obtrudeValue("javadone"); assertEquals("javadone", cf.get()); // obtrude does not mutate the underlying Scala future assertEquals("scaladone", Await.result(sf, Duration.Inf())); } ``` This commit throws an UOE, preferring to fail fast.
1 parent 385de85 commit 1d60cd0

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/main/scala/scala/concurrent/java8/FutureConvertersImpl.scala

+5-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ object FuturesConvertersImpl {
7272
* WARNING: completing the result of this method will not complete the underlying
7373
* Scala Future or Promise (ie, the one that that was passed to `toJava`.)
7474
*/
75-
override def toCompletableFuture(): CompletableFuture[T] = {
76-
this
77-
}
75+
override def toCompletableFuture(): CompletableFuture[T] = this
76+
77+
override def obtrudeValue(value: T): Unit = throw new UnsupportedOperationException("obtrudeValue may not be used on the result of toJava(scalaFuture)")
78+
79+
override def obtrudeException(ex: Throwable): Unit = throw new UnsupportedOperationException("obtrudeException may not be used on the result of toJava(scalaFuture)")
7880

7981
override def get(): T = scala.concurrent.blocking(super.get())
8082

src/test/java/scala/compat/java8/FutureConvertersTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,24 @@ public void testToJavaToCompletableFutureJavaCompleteCalledBeforeScalaComplete()
378378
p.success("scaladone");
379379
assertEquals("javadone", cf.get());
380380
}
381+
382+
@Test
383+
public void testToJavaToCompletableFutureJavaObtrudeCalledBeforeScalaComplete() throws ExecutionException, InterruptedException {
384+
final Promise<String> p = promise();
385+
Future<String> sf = p.future();
386+
final CompletionStage<String> cs = toJava(sf);
387+
CompletableFuture<String> cf = cs.toCompletableFuture();
388+
try {
389+
cf.obtrudeValue("");
390+
fail();
391+
} catch (UnsupportedOperationException iae) {
392+
// okay
393+
}
394+
try {
395+
cf.obtrudeException(new Exception());
396+
fail();
397+
} catch (UnsupportedOperationException iae) {
398+
// okay
399+
}
400+
}
381401
}

0 commit comments

Comments
 (0)