Futures { public static <T> CompletionStage<T> asCompletionStage( io.netty.util.concurrent.Future<T> future ) { CompletableFuture<T> result = new CompletableFuture<>(); return asCompletionStage( future, result ); } private  Futures(); @SuppressWarnings( "unchecked" ) static CompletableFuture<T> completedWithNull(); static CompletableFuture<T> completeWithNullIfNoError( CompletableFuture<T> future, Throwable error ); static CompletionStage<T> asCompletionStage( io.netty.util.concurrent.Future<T> future ); static CompletionStage<T> asCompletionStage( io.netty.util.concurrent.Future<T> future, CompletableFuture<T> result ); static CompletableFuture<T> failedFuture( Throwable error ); static V blockingGet( CompletionStage<V> stage ); static V blockingGet( CompletionStage<V> stage, Runnable interruptHandler ); static T getNow( CompletionStage<T> stage ); static Throwable completionExceptionCause( Throwable error ); static CompletionException asCompletionException( Throwable error ); static CompletionException combineErrors( Throwable error1, Throwable error2 ); @SuppressWarnings( "ThrowableNotThrown" ) static CompletableFuture<T> onErrorContinue( CompletableFuture<T> future, Throwable errorRecorder,
            Function<Throwable,? extends CompletionStage<T>> onErrorAction );  }
@Test void shouldConvertCanceledNettyFutureToCompletionStage() throws Exception { DefaultPromise<String> promise = new DefaultPromise<>( ImmediateEventExecutor.INSTANCE ); promise.cancel( true ); CompletableFuture<String> future = Futures.asCompletionStage( promise ).toCompletableFuture(); assertTrue( future.isCancelled() ); assertTrue( future.isCompletedExceptionally() ); assertThrows( CancellationException.class, future::get ); }
@Test void shouldConvertSucceededNettyFutureToCompletionStage() throws Exception { SucceededFuture<String> nettyFuture = new SucceededFuture<>( ImmediateEventExecutor.INSTANCE, "Hello" ); CompletableFuture<String> future = Futures.asCompletionStage( nettyFuture ).toCompletableFuture(); assertTrue( future.isDone() ); assertFalse( future.isCompletedExceptionally() ); assertEquals( "Hello", future.get() ); }
@Test void shouldConvertFailedNettyFutureToCompletionStage() throws Exception { RuntimeException error = new RuntimeException( "Hello" ); FailedFuture<Object> nettyFuture = new FailedFuture<>( ImmediateEventExecutor.INSTANCE, error ); CompletableFuture<Object> future = Futures.asCompletionStage( nettyFuture ).toCompletableFuture(); assertTrue( future.isCompletedExceptionally() ); ExecutionException e = assertThrows( ExecutionException.class, future::get ); assertEquals( error, e.getCause() ); }
@Test void shouldConvertRunningNettyFutureToCompletionStageWhenFutureCanceled() throws Exception { DefaultPromise<String> promise = new DefaultPromise<>( ImmediateEventExecutor.INSTANCE ); CompletableFuture<String> future = Futures.asCompletionStage( promise ).toCompletableFuture(); assertFalse( future.isDone() ); promise.cancel( true ); assertTrue( future.isCancelled() ); assertTrue( future.isCompletedExceptionally() ); assertThrows( CancellationException.class, future::get ); }
@Test void shouldConvertRunningNettyFutureToCompletionStageWhenFutureSucceeded() throws Exception { DefaultPromise<String> promise = new DefaultPromise<>( ImmediateEventExecutor.INSTANCE ); CompletableFuture<String> future = Futures.asCompletionStage( promise ).toCompletableFuture(); assertFalse( future.isDone() ); promise.setSuccess( "Hello" ); assertTrue( future.isDone() ); assertFalse( future.isCompletedExceptionally() ); assertEquals( "Hello", future.get() ); }
@Test void shouldConvertRunningNettyFutureToCompletionStageWhenFutureFailed() throws Exception { RuntimeException error = new RuntimeException( "Hello" ); DefaultPromise<String> promise = new DefaultPromise<>( ImmediateEventExecutor.INSTANCE ); CompletableFuture<String> future = Futures.asCompletionStage( promise ).toCompletableFuture(); assertFalse( future.isDone() ); promise.setFailure( error ); assertTrue( future.isCompletedExceptionally() ); ExecutionException e = assertThrows( ExecutionException.class, future::get ); assertEquals( error, e.getCause() ); }