Skip to content

Support blocking with: scalaFuture.toJava.toCompletableFuture #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/main/scala/scala/concurrent/java8/FutureConvertersImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package scala.concurrent.java8
// Located in this package to access private[concurrent] members

import scala.concurrent.{ Future, Promise, ExecutionContext, ExecutionContextExecutorService, ExecutionContextExecutor, impl }
import java.util.concurrent.{ CompletionStage, Executor, ExecutorService, CompletableFuture }
import java.util.concurrent._
import scala.util.{ Try, Success, Failure }
import java.util.function.{ BiConsumer, Function ⇒ JF, Consumer, BiFunction }

Expand Down Expand Up @@ -66,8 +66,21 @@ object FuturesConvertersImpl {
cf
}

override def toCompletableFuture(): CompletableFuture[T] =
throw new UnsupportedOperationException("this CompletionStage represents a read-only Scala Future")
/**
* @inheritdoc
*
* WARNING: completing the result of this method will not complete the underlying

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no underlying Scala Future or Promise for this class? (CF)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the CF is completed by a Scala Future prior to toCompletableFuture.complete(x) gets called?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no underlying Scala Future or Promise for this class? (CF)

The underlying future is the one passed to toJava:

  def toJava[T](f: Future[T]): CompletionStage[T] = {
    val cf = new CF[T]
    implicit val ec = InternalCallbackExecutor
    f onComplete cf
    cf
  }

In my other PR, that also becomes a field of CF so that we can efficiently unwrap it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the CF is completed by a Scala Future prior to toCompletableFuture.complete(x) gets called?

I've added a test case to show what happens, could you please review it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That test looks great.

What happens if obtrudeX is called?

* Scala Future or Promise (ie, the one that that was passed to `toJava`.)
*/
override def toCompletableFuture(): CompletableFuture[T] = this

override def obtrudeValue(value: T): Unit = throw new UnsupportedOperationException("obtrudeValue may not be used on the result of toJava(scalaFuture)")

override def obtrudeException(ex: Throwable): Unit = throw new UnsupportedOperationException("obtrudeException may not be used on the result of toJava(scalaFuture)")

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

override def get(timeout: Long, unit: TimeUnit): T = scala.concurrent.blocking(super.get(timeout, unit))

override def toString: String = super[CompletableFuture].toString
}
Expand Down
88 changes: 84 additions & 4 deletions src/test/java/scala/compat/java8/FutureConvertersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import scala.concurrent.Future;
import scala.concurrent.Promise;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.*;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.*;
import static scala.compat.java8.FutureConverters.*;
Expand Down Expand Up @@ -318,4 +316,86 @@ public void testToJavaExceptionally() throws InterruptedException,
latch.countDown();
assertEquals("Hello", second.toCompletableFuture().get());
}

@Test
public void testToJavaThenComposeWithToJavaThenAccept() throws InterruptedException,
ExecutionException, TimeoutException {
// Test case from https://github.com/scala/scala-java8-compat/issues/29
final Promise<String> p1 = promise();
final CompletableFuture<String> future = new CompletableFuture<>();

CompletableFuture.supplyAsync(() -> "Hello").
thenCompose(x -> toJava(p1.future())).handle((x, t) -> future.complete(x));
p1.success("Hello");
assertEquals("Hello", future.get(1000, MILLISECONDS));
}

@Test
public void testToJavaToCompletableFuture() throws ExecutionException, InterruptedException {
final Promise<String> p = promise();
final CompletionStage<String> cs = toJava(p.future());
CompletableFuture<String> cf = cs.toCompletableFuture();
assertEquals("notyet", cf.getNow("notyet"));
p.success("done");
assertEquals("done", cf.get());
}

@Test
public void testToJavaToCompletableFutureDoesNotMutateUnderlyingPromise() throws ExecutionException, InterruptedException {
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"));
cf.complete("done");
assertEquals("done", cf.get());
assertFalse(sf.isCompleted());
assertFalse(p.isCompleted());
}

@Test
public void testToJavaToCompletableFutureJavaCompleteCalledAfterScalaComplete() throws ExecutionException, InterruptedException {
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.complete("javadone");
assertEquals("scaladone", cf.get());
}

@Test
public void testToJavaToCompletableFutureJavaCompleteCalledBeforeScalaComplete() throws ExecutionException, InterruptedException {
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"));
cf.complete("javadone");
assertEquals("javadone", cf.get());
p.success("scaladone");
assertEquals("javadone", cf.get());
}

@Test
public void testToJavaToCompletableFutureJavaObtrudeCalledBeforeScalaComplete() throws ExecutionException, InterruptedException {
final Promise<String> p = promise();
Future<String> sf = p.future();
final CompletionStage<String> cs = toJava(sf);
CompletableFuture<String> cf = cs.toCompletableFuture();
try {
cf.obtrudeValue("");
fail();
} catch (UnsupportedOperationException iae) {
// okay
}
try {
cf.obtrudeException(new Exception());
fail();
} catch (UnsupportedOperationException iae) {
// okay
}
}
}