package dk.thoerup.circuitbreaker; import static org.junit.Assert.assertTrue; import java.io.IOException; import org.junit.Before; import org.junit.Test; import dk.thoerup.circuitbreaker.config.StaticConfig; import dk.thoerup.circuitbreaker.statistics.AccountingStatistics; public class TestAccountingCircuitBreaker { public static final int DELAY = 50; public static final int THRESHOLD = 2; AccountingStatistics stats; CircuitBreaker acb; @Before public void setup() { stats = new AccountingStatistics(); acb = new CircuitBreaker("test", new StaticConfig(THRESHOLD, DELAY) ); acb.setStatistics(stats); } @Test public void initialValues() { assertTrue(stats.getBlockCount() == 0); assertTrue(stats.getTotalCallCount() == 0); assertTrue(stats.getTotalFailureCount() == 0); assertTrue(stats.getTripCount() == 0 ); } @Test public void callCounter() throws Exception{ acb.invoke( new SucceedingInvocation() ); assertTrue( stats.getTotalCallCount() == 1); acb.invoke( new SucceedingInvocation() ); assertTrue( stats.getTotalCallCount() == 2); } @Test public void testFailing() throws Exception{ try{ acb.invoke( new FailingInvocation() ); } catch (IOException e) {} assertTrue( stats.getTotalFailureCount() == 1); try{ acb.invoke( new FailingInvocation() ); } catch (IOException e) {} assertTrue( stats.getTripCount() == 1 ); assertTrue( stats.getTotalCallCount() == 2 ); assertTrue( stats.getTotalFailureCount() == 2); } @Test public void testBlockCounter() { assertTrue( stats.getBlockCount() == 0); acb.tripBreaker(); try { acb.invoke(new SucceedingInvocation() ); } catch (Exception e) {} assertTrue( stats.getBlockCount() == 1); } }