9 |
import org.junit.Before; |
import org.junit.Before; |
10 |
import org.junit.Test; |
import org.junit.Test; |
11 |
|
|
12 |
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
13 |
import dk.thoerup.curcuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
14 |
import dk.thoerup.curcuitbreaker.CircuitInvocation; |
import dk.thoerup.circuitbreaker.config.StaticConfig; |
15 |
import dk.thoerup.curcuitbreaker.notification.SystemOutNotifier; |
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
public class TestCircuitBreaker { |
public class TestCircuitBreaker { |
20 |
public static final int DELAY = 50; |
public static final int DELAY = 50; |
21 |
public static final int THRESHOLD = 2; |
public static final int THRESHOLD = 2; |
22 |
|
|
|
class SucceedingInvocation implements CircuitInvocation { |
|
|
public Object proceed() throws Exception { |
|
|
return "OK"; |
|
|
} |
|
|
} |
|
|
|
|
|
class FailingInvocation implements CircuitInvocation { |
|
|
public Object proceed() throws Exception { |
|
|
throw new IOException("Error"); |
|
|
} |
|
|
} |
|
|
|
|
23 |
|
|
24 |
CircuitBreaker cb; |
CircuitBreaker cb; |
25 |
|
|
26 |
@Before public void setup() { |
@Before public void setup() { |
27 |
cb = new CircuitBreaker("test", THRESHOLD, DELAY); |
cb = new CircuitBreaker("test", new StaticConfig(THRESHOLD, DELAY) ); |
|
cb.setNotifier( new SystemOutNotifier() ); |
|
28 |
} |
} |
29 |
|
|
30 |
@Test public void defaultState() { |
@Test public void defaultState() { |
74 |
assertTrue( cb.isOpen() ); |
assertTrue( cb.isOpen() ); |
75 |
} |
} |
76 |
|
|
77 |
|
@Test public void forcedTrip() { |
78 |
|
assertTrue( cb.isClosed() ); |
79 |
|
cb.tripBreaker(); |
80 |
|
assertTrue( cb.isOpen() ); |
81 |
|
} |
82 |
|
|
83 |
@Test public void forcedResetTest() throws Exception { |
@Test public void forcedResetTest() throws Exception { |
84 |
try{ |
try{ |
85 |
cb.invoke( new FailingInvocation() ); |
cb.invoke( new FailingInvocation() ); |
92 |
cb.reset(); |
cb.reset(); |
93 |
|
|
94 |
assertTrue( cb.isClosed() ); |
assertTrue( cb.isClosed() ); |
95 |
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 ); |
96 |
} |
} |
97 |
|
|
98 |
|
|
124 |
assertTrue( cb.isOpen() ); |
assertTrue( cb.isOpen() ); |
125 |
Thread.sleep(DELAY*2); |
Thread.sleep(DELAY*2); |
126 |
|
|
127 |
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 |
128 |
} |
} |
129 |
|
|
130 |
@Test public void halfOpen3() throws Exception { |
@Test public void halfOpen3() throws Exception { |
134 |
Thread.sleep(DELAY*2); |
Thread.sleep(DELAY*2); |
135 |
|
|
136 |
try{ |
try{ |
137 |
cb.invoke( new FailingInvocation() ); |
cb.invoke( new FailingInvocation() ); |
138 |
} catch (CircuitBreakerException e) {} |
} catch (CircuitBreakerException e) {} |
139 |
|
|
140 |
assertTrue( cb.isOpen() ); |
assertTrue( cb.isOpen() ); //after failing in half-open go back to open |
141 |
} |
} |
142 |
|
|
143 |
|
|