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