--- CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java 2009/10/20 10:32:19 451 +++ CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java 2009/10/20 10:47:36 452 @@ -1,13 +1,24 @@ package dk.thoerup.circuitbreaker; -import org.junit.*; -import static org.junit.Assert.*; -import dk.thoerup.curcuitbreaker.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import java.io.IOException; +import org.junit.Before; +import org.junit.Test; + +import dk.thoerup.curcuitbreaker.CircuitBreaker; +import dk.thoerup.curcuitbreaker.CircuitBreakerException; +import dk.thoerup.curcuitbreaker.CircuitInvocation; +import dk.thoerup.curcuitbreaker.notification.SystemOutNotifier; + public class TestCircuitBreaker { + public static final int DELAY = 50; + public static final int THRESHOLD = 2; + class SucceedingInvocation implements CircuitInvocation { public Object proceed() throws Exception { return "OK"; @@ -20,33 +31,42 @@ } } + CircuitBreaker cb; @Before public void setup() { - cb = new CircuitBreaker("test",2,500); + cb = new CircuitBreaker("test", THRESHOLD, DELAY); + cb.setNotifier( new SystemOutNotifier() ); + } + + @Test public void defaultState() { + assertTrue(cb.isClosed()); + assertTrue(cb.getFailureCount() == 0); } - @Test public void simpleTest() throws Throwable { + @Test public void simpleTest() throws Exception { String retval = (String) cb.invoke( new SucceedingInvocation() ); assertEquals(retval, "OK"); + assertTrue( cb.isClosed() ); assertTrue(cb.getFailureCount() == 0); } - @Test(expected=IOException.class) public void simpleFailingTest() throws Throwable { + @Test(expected=IOException.class) public void simpleFailingTest() throws Exception { cb.invoke( new FailingInvocation() ); } @Test public void failingTest() { try { cb.invoke( new FailingInvocation() ); - }catch (Throwable t) {} + }catch (Exception e) {} assertTrue(cb.getFailureCount() == 1); + assertTrue(cb.isClosed()); } - @Test public void failAndResetTest() throws Throwable { + @Test public void failAndResetTest() throws Exception { try { cb.invoke( new FailingInvocation() ); - }catch (Throwable t) {} + }catch (Exception e) {} cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed @@ -54,7 +74,7 @@ assertTrue(cb.getFailureCount() == 0); } - @Test public void normalOpenTest() throws Throwable { + @Test public void normalOpenTest() throws Exception { try{ cb.invoke( new FailingInvocation() ); } catch (IOException e) {} @@ -66,7 +86,7 @@ assertTrue( cb.isOpen() ); } - @Test public void forcedResetTest() throws Throwable { + @Test public void forcedResetTest() throws Exception { try{ cb.invoke( new FailingInvocation() ); } catch (IOException e) {} @@ -77,7 +97,54 @@ cb.reset(); - assertTrue( cb.isClosed() ); + assertTrue( cb.isClosed() ); + assertTrue( cb.getFailureCount() == 0 ); //currently an externally triggered reset doesn't reset failure count - should a forced reset be possible at all ? + } + + + @Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception { + cb.tripBreaker(); + assertTrue(cb.isOpen()); + cb.invoke( new FailingInvocation() ); + } + + @Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception { + cb.tripBreaker(); + assertTrue(cb.isOpen()); + cb.invoke( new SucceedingInvocation() ); + } + + @Test public void halfOpen1() throws Exception { + cb.tripBreaker(); + + assertTrue( cb.isOpen() ); + Thread.sleep(DELAY*2); + + cb.invoke( new SucceedingInvocation() ); + assertTrue( cb.isClosed() ); + } + + @Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception { + cb.tripBreaker(); + + assertTrue( cb.isOpen() ); + Thread.sleep(DELAY*2); + + cb.invoke( new FailingInvocation() ); //in half open this will cause a CircuitBreakerException + } + + @Test public void halfOpen3() throws Exception { + cb.tripBreaker(); + + assertTrue( cb.isOpen() ); + Thread.sleep(DELAY*2); + + try{ + cb.invoke( new FailingInvocation() ); + } catch (CircuitBreakerException e) {} + + assertTrue( cb.isOpen() ); } + }