use of io.github.resilience4j.timelimiter.TimeLimiterConfig in project resilience4j by resilience4j.
the class InMemoryTimeLimiterRegistryTest method timeLimiterNewWithNullConfigSupplier.
@Test
public void timeLimiterNewWithNullConfigSupplier() {
exception.expect(NullPointerException.class);
exception.expectMessage("Supplier must not be null");
TimeLimiterRegistry registry = new InMemoryTimeLimiterRegistry(config);
registry.timeLimiter("name", (Supplier<TimeLimiterConfig>) null);
}
use of io.github.resilience4j.timelimiter.TimeLimiterConfig in project resilience4j by resilience4j.
the class TimeLimiterTest method shouldThrowTimeoutExceptionAndInvokeCancel.
@Test
public void shouldThrowTimeoutExceptionAndInvokeCancel() throws Exception {
Duration timeoutDuration = Duration.ofSeconds(1);
TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom().timeoutDuration(timeoutDuration).build();
TimeLimiter timeLimiter = TimeLimiter.of(TIME_LIMITER_NAME, timeLimiterConfig);
@SuppressWarnings("unchecked") Future<Integer> mockFuture = (Future<Integer>) mock(Future.class);
Supplier<Future<Integer>> supplier = () -> mockFuture;
given(mockFuture.get(timeoutDuration.toMillis(), TimeUnit.MILLISECONDS)).willThrow(new TimeoutException());
Callable<Integer> decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier);
Try<Integer> decoratedResult = Try.ofCallable(decorated);
assertThat(decoratedResult.isFailure()).isTrue();
assertThat(decoratedResult.getCause()).isInstanceOf(TimeoutException.class);
assertThat(decoratedResult.getCause()).hasMessage(TimeLimiter.createdTimeoutExceptionWithName(TIME_LIMITER_NAME, null).getMessage());
then(mockFuture).should().cancel(true);
}
use of io.github.resilience4j.timelimiter.TimeLimiterConfig in project resilience4j by resilience4j.
the class InMemoryTimeLimiterRegistryTest method timeLimiterPositiveWithSupplier.
@Test
@SuppressWarnings("unchecked")
public void timeLimiterPositiveWithSupplier() {
TimeLimiterRegistry registry = new InMemoryTimeLimiterRegistry(config);
Supplier<TimeLimiterConfig> timeLimiterConfigSupplier = mock(Supplier.class);
given(timeLimiterConfigSupplier.get()).willReturn(config);
TimeLimiter firstTimeLimiter = registry.timeLimiter("test", timeLimiterConfigSupplier);
verify(timeLimiterConfigSupplier, times(1)).get();
TimeLimiter sameAsFirst = registry.timeLimiter("test", timeLimiterConfigSupplier);
verify(timeLimiterConfigSupplier, times(1)).get();
TimeLimiter anotherLimit = registry.timeLimiter("test1", timeLimiterConfigSupplier);
verify(timeLimiterConfigSupplier, times(2)).get();
then(firstTimeLimiter).isEqualTo(sameAsFirst);
then(firstTimeLimiter).isNotEqualTo(anotherLimit);
}
use of io.github.resilience4j.timelimiter.TimeLimiterConfig in project resilience4j by resilience4j.
the class TimeLimiterConfigurationPropertiesTest method testCustomizeTimeLimiterConfig.
@Test
public void testCustomizeTimeLimiterConfig() {
TimeLimiterConfigurationProperties timeLimiterConfigurationProperties = new TimeLimiterConfigurationProperties();
TimeLimiterConfigCustomizer customizer = TimeLimiterConfigCustomizer.of("backend", builder -> builder.timeoutDuration(Duration.ofSeconds(10)));
TimeLimiterConfigurationProperties.InstanceProperties instanceProperties = new TimeLimiterConfigurationProperties.InstanceProperties();
instanceProperties.setTimeoutDuration(Duration.ofSeconds(3));
TimeLimiterConfig config = timeLimiterConfigurationProperties.createTimeLimiterConfig("backend", instanceProperties, new CompositeCustomizer<>(Collections.singletonList(customizer)));
assertThat(config).isNotNull();
assertThat(config.getTimeoutDuration()).isEqualTo(Duration.ofSeconds(10));
}
use of io.github.resilience4j.timelimiter.TimeLimiterConfig in project resilience4j by resilience4j.
the class TimeLimiterConfigurationPropertiesTest method testTimeLimiterProperties.
@Test
public void testTimeLimiterProperties() {
// Given
TimeLimiterConfigurationProperties.InstanceProperties instanceProperties1 = new TimeLimiterConfigurationProperties.InstanceProperties();
instanceProperties1.setTimeoutDuration(Duration.ofSeconds(3));
instanceProperties1.setCancelRunningFuture(true);
instanceProperties1.setEventConsumerBufferSize(200);
TimeLimiterConfigurationProperties.InstanceProperties instanceProperties2 = new TimeLimiterConfigurationProperties.InstanceProperties();
instanceProperties2.setTimeoutDuration(Duration.ofSeconds(5));
instanceProperties2.setCancelRunningFuture(false);
instanceProperties2.setEventConsumerBufferSize(500);
TimeLimiterConfigurationProperties timeLimiterConfigurationProperties = new TimeLimiterConfigurationProperties();
timeLimiterConfigurationProperties.getInstances().put("backend1", instanceProperties1);
timeLimiterConfigurationProperties.getInstances().put("backend2", instanceProperties2);
Map<String, String> tags = new HashMap<>();
tags.put("testKey1", "testKet2");
timeLimiterConfigurationProperties.setTags(tags);
// Then
assertThat(timeLimiterConfigurationProperties.getTags()).isNotEmpty();
assertThat(timeLimiterConfigurationProperties.getInstances().size()).isEqualTo(2);
final TimeLimiterConfig timeLimiter1 = timeLimiterConfigurationProperties.createTimeLimiterConfig("backend1");
final TimeLimiterConfig timeLimiter2 = timeLimiterConfigurationProperties.createTimeLimiterConfig("backend2");
TimeLimiterConfigurationProperties.InstanceProperties instancePropertiesForTimeLimiter1 = timeLimiterConfigurationProperties.getInstances().get("backend1");
assertThat(instancePropertiesForTimeLimiter1.getTimeoutDuration().toMillis()).isEqualTo(3000);
assertThat(timeLimiter1).isNotNull();
assertThat(timeLimiter1.shouldCancelRunningFuture()).isEqualTo(true);
assertThat(timeLimiter2).isNotNull();
assertThat(timeLimiter2.getTimeoutDuration().toMillis()).isEqualTo(5000);
}
Aggregations