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();
}
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);
}
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();
}
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);
}
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();
}
Aggregations