Skip to content

Commit

Permalink
Creating or failing a future will null should always return a failed …
Browse files Browse the repository at this point in the history
…future - fixes #1426
  • Loading branch information
vietj committed May 21, 2016
1 parent e365d9c commit 83b6889
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/vertx/core/Future.java
Expand Up @@ -50,7 +50,7 @@ static <T> Future<T> future() {
* @return the future
*/
static <T> Future<T> succeededFuture() {
return factory.completedFuture();
return factory.succeededFuture();
}

/**
Expand All @@ -61,7 +61,7 @@ static <T> Future<T> succeededFuture() {
* @return the future
*/
static <T> Future<T> succeededFuture(T result) {
return factory.completedFuture(result);
return factory.succeededFuture(result);
}

/**
Expand All @@ -73,7 +73,7 @@ static <T> Future<T> succeededFuture(T result) {
*/
@GenIgnore
static <T> Future<T> failedFuture(Throwable t) {
return factory.completedFuture(t);
return factory.failedFuture(t);
}

/**
Expand All @@ -84,7 +84,7 @@ static <T> Future<T> failedFuture(Throwable t) {
* @return the future
*/
static <T> Future<T> failedFuture(String failureMessage) {
return factory.completedFuture(failureMessage, true);
return factory.failureFuture(failureMessage);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/vertx/core/http/impl/HttpServerImpl.java
Expand Up @@ -504,7 +504,8 @@ public int actualPort() {

private void executeCloseDone(final ContextImpl closeContext, final Handler<AsyncResult<Void>> done, final Exception e) {
if (done != null) {
closeContext.runOnContext((v) -> done.handle(Future.failedFuture(e)));
Future<Void> fut = e != null ? Future.failedFuture(e) : Future.succeededFuture();
closeContext.runOnContext((v) -> done.handle(fut));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/io/vertx/core/impl/FutureFactoryImpl.java
Expand Up @@ -31,22 +31,22 @@ public <T> Future<T> future() {

// TODO - for completed futures with null values we could maybe reuse a static instance to save allocation
@Override
public <T> Future<T> completedFuture() {
public <T> Future<T> succeededFuture() {
return new FutureImpl<>((T)null);
}

@Override
public <T> Future<T> completedFuture(T result) {
public <T> Future<T> succeededFuture(T result) {
return new FutureImpl<>(result);
}

@Override
public <T> Future<T> completedFuture(Throwable t) {
public <T> Future<T> failedFuture(Throwable t) {
return new FutureImpl<>(t);
}

@Override
public <T> Future<T> completedFuture(String failureMessage, boolean failed) {
return new FutureImpl<>(failureMessage, true);
public <T> Future<T> failureFuture(String failureMessage) {
return new FutureImpl<>(failureMessage);
}
}
24 changes: 12 additions & 12 deletions src/main/java/io/vertx/core/impl/FutureImpl.java
Expand Up @@ -28,30 +28,30 @@ class FutureImpl<T> implements Future<T>, Handler<AsyncResult<T>> {
private Throwable throwable;

/**
* Create a FutureResult that hasn't completed yet
* Create a future that hasn't completed yet
*/
FutureImpl() {
}

/**
* Create a VoidResult that has already completed
* @param t The Throwable or null if succeeded
* Create a future that has already failed
* @param t the throwable
*/
FutureImpl(Throwable t) {
if (t == null) {
complete(null);
} else {
fail(t);
}
fail(t != null ? t : new NoStackTraceThrowable(null));
}

FutureImpl(String failureMessage, boolean failed) {
/**
* Create a future that has already failed
* @param failureMessage the failure message
*/
FutureImpl(String failureMessage) {
this(new NoStackTraceThrowable(failureMessage));
}

/**
* Create a FutureResult that has already succeeded
* @param result The result
* Create a future that has already succeeded
* @param result the result
*/
FutureImpl(T result) {
complete(result);
Expand Down Expand Up @@ -143,7 +143,7 @@ public void handle(AsyncResult<T> ar) {
*/
public void fail(Throwable throwable) {
checkComplete();
this.throwable = throwable;
this.throwable = throwable != null ? throwable : new NoStackTraceThrowable(null);
failed = true;
checkCallHandler();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/vertx/core/net/impl/NetServerImpl.java
Expand Up @@ -372,7 +372,8 @@ private void actualClose(ContextImpl closeContext, Handler<AsyncResult<Void>> do

private void executeCloseDone(ContextImpl closeContext, Handler<AsyncResult<Void>> done, Exception e) {
if (done != null) {
closeContext.runOnContext(v -> done.handle(Future.failedFuture(e)));
Future<Void> fut = e == null ? Future.succeededFuture() : Future.failedFuture(e);
closeContext.runOnContext(v -> done.handle(fut));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/vertx/core/spi/FutureFactory.java
Expand Up @@ -25,11 +25,11 @@ public interface FutureFactory {

<T> Future<T> future();

<T> Future<T> completedFuture();
<T> Future<T> succeededFuture();

<T> Future<T> completedFuture(T result);
<T> Future<T> succeededFuture(T result);

<T> Future<T> completedFuture(Throwable t);
<T> Future<T> failedFuture(Throwable t);

<T> Future<T> completedFuture(String failureMessage, boolean failed);
<T> Future<T> failureFuture(String failureMessage);
}
21 changes: 20 additions & 1 deletion src/test/java/io/vertx/test/core/FutureTest.java
Expand Up @@ -19,6 +19,7 @@
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.impl.NoStackTraceThrowable;
import org.junit.Test;

import java.util.ArrayList;
Expand Down Expand Up @@ -185,6 +186,24 @@ public void testFailFutureToHandler() {
assertEquals(cause, fut.cause());
}


@Test
public void testCreateFailedWithNullFailure() {
Future<String> future = Future.failedFuture((Throwable)null);
Checker<String> checker = new Checker<>(future);
NoStackTraceThrowable failure = (NoStackTraceThrowable) checker.assertFailed();
assertNull(failure.getMessage());
}

@Test
public void testFailurFutureWithNullFailure() {
Future<String> future = Future.future();
future.fail((Throwable)null);
Checker<String> checker = new Checker<>(future);
NoStackTraceThrowable failure = (NoStackTraceThrowable) checker.assertFailed();
assertNull(failure.getMessage());
}

@Test
public void testAllSucceeded() {
testAllSucceeded(CompositeFuture::all);
Expand Down Expand Up @@ -602,7 +621,7 @@ Throwable assertFailed() {
}
}

/*
/*
private <T> void assertSucceeded(Future<T> future, T expected) {
assertTrue(future.isComplete());
assertTrue(future.succeeded());
Expand Down

0 comments on commit 83b6889

Please sign in to comment.