Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: add CompletableFutures#fromCompletableFuture method
Motivation: for smooth migration from guava's ListenableFuture to java8's CompletableFuture there will be a time period where both will be used in parallel. Modification: add CompletableFutures#fromCompletableFuture method to create a ListenableFuture from CompletableFuture. Result: code that uses CompletableFuture can be mixed with ListenableFuture based one. Acked-by: Marina Sahakyan Target: master Require-book: no Require-notes: no
- Loading branch information
Showing
2 changed files
with
184 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
modules/common/src/test/java/org/dcache/util/CompletableFuturesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package org.dcache.util; | ||
|
||
import com.google.common.util.concurrent.Futures; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.common.util.concurrent.SettableFuture; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class CompletableFuturesTest { | ||
|
||
@Test | ||
public void shouldNotBeDoneIfCompletableNotDone() { | ||
|
||
CompletableFuture<Void> completable = new CompletableFuture<>(); | ||
|
||
ListenableFuture<Void> listenable = CompletableFutures.fromCompletableFuture(completable); | ||
assertFalse(listenable.isDone()); | ||
} | ||
|
||
@Test | ||
public void shouldNotBeDoneIfListenableFutureNotDone() { | ||
|
||
ListenableFuture<Void> listenable = SettableFuture.create(); | ||
CompletableFuture<Void> completable = CompletableFutures.fromListenableFuture(listenable); | ||
|
||
assertFalse(completable.isDone()); | ||
} | ||
|
||
@Test | ||
public void shouldBeDoneWhenCompletableIsDone() { | ||
|
||
CompletableFuture<Void> completable = CompletableFuture.completedFuture(null); | ||
|
||
ListenableFuture<Void> listenable = CompletableFutures.fromCompletableFuture(completable); | ||
assertTrue(listenable.isDone()); | ||
} | ||
|
||
@Test | ||
public void shouldBeDoneWhenListenableFutureIsDone() { | ||
|
||
ListenableFuture<Void> listenable = Futures.immediateFuture(null); | ||
CompletableFuture<Void> completable = CompletableFutures.fromListenableFuture(listenable); | ||
|
||
assertTrue(completable.isDone()); | ||
} | ||
|
||
@Test | ||
public void shouldCompleteWhenCompletableIsDone() { | ||
|
||
CompletableFuture<Void> completable = new CompletableFuture<>(); | ||
|
||
ListenableFuture<Void> listenable = CompletableFutures.fromCompletableFuture(completable); | ||
completable.complete(null); | ||
assertTrue(listenable.isDone()); | ||
} | ||
|
||
@Test | ||
public void shouldCompleteWhenListenableIsDone() { | ||
|
||
SettableFuture<Void> listenable = SettableFuture.create(); | ||
CompletableFuture<Void> completable = CompletableFutures.fromListenableFuture(listenable); | ||
|
||
listenable.set(null); | ||
assertTrue(completable.isDone()); | ||
} | ||
|
||
// requires Java9+ | ||
// @Test(expected = ExecutionException.class) | ||
// public void shouldFailWhenCompletableIsFailed() throws InterruptedException, ExecutionException { | ||
// | ||
// CompletableFuture<Void> completable = CompletableFuture.failedFuture(new IOException()); | ||
// | ||
// ListenableFuture<Void> listenable = CompletableFutures.fromCompletableFuture(completable); | ||
// assertTrue(listenable.isDone()); | ||
// listenable.get(); | ||
// } | ||
|
||
@Test(expected = ExecutionException.class) | ||
public void shouldFailWhenCompletableIsFailed() throws InterruptedException, ExecutionException { | ||
|
||
CompletableFuture<Void> completable = new CompletableFuture<>(); | ||
ListenableFuture<Void> listenable = CompletableFutures.fromCompletableFuture(completable); | ||
|
||
completable.completeExceptionally(new IOException()); | ||
|
||
assertTrue(listenable.isDone()); | ||
listenable.get(); | ||
} | ||
|
||
@Test(expected = ExecutionException.class) | ||
public void shouldFailWhenListenableIsFailed() throws InterruptedException, ExecutionException { | ||
|
||
ListenableFuture<Void> listenable = Futures.immediateFailedFuture(new IOException()); | ||
CompletableFuture<Void> completable = CompletableFutures.fromListenableFuture(listenable); | ||
|
||
assertTrue(completable.isDone()); | ||
completable.get(); | ||
} | ||
|
||
@Test(expected = ExecutionException.class) | ||
public void shouldFailWhenListenableCompletesExceptionally() throws InterruptedException, ExecutionException { | ||
|
||
SettableFuture<Void> listenable = SettableFuture.create(); | ||
CompletableFuture<Void> completable = CompletableFutures.fromListenableFuture(listenable); | ||
|
||
listenable.setException(new IOException()); | ||
assertTrue(completable.isDone()); | ||
completable.get(); | ||
} | ||
|
||
@Test | ||
public void shouldCancelWhenCompletableIsCanceled() throws InterruptedException, ExecutionException { | ||
|
||
CompletableFuture<Void> completable = new CompletableFuture<>(); | ||
ListenableFuture<Void> listenable = CompletableFutures.fromCompletableFuture(completable); | ||
|
||
completable.cancel(true); | ||
assertTrue(listenable.isDone()); | ||
assertTrue(listenable.isCancelled()); | ||
} | ||
|
||
@Test | ||
public void shouldCancelWhenListenableIsCanceled() throws InterruptedException, ExecutionException { | ||
|
||
SettableFuture<Void> listenable = SettableFuture.create(); | ||
CompletableFuture<Void> completable = CompletableFutures.fromListenableFuture(listenable); | ||
|
||
listenable.cancel(true); | ||
assertTrue(completable.isDone()); | ||
assertTrue(completable.isCancelled()); | ||
} | ||
|
||
} |