Skip to content

Commit

Permalink
Unwrap the ExecutionException in TimeLimiter and rethrow the cause at…
Browse files Browse the repository at this point in the history
… decorateFutureSupplier function (#529)
  • Loading branch information
MrRee1995 authored and RobWin committed Jul 6, 2019
1 parent 322ee78 commit 2bbe53c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import io.github.resilience4j.timelimiter.internal.TimeLimiterImpl;

import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.*;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -66,6 +63,15 @@ static <T, F extends Future<T>> Callable<T> decorateFutureSupplier(TimeLimiter t
future.cancel(true);
}
throw e;
} catch (ExecutionException e){
Throwable t = e.getCause();
if (t == null){
throw e;
}
if (t instanceof Error){
throw (Error) t;
}
throw (Exception) t;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ public void shouldReturnResult() throws Exception {
result = timeLimiter.decorateFutureSupplier(supplier).call();
Assertions.assertThat(result).isEqualTo(42);
}

@Test
public void unwrapExecutionException() {
TimeLimiter timeLimiter = TimeLimiter.ofDefaults();
ExecutorService executorService = Executors.newSingleThreadExecutor();

Supplier<Future<Integer>> supplier = () -> executorService.submit(() -> {throw new RuntimeException();});
Callable<Integer> decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);

Try<Integer> decoratedResult = Try.ofCallable(decorated);

Assertions.assertThat(decoratedResult.getCause() instanceof RuntimeException).isTrue();
}
}

0 comments on commit 2bbe53c

Please sign in to comment.