Search in sources :

Example 6 with TimeLimiterConfig

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);
}
Also used : TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) Test(org.junit.Test)

Example 7 with TimeLimiterConfig

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);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) Duration(java.time.Duration) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) Test(org.junit.Test)

Example 8 with TimeLimiterConfig

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);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) TimeLimiterRegistry(io.github.resilience4j.timelimiter.TimeLimiterRegistry) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) Test(org.junit.Test)

Example 9 with TimeLimiterConfig

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));
}
Also used : TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) Test(org.junit.Test)

Example 10 with TimeLimiterConfig

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);
}
Also used : HashMap(java.util.HashMap) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) Test(org.junit.Test)

Aggregations

TimeLimiterConfig (io.github.resilience4j.timelimiter.TimeLimiterConfig)10 Test (org.junit.Test)8 TimeLimiter (io.github.resilience4j.timelimiter.TimeLimiter)2 TimeLimiterRegistry (io.github.resilience4j.timelimiter.TimeLimiterRegistry)2 HashMap (java.util.HashMap)2 Duration (java.time.Duration)1