19 |
public static final int DELAY = 50; |
public static final int DELAY = 50; |
20 |
public static final int THRESHOLD = 2; |
public static final int THRESHOLD = 2; |
21 |
|
|
|
class SucceedingInvocation implements CircuitInvocation { |
|
|
public Object proceed() throws Exception { |
|
|
return "OK"; |
|
|
} |
|
|
} |
|
|
|
|
|
class FailingInvocation implements CircuitInvocation { |
|
|
public Object proceed() throws Exception { |
|
|
throw new IOException("Error"); |
|
|
} |
|
|
} |
|
|
|
|
22 |
|
|
23 |
CircuitBreaker cb; |
CircuitBreaker cb; |
24 |
|
|
73 |
assertTrue( cb.isOpen() ); |
assertTrue( cb.isOpen() ); |
74 |
} |
} |
75 |
|
|
76 |
|
@Test public void forcedTrip() { |
77 |
|
assertTrue( cb.isClosed() ); |
78 |
|
cb.tripBreaker(); |
79 |
|
assertTrue( cb.isOpen() ); |
80 |
|
} |
81 |
|
|
82 |
@Test public void forcedResetTest() throws Exception { |
@Test public void forcedResetTest() throws Exception { |
83 |
try{ |
try{ |
84 |
cb.invoke( new FailingInvocation() ); |
cb.invoke( new FailingInvocation() ); |
91 |
cb.reset(); |
cb.reset(); |
92 |
|
|
93 |
assertTrue( cb.isClosed() ); |
assertTrue( cb.isClosed() ); |
94 |
assertTrue( cb.getFailureCount() == 0 ); //TODO: currently an externally triggered reset doesn't reset failure count - should a forced reset be possible at all ? |
assertTrue( cb.getFailureCount() == 0 ); |
95 |
} |
} |
96 |
|
|
97 |
|
|
123 |
assertTrue( cb.isOpen() ); |
assertTrue( cb.isOpen() ); |
124 |
Thread.sleep(DELAY*2); |
Thread.sleep(DELAY*2); |
125 |
|
|
126 |
cb.invoke( new FailingInvocation() ); //in half open this will cause a CircuitBreakerException |
cb.invoke( new FailingInvocation() ); //in half-open this will cause a CircuitBreakerException, as if it was in open mode |
127 |
} |
} |
128 |
|
|
129 |
@Test public void halfOpen3() throws Exception { |
@Test public void halfOpen3() throws Exception { |
133 |
Thread.sleep(DELAY*2); |
Thread.sleep(DELAY*2); |
134 |
|
|
135 |
try{ |
try{ |
136 |
cb.invoke( new FailingInvocation() ); |
cb.invoke( new FailingInvocation() ); |
137 |
} catch (CircuitBreakerException e) {} |
} catch (CircuitBreakerException e) {} |
138 |
|
|
139 |
assertTrue( cb.isOpen() ); |
assertTrue( cb.isOpen() ); //after failing in half-open go back to open |
140 |
} |
} |
141 |
|
|
142 |
|
|