--- CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java 2009/10/19 13:01:46 445 +++ miscJava/CircuitBreaker/src/test/java/dk/thoerup/circuitbreaker/TestCircuitBreaker.java 2015/03/20 08:58:46 2449 @@ -1,49 +1,86 @@ 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.circuitbreaker.CircuitBreaker; +import dk.thoerup.circuitbreaker.CircuitBreakerException; +import dk.thoerup.circuitbreaker.config.StaticConfig; + + + public class TestCircuitBreaker { - class SucceedingInvocation implements CircuitInvocation { - public Object proceed() throws Exception { - return "OK"; - } - } + public static final int DELAY = 50; + public static final int THRESHOLD = 2; - class FailingInvocation implements CircuitInvocation { - public Object proceed() throws Exception { - throw new IOException("Error"); - } - } CircuitBreaker cb; @Before public void setup() { - cb = new CircuitBreaker("test",2,500); + cb = new CircuitBreaker("test", new StaticConfig(THRESHOLD, DELAY) ); } - @Test public void simpleTest() throws Throwable { + @Test public void defaultState() { + assertTrue(cb.isClosed()); + assertTrue(cb.getFailureCount() == 0); + } + + @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() throws Throwable { + @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 Exception { + try { + cb.invoke( new FailingInvocation() ); + }catch (Exception e) {} + + cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed + + assertTrue(cb.isClosed()); + assertTrue(cb.getFailureCount() == 0); + } + + @Test public void normalOpenTest() throws Exception { + try{ + cb.invoke( new FailingInvocation() ); + } catch (IOException e) {} + + try{ + cb.invoke( new FailingInvocation() ); + } catch (IOException e) {} + + assertTrue( cb.isOpen() ); + } + + @Test public void forcedTrip() { + assertTrue( cb.isClosed() ); + cb.tripBreaker(); + assertTrue( cb.isOpen() ); } - @Test public void normalOpenTest() throws Throwable { + @Test public void forcedResetTest() throws Exception { try{ cb.invoke( new FailingInvocation() ); } catch (IOException e) {} @@ -51,8 +88,57 @@ try{ cb.invoke( new FailingInvocation() ); } catch (IOException e) {} + + cb.reset(); + + assertTrue( cb.isClosed() ); + assertTrue( cb.getFailureCount() == 0 ); + } + - assertTrue( cb.getStateName().equalsIgnoreCase("open") ); + @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, as if it was in open mode + } + + @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() ); //after failing in half-open go back to open + } + + }