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

Conversation

retronym
Copy link
Member

Thought I'd get the ball rolling with a PR.

Review by @jroper / @viktorklang / @rkuhn (as available)

/**
* @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?

@viktorklang
Copy link

One thought, shouldn't get() + get(timeout) use scala.concurrent.blocking?

@retronym
Copy link
Member Author

One thought, shouldn't get() + get(timeout) use scala.concurrent.blocking?

Sounds legit. Does my last commit put this in the right spot?

@retronym
Copy link
Member Author

@viktorklang Please see my latest commit that addresses the obtrude.

I believe this implements the somewhat reluctant consensus
reached in scala#43. Go with the flow by allowing people to block
using the standard Java 8 idiom of `toCompletableFuture.get`.

I've added a comment and a test to caution that calling
`toCompletableFuture.complete(value)` does *not* effect the underlying
Promise.

Fixes scala#43
Since `toCompletableFuture` stopped throwing an
`UnsupportedOperationException` in the previous commit, this use
case is now supported.

Fixes scala#29
I wasn't sure which way to go here, but now we have a few more
tests I guess this comment doesn't really help anyone.
As suggested by Viktor.
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.
retronym added a commit that referenced this pull request Aug 18, 2015
Support blocking with: `scalaFuture.toJava.toCompletableFuture`
@retronym retronym merged commit 2533dc1 into scala:master Aug 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants