mirror of
https://github.com/tommyskeff/futur4j.git
synced 2026-01-17 23:16:01 +00:00
catch RejectedExecutionException
This commit is contained in:
@@ -150,9 +150,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
addDirectListener(
|
||||
res -> {
|
||||
try {
|
||||
Runnable runnable = createRunnable(res, promise, task);
|
||||
F future = getExecutor().runSync(runnable);
|
||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
},
|
||||
promise::completeExceptionally
|
||||
);
|
||||
@@ -166,9 +170,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
addDirectListener(
|
||||
res -> {
|
||||
try {
|
||||
Runnable runnable = createRunnable(res, promise, task);
|
||||
F future = getExecutor().runSync(runnable, delay, unit);
|
||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
},
|
||||
promise::completeExceptionally
|
||||
);
|
||||
@@ -251,9 +259,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
addDirectListener(
|
||||
(res) -> {
|
||||
try {
|
||||
Runnable runnable = createRunnable(res, promise, task);
|
||||
F future = getExecutor().runAsync(runnable);
|
||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
},
|
||||
promise::completeExceptionally
|
||||
);
|
||||
@@ -267,9 +279,13 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
Promise<V> promise = getFactory().unresolved();
|
||||
addDirectListener(
|
||||
res -> {
|
||||
try {
|
||||
Runnable runnable = createRunnable(res, promise, task);
|
||||
F future = getExecutor().runAsync(runnable, delay, unit);
|
||||
promise.onCancel((e) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
promise.completeExceptionally(e);
|
||||
}
|
||||
},
|
||||
promise::completeExceptionally
|
||||
);
|
||||
@@ -355,7 +371,11 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
private void callListener(PromiseListener<T> listener, PromiseCompletion<T> ctx) {
|
||||
if (listener instanceof AsyncPromiseListener) {
|
||||
try {
|
||||
getExecutor().runAsync(() -> callListenerNow(listener, ctx));
|
||||
} catch (RejectedExecutionException ignored) {
|
||||
|
||||
}
|
||||
} else {
|
||||
callListenerNow(listener, ctx);
|
||||
}
|
||||
@@ -407,8 +427,14 @@ public abstract class AbstractPromise<T, F> implements Promise<T> {
|
||||
|
||||
@Override
|
||||
public @NotNull Promise<T> maxWaitTime(long time, @NotNull TimeUnit unit) {
|
||||
F future = getExecutor().runAsync(() -> completeExceptionally(new TimeoutException("Promise stopped waiting after " + time + " " + unit)), time, unit);
|
||||
return addListener((_v) -> getExecutor().cancel(future));
|
||||
try {
|
||||
Exception e = new TimeoutException("Promise stopped waiting after " + time + " " + unit);
|
||||
F future = getExecutor().runAsync(() -> completeExceptionally(e), time, unit);
|
||||
return addDirectListener((_v) -> getExecutor().cancel(future));
|
||||
} catch (RejectedExecutionException e) {
|
||||
completeExceptionally(e);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleCompletion(@NotNull PromiseCompletion<T> ctx) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package dev.tommyjs.futur;
|
||||
import dev.tommyjs.futur.executor.PromiseExecutor;
|
||||
import dev.tommyjs.futur.executor.SinglePoolExecutor;
|
||||
import dev.tommyjs.futur.impl.SimplePromiseFactory;
|
||||
import dev.tommyjs.futur.promise.Promise;
|
||||
import dev.tommyjs.futur.promise.PromiseFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
@@ -12,16 +13,14 @@ import reactor.core.publisher.Mono;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public final class PromiseTests {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(PromiseTests.class);
|
||||
private final PromiseExecutor<Future<?>> executor = SinglePoolExecutor.create(5);
|
||||
private final PromiseFactory pfac = new SimplePromiseFactory<>(executor, logger);
|
||||
private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
|
||||
private final PromiseFactory pfac = new SimplePromiseFactory<>(new SinglePoolExecutor(executor), logger);
|
||||
|
||||
@Test
|
||||
public void testMono() {
|
||||
@@ -36,6 +35,17 @@ public final class PromiseTests {
|
||||
assert resolved.getCompletion().getResult() == value;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShutdown() {
|
||||
executor.close();
|
||||
Promise<?> promise = pfac.resolve(null).thenSupplyAsync(() -> null);
|
||||
try {
|
||||
promise.await();
|
||||
} catch (RuntimeException e) {
|
||||
assert e.getCause() instanceof RejectedExecutionException;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorCancellation() throws InterruptedException {
|
||||
var finished = new AtomicBoolean();
|
||||
|
||||
Reference in New Issue
Block a user