package io.github.resilience4j.circuitbreaker.operator; import io.github.resilience4j.circuitbreaker.CallNotPermittedException; import io.reactivex.Completable; import org.junit.Test; import java.io.IOException; import java.util.concurrent.TimeUnit; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.never; /** * Unit test for {@link CompletableCircuitBreaker}. */ public class CompletableCircuitBreakerTest extends BaseCircuitBreakerTest { @Test public void shouldSubscribeToCompletable() { given(circuitBreaker.tryAcquirePermission()).willReturn(true); Completable.complete() .compose(CircuitBreakerOperator.of(circuitBreaker)) .test() .assertSubscribed() .assertComplete(); then(circuitBreaker).should().onSuccess(anyLong(), any(TimeUnit.class)); then(circuitBreaker).should(never()) .onError(anyLong(), any(TimeUnit.class), any(Throwable.class)); } @Test public void shouldPropagateAndMarkError() { given(circuitBreaker.tryAcquirePermission()).willReturn(true); Completable.error(new IOException("BAM!")) .compose(CircuitBreakerOperator.of(circuitBreaker)) .test() .assertSubscribed() .assertError(IOException.class) .assertNotComplete(); then(circuitBreaker).should() .onError(anyLong(), any(TimeUnit.class), any(IOException.class)); then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class)); } @Test public void shouldEmitErrorWithCallNotPermittedException() { given(circuitBreaker.tryAcquirePermission()).willReturn(false); Completable.complete() .compose(CircuitBreakerOperator.of(circuitBreaker)) .test() .assertSubscribed() .assertError(CallNotPermittedException.class) .assertNotComplete(); then(circuitBreaker).should(never()).onSuccess(anyLong(), any(TimeUnit.class)); then(circuitBreaker).should(never()) .onError(anyLong(), any(TimeUnit.class), any(Throwable.class)); } }