Search in sources :

Example 6 with BulkheadConfig

use of io.github.resilience4j.bulkhead.BulkheadConfig in project resilience4j by resilience4j.

the class SemaphoreBulkheadTest method testEntryTimeout.

@Test
public void testEntryTimeout() {
    // given
    BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(1).maxWaitTime(10).build();
    SemaphoreBulkhead bulkhead = new SemaphoreBulkhead("test", config);
    // consume the permit
    bulkhead.isCallPermitted();
    // when
    boolean entered = bulkhead.tryEnterBulkhead();
    // then
    assertThat(entered).isFalse();
}
Also used : BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Test(org.junit.Test)

Example 7 with BulkheadConfig

use of io.github.resilience4j.bulkhead.BulkheadConfig in project resilience4j by resilience4j.

the class SemaphoreBulkheadTest method changeWaitingTimeWhileOneThreadIsWaitingForPermission.

@Test
public void changeWaitingTimeWhileOneThreadIsWaitingForPermission() {
    BulkheadConfig originalConfig = BulkheadConfig.custom().maxConcurrentCalls(1).maxWaitTime(500000).build();
    SemaphoreBulkhead bulkhead = new SemaphoreBulkhead("test", originalConfig);
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
    bulkhead.isCallPermitted();
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(0);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(1);
    Thread bulkheadThread = new Thread(() -> {
        bulkhead.isCallPermitted();
        bulkhead.onComplete();
    });
    bulkheadThread.setDaemon(true);
    bulkheadThread.start();
    await().atMost(1, SECONDS).until(() -> bulkheadThread.getState().equals(TIMED_WAITING));
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(0);
    BulkheadConfig newConfig = BulkheadConfig.custom().maxConcurrentCalls(1).maxWaitTime(0).build();
    bulkhead.changeConfig(newConfig);
    // main thread is not blocked
    assertThat(bulkhead.tryEnterBulkhead()).isFalse();
    // previously blocked thread is still waiting
    await().atMost(1, SECONDS).until(() -> bulkheadThread.getState().equals(TIMED_WAITING));
}
Also used : BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Test(org.junit.Test)

Example 8 with BulkheadConfig

use of io.github.resilience4j.bulkhead.BulkheadConfig in project resilience4j by resilience4j.

the class SemaphoreBulkheadTest method changeWaitTimeInIdleState.

@Test
public void changeWaitTimeInIdleState() {
    BulkheadConfig originalConfig = BulkheadConfig.custom().maxConcurrentCalls(3).maxWaitTime(5000).build();
    SemaphoreBulkhead bulkhead = new SemaphoreBulkhead("test", originalConfig);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(3);
    assertThat(bulkhead.getBulkheadConfig().getMaxWaitTime()).isEqualTo(5000);
    BulkheadConfig newConfig = BulkheadConfig.custom().maxConcurrentCalls(3).maxWaitTime(3000).build();
    bulkhead.changeConfig(newConfig);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(3);
    assertThat(bulkhead.getBulkheadConfig().getMaxWaitTime()).isEqualTo(3000);
    newConfig = BulkheadConfig.custom().maxConcurrentCalls(3).maxWaitTime(7000).build();
    bulkhead.changeConfig(newConfig);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(3);
    assertThat(bulkhead.getBulkheadConfig().getMaxWaitTime()).isEqualTo(7000);
    bulkhead.changeConfig(newConfig);
}
Also used : BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Test(org.junit.Test)

Example 9 with BulkheadConfig

use of io.github.resilience4j.bulkhead.BulkheadConfig in project resilience4j by resilience4j.

the class SemaphoreBulkheadTest method changePermissionsCountWhileOneThreadIsWaitingForPermission.

@Test
public void changePermissionsCountWhileOneThreadIsWaitingForPermission() {
    BulkheadConfig originalConfig = BulkheadConfig.custom().maxConcurrentCalls(1).maxWaitTime(500000).build();
    SemaphoreBulkhead bulkhead = new SemaphoreBulkhead("test", originalConfig);
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
    bulkhead.isCallPermitted();
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(0);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(1);
    Thread bulkheadThread = new Thread(() -> {
        bulkhead.isCallPermitted();
        bulkhead.onComplete();
    });
    bulkheadThread.setDaemon(true);
    bulkheadThread.start();
    await().atMost(1, SECONDS).until(() -> bulkheadThread.getState().equals(TIMED_WAITING));
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(0);
    BulkheadConfig newConfig = BulkheadConfig.custom().maxConcurrentCalls(2).maxWaitTime(500000).build();
    bulkhead.changeConfig(newConfig);
    await().atMost(1, SECONDS).until(() -> bulkheadThread.getState().equals(TERMINATED));
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(2);
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
}
Also used : BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Test(org.junit.Test)

Example 10 with BulkheadConfig

use of io.github.resilience4j.bulkhead.BulkheadConfig in project resilience4j by resilience4j.

the class SemaphoreBulkheadTest method changePermissionsCountWhileOneThreadIsRunningWithThisPermission.

@SuppressWarnings("Duplicates")
@Test
public void changePermissionsCountWhileOneThreadIsRunningWithThisPermission() {
    BulkheadConfig originalConfig = BulkheadConfig.custom().maxConcurrentCalls(1).maxWaitTime(0).build();
    SemaphoreBulkhead bulkhead = new SemaphoreBulkhead("test", originalConfig);
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
    AtomicBoolean bulkheadThreadTrigger = new AtomicBoolean(true);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(1);
    Thread bulkheadThread = new Thread(() -> {
        bulkhead.isCallPermitted();
        while (bulkheadThreadTrigger.get()) {
            Thread.yield();
        }
        bulkhead.onComplete();
    });
    bulkheadThread.setDaemon(true);
    bulkheadThread.start();
    await().atMost(1, SECONDS).until(() -> bulkheadThread.getState().equals(RUNNABLE));
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(0);
    assertThat(bulkhead.tryEnterBulkhead()).isFalse();
    BulkheadConfig newConfig = BulkheadConfig.custom().maxConcurrentCalls(2).maxWaitTime(0).build();
    bulkhead.changeConfig(newConfig);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(2);
    assertThat(bulkhead.getBulkheadConfig().getMaxWaitTime()).isEqualTo(0);
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
    assertThat(bulkhead.tryEnterBulkhead()).isTrue();
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(0);
    assertThat(bulkhead.tryEnterBulkhead()).isFalse();
    Thread changerThread = new Thread(() -> {
        bulkhead.changeConfig(BulkheadConfig.custom().maxConcurrentCalls(1).maxWaitTime(0).build());
    });
    changerThread.setDaemon(true);
    changerThread.start();
    await().atMost(1, SECONDS).until(() -> changerThread.getState().equals(WAITING));
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(2);
    bulkheadThreadTrigger.set(false);
    await().atMost(1, SECONDS).until(() -> bulkheadThread.getState().equals(TERMINATED));
    await().atMost(1, SECONDS).until(() -> changerThread.getState().equals(TERMINATED));
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(0);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(1);
    bulkhead.onComplete();
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(1);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Test(org.junit.Test)

Aggregations

BulkheadConfig (io.github.resilience4j.bulkhead.BulkheadConfig)11 Test (org.junit.Test)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)1 BulkheadEvent (io.github.resilience4j.bulkhead.event.BulkheadEvent)1 Before (org.junit.Before)1