io.vertx.ext.unit.impl.CompletionImpl Maven / Gradle / Ivy
package io.vertx.ext.unit.impl;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.ext.unit.Completion;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* @author Julien Viet
*/
public class CompletionImpl implements Completion {
protected final CompletableFuture completable = new CompletableFuture<>();
@Override
public void resolve(Promise future) {
completable.whenComplete((done, err) -> {
if (err != null) {
future.fail(err);
} else {
future.complete();
}
});
}
@Override
public boolean isCompleted() {
return completable.isDone();
}
@Override
public boolean isSucceeded() {
return isCompleted() && !isFailed();
}
@Override
public boolean isFailed() {
return completable.isCompletedExceptionally();
}
@Override
public void handler(Handler> completionHandler) {
Promise completion = Promise.promise();
completion.future().onComplete(completionHandler);
resolve(completion);
}
@Override
public void await() {
try {
completable.get();
} catch (ExecutionException ignore) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Helper.uncheckedThrow(e);
}
}
@Override
public void await(long timeoutMillis) {
try {
completable.get(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (ExecutionException ignore) {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Helper.uncheckedThrow(e);
} catch (TimeoutException e) {
Helper.uncheckedThrow(new TimeoutException("Timed out"));
}
}
@Override
public void awaitSuccess() {
try {
completable.get();
} catch (ExecutionException result) {
Helper.uncheckedThrow(result.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Helper.uncheckedThrow(e);
}
}
@Override
public void awaitSuccess(long timeoutMillis) {
try {
completable.get(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (ExecutionException result) {
Helper.uncheckedThrow(result.getCause());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Helper.uncheckedThrow(e);
} catch (TimeoutException e) {
Helper.uncheckedThrow(new TimeoutException("Timed out"));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy