package org.skife.muckery.circuits;

import net.jodah.failsafe.AsyncFailsafe;
import net.jodah.failsafe.CircuitBreaker;
import net.jodah.failsafe.CircuitBreakerOpenException;
import net.jodah.failsafe.Failsafe;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class FailsafeTest {
    private Service service;
    private ScheduledExecutorService clock = Executors.newScheduledThreadPool(2);

    @Before
    public void setUp() throws Exception {
        service = new Service();
    }

    @After
    public void tearDown() throws Exception {
        service.close();
    }

    @Test
    public void testFoo() throws Exception {
        CircuitBreaker cb = new CircuitBreaker().failOn(RuntimeException.class)
                                                .withDelay(1, TimeUnit.SECONDS)
                                                .withFailureThreshold(1, 1);

        AsyncFailsafe<?> fs = Failsafe.with(cb)
                                      .with(clock)
                                      .withFallback(Failsafer.bounce());

        CompletableFuture<String> f = fs.future(() -> service.execute(() -> "hello"));
        f.get();
        assertThat(f).isCompletedWithValue("hello");

        f = fs.future(() -> service.execute(() -> {
            throw new RuntimeException("NooOoOooO");
        }));

        assertThatThrownBy(f::get).hasCauseInstanceOf(RuntimeException.class);

        assertThat(cb.getState()).isEqualTo(CircuitBreaker.State.OPEN);
        f = fs.future(() -> service.execute(() -> "yes"));

        assertThatThrownBy(f::get).hasCauseInstanceOf(CircuitBreakerOpenException.class);
    }
}

@@ -1527,16 +1527,90 @@
 hello%22);
+%0A        assertThat(cb.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
 %0A%0A      
@@ -1717,25 +1717,24 @@
        %7D));%0A
-%0A
         asse
@@ -1798,18 +1798,86 @@
 n.class)
-;
 %0A
+                                  .hasMessageContaining(%22NooOoOooO%22);
 %0A       
@@ -1941,16 +1941,17 @@
 .OPEN);%0A
+%0A
         
@@ -1997,25 +1997,24 @@
 -%3E %22yes%22));%0A
-%0A
         asse

add an assertion