Search in sources :

Example 1 with BulkheadConfig

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

the class SemaphoreBulkheadTest method testTryEnterWithTimeout.

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

Example 2 with BulkheadConfig

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

the class SemaphoreBulkheadTest method changePermissionsConcurrently.

@SuppressWarnings("Duplicates")
@Test
public void changePermissionsConcurrently() {
    BulkheadConfig originalConfig = BulkheadConfig.custom().maxConcurrentCalls(3).maxWaitTime(0).build();
    SemaphoreBulkhead bulkhead = new SemaphoreBulkhead("test", originalConfig);
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(3);
    AtomicBoolean bulkheadThreadTrigger = new AtomicBoolean(true);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(3);
    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(2);
    assertThat(bulkhead.tryEnterBulkhead()).isTrue();
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
    Thread firstChangerThread = new Thread(() -> {
        bulkhead.changeConfig(BulkheadConfig.custom().maxConcurrentCalls(1).maxWaitTime(0).build());
    });
    firstChangerThread.setDaemon(true);
    firstChangerThread.start();
    await().atMost(1, SECONDS).until(() -> firstChangerThread.getState().equals(WAITING));
    Thread secondChangerThread = new Thread(() -> {
        bulkhead.changeConfig(BulkheadConfig.custom().maxConcurrentCalls(4).maxWaitTime(0).build());
    });
    secondChangerThread.setDaemon(true);
    secondChangerThread.start();
    await().atMost(1, SECONDS).until(() -> secondChangerThread.getState().equals(BLOCKED));
    bulkheadThreadTrigger.set(false);
    await().atMost(1, SECONDS).until(() -> bulkheadThread.getState().equals(TERMINATED));
    await().atMost(1, SECONDS).until(() -> firstChangerThread.getState().equals(TERMINATED));
    await().atMost(1, SECONDS).until(() -> secondChangerThread.getState().equals(TERMINATED));
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(4);
    // main thread is still holding
    assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(3);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Test(org.junit.Test)

Example 3 with BulkheadConfig

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

the class SemaphoreBulkheadTest method testEntryInterrupted.

// best effort, no asserts
@Test
public void testEntryInterrupted() {
    // given
    BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(1).maxWaitTime(10000).build();
    final SemaphoreBulkhead bulkhead = new SemaphoreBulkhead("test", config);
    // consume the permit
    bulkhead.isCallPermitted();
    AtomicBoolean entered = new AtomicBoolean(true);
    Thread t = new Thread(() -> {
        entered.set(bulkhead.tryEnterBulkhead());
    });
    // when
    t.start();
    sleep(500);
    t.interrupt();
    sleep(500);
// then
// assertThat(entered.get()).isFalse();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Test(org.junit.Test)

Example 4 with BulkheadConfig

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

the class SemaphoreBulkheadTest method changePermissionsInIdleState.

@Test
public void changePermissionsInIdleState() {
    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(5).maxWaitTime(5000).build();
    bulkhead.changeConfig(newConfig);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(5);
    assertThat(bulkhead.getBulkheadConfig().getMaxWaitTime()).isEqualTo(5000);
    newConfig = BulkheadConfig.custom().maxConcurrentCalls(2).maxWaitTime(5000).build();
    bulkhead.changeConfig(newConfig);
    assertThat(bulkhead.getBulkheadConfig().getMaxConcurrentCalls()).isEqualTo(2);
    assertThat(bulkhead.getBulkheadConfig().getMaxWaitTime()).isEqualTo(5000);
    bulkhead.changeConfig(newConfig);
}
Also used : BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Test(org.junit.Test)

Example 5 with BulkheadConfig

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

the class SemaphoreBulkheadTest method setUp.

@Before
public void setUp() {
    BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(2).maxWaitTime(0).build();
    bulkhead = Bulkhead.of("test", config);
    testSubscriber = RxJava2Adapter.toFlowable(bulkhead.getEventPublisher()).map(BulkheadEvent::getEventType).test();
}
Also used : BulkheadEvent(io.github.resilience4j.bulkhead.event.BulkheadEvent) BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Before(org.junit.Before)

Aggregations

BulkheadConfig (io.github.resilience4j.bulkhead.BulkheadConfig)13 Test (org.junit.Test)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 BulkheadRegistry (io.github.resilience4j.bulkhead.BulkheadRegistry)1 BulkheadEvent (io.github.resilience4j.bulkhead.event.BulkheadEvent)1 ArrayList (java.util.ArrayList)1 QueryCapacityExceededException (org.apache.druid.query.QueryCapacityExceededException)1 Before (org.junit.Before)1