use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class DecoratorsTest method shouldThrowTimeoutExceptionAndPropagateMDCContext.
@Test
public void shouldThrowTimeoutExceptionAndPropagateMDCContext() {
TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
MDC.put("key", "ValueShouldPropagateThreadBoundary");
MDC.put("key2", "value2");
final Map<String, String> contextMap = MDC.getCopyOfContextMap();
ContextAwareScheduledThreadPoolExecutor scheduledThreadPool = ContextAwareScheduledThreadPoolExecutor.newScheduledThreadPool().corePoolSize(1).build();
CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
assertThat(Thread.currentThread().getName()).isEqualTo("bulkhead-helloBackend-1");
Thread.sleep(1000);
return "Bla";
}).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, scheduledThreadPool).withCircuitBreaker(circuitBreaker).get();
final CompletableFuture<String> completableFuture = completionStage.toCompletableFuture().exceptionally(throwable -> {
if (throwable != null) {
assertThat(Thread.currentThread().getName()).isEqualTo("ContextAwareScheduledThreadPool-1");
assertThat(MDC.getCopyOfContextMap()).hasSize(2).containsExactlyEntriesOf(contextMap);
return MDC.getCopyOfContextMap().get("key");
}
return null;
});
waitAtMost(2, TimeUnit.SECONDS).until(matches(() -> assertThat(completableFuture).isCompletedWithValue("ValueShouldPropagateThreadBoundary")));
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class DecoratorsTest method testDecorateCompletionStageWithTimeoutExceptionFallback.
@Test
public void testDecorateCompletionStageWithTimeoutExceptionFallback() throws ExecutionException, InterruptedException {
TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
Thread.sleep(1000);
return "Bla";
}).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, Executors.newSingleThreadScheduledExecutor()).withCircuitBreaker(circuitBreaker).withFallback(TimeoutException.class, (e) -> "Fallback").get();
String result = completionStage.toCompletableFuture().get();
assertThat(result).isEqualTo("Fallback");
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class AbstractTimeLimiterMetrics method registerMetrics.
protected void registerMetrics(MeterRegistry meterRegistry, TimeLimiter timeLimiter, List<Tag> customTags) {
// Remove previous meters before register
removeMetrics(meterRegistry, timeLimiter.getName());
Counter successes = Counter.builder(names.getCallsMetricName()).description("The number of successful calls").tag(TagNames.NAME, timeLimiter.getName()).tag(TagNames.KIND, KIND_SUCCESSFUL).tags(customTags).register(meterRegistry);
Counter failures = Counter.builder(names.getCallsMetricName()).description("The number of failed calls").tag(TagNames.NAME, timeLimiter.getName()).tag(TagNames.KIND, KIND_FAILED).tags(customTags).register(meterRegistry);
Counter timeouts = Counter.builder(names.getCallsMetricName()).description("The number of timed out calls").tag(TagNames.NAME, timeLimiter.getName()).tag(TagNames.KIND, KIND_TIMEOUT).tags(customTags).register(meterRegistry);
timeLimiter.getEventPublisher().onSuccess(event -> successes.increment()).onError(event -> failures.increment()).onTimeout(event -> timeouts.increment());
List<Meter.Id> ids = Arrays.asList(successes.getId(), failures.getId(), timeouts.getId());
meterIdMap.put(timeLimiter.getName(), new HashSet<>(ids));
}
use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class TimeLimiterMetricsTest method shouldRecordSuccesses.
@Test
public void shouldRecordSuccesses() {
TimeLimiter timeLimiter = TimeLimiter.of(TimeLimiterConfig.ofDefaults());
metricRegistry.registerAll(TimeLimiterMetrics.ofTimeLimiter(timeLimiter));
timeLimiter.onSuccess();
timeLimiter.onSuccess();
assertThat(metricRegistry).hasMetricsSize(3);
assertThat(metricRegistry).counter(DEFAULT_PREFIX + SUCCESSFUL).hasValue(2L);
assertThat(metricRegistry).counter(DEFAULT_PREFIX + FAILED).hasValue(0L);
assertThat(metricRegistry).counter(DEFAULT_PREFIX + TIMEOUT).hasValue(0L);
}
use of io.github.resilience4j.timelimiter.TimeLimiter in project resilience4j by resilience4j.
the class TimeLimiterMetricsTest method given.
@Override
protected TimeLimiter given(String prefix, MetricRegistry metricRegistry) {
TimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry.ofDefaults();
TimeLimiter timeLimiter = timeLimiterRegistry.timeLimiter("testLimit");
metricRegistry.registerAll(TimeLimiterMetrics.ofTimeLimiterRegistry(prefix, timeLimiterRegistry));
return timeLimiter;
}
Aggregations