1 |
package dk.thoerup.circuitbreaker; |
package dk.thoerup.circuitbreaker; |
2 |
|
|
3 |
|
|
4 |
import org.junit.*; |
import static org.junit.Assert.assertEquals; |
5 |
import static org.junit.Assert.*; |
import static org.junit.Assert.assertTrue; |
6 |
import dk.thoerup.curcuitbreaker.*; |
|
7 |
import java.io.IOException; |
import java.io.IOException; |
8 |
|
|
9 |
|
import org.junit.Before; |
10 |
|
import org.junit.Test; |
11 |
|
|
12 |
|
import dk.thoerup.circuitbreaker.CircuitBreaker; |
13 |
|
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
14 |
|
import dk.thoerup.circuitbreaker.config.StaticConfig; |
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
public class TestCircuitBreaker { |
public class TestCircuitBreaker { |
20 |
class SucceedingInvocation implements CircuitInvocation { |
public static final int DELAY = 50; |
21 |
public Object proceed() throws Exception { |
public static final int THRESHOLD = 2; |
|
return "OK"; |
|
|
} |
|
|
} |
|
22 |
|
|
|
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",2,500); |
cb = new CircuitBreaker("test", new StaticConfig(THRESHOLD, DELAY) ); |
28 |
} |
} |
29 |
|
|
30 |
@Test public void simpleTest() throws Throwable { |
@Test public void defaultState() { |
31 |
|
assertTrue(cb.isClosed()); |
32 |
|
assertTrue(cb.getFailureCount() == 0); |
33 |
|
} |
34 |
|
|
35 |
|
@Test public void simpleTest() throws Exception { |
36 |
String retval = (String) cb.invoke( new SucceedingInvocation() ); |
String retval = (String) cb.invoke( new SucceedingInvocation() ); |
37 |
assertEquals(retval, "OK"); |
assertEquals(retval, "OK"); |
38 |
|
assertTrue( cb.isClosed() ); |
39 |
assertTrue(cb.getFailureCount() == 0); |
assertTrue(cb.getFailureCount() == 0); |
40 |
} |
} |
41 |
|
|
42 |
@Test(expected=IOException.class) public void simpleFailingTest() throws Throwable { |
@Test(expected=IOException.class) public void simpleFailingTest() throws Exception { |
43 |
cb.invoke( new FailingInvocation() ); |
cb.invoke( new FailingInvocation() ); |
44 |
} |
} |
45 |
|
|
46 |
@Test public void failingTest() throws Throwable { |
@Test public void failingTest() { |
47 |
try { |
try { |
48 |
cb.invoke( new FailingInvocation() ); |
cb.invoke( new FailingInvocation() ); |
49 |
}catch (Throwable t) {} |
}catch (Exception e) {} |
50 |
assertTrue(cb.getFailureCount() == 1); |
assertTrue(cb.getFailureCount() == 1); |
51 |
|
assertTrue(cb.isClosed()); |
52 |
|
} |
53 |
|
|
54 |
|
@Test public void failAndResetTest() throws Exception { |
55 |
|
try { |
56 |
|
cb.invoke( new FailingInvocation() ); |
57 |
|
}catch (Exception e) {} |
58 |
|
|
59 |
|
cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed |
60 |
|
|
61 |
|
assertTrue(cb.isClosed()); |
62 |
|
assertTrue(cb.getFailureCount() == 0); |
63 |
|
} |
64 |
|
|
65 |
|
@Test public void normalOpenTest() throws Exception { |
66 |
|
try{ |
67 |
|
cb.invoke( new FailingInvocation() ); |
68 |
|
} catch (IOException e) {} |
69 |
|
|
70 |
|
try{ |
71 |
|
cb.invoke( new FailingInvocation() ); |
72 |
|
} catch (IOException e) {} |
73 |
|
|
74 |
|
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 normalOpenTest() throws Throwable { |
@Test public void forcedResetTest() throws Exception { |
84 |
try{ |
try{ |
85 |
cb.invoke( new FailingInvocation() ); |
cb.invoke( new FailingInvocation() ); |
86 |
} catch (IOException e) {} |
} catch (IOException e) {} |
88 |
try{ |
try{ |
89 |
cb.invoke( new FailingInvocation() ); |
cb.invoke( new FailingInvocation() ); |
90 |
} catch (IOException e) {} |
} catch (IOException e) {} |
91 |
|
|
92 |
|
cb.reset(); |
93 |
|
|
94 |
|
assertTrue( cb.isClosed() ); |
95 |
|
assertTrue( cb.getFailureCount() == 0 ); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
assertTrue( cb.getStateName().equalsIgnoreCase("open") ); |
@Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception { |
100 |
|
cb.tripBreaker(); |
101 |
|
assertTrue(cb.isOpen()); |
102 |
|
cb.invoke( new FailingInvocation() ); |
103 |
|
} |
104 |
|
|
105 |
|
@Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception { |
106 |
|
cb.tripBreaker(); |
107 |
|
assertTrue(cb.isOpen()); |
108 |
|
cb.invoke( new SucceedingInvocation() ); |
109 |
} |
} |
110 |
|
|
111 |
|
@Test public void halfOpen1() throws Exception { |
112 |
|
cb.tripBreaker(); |
113 |
|
|
114 |
|
assertTrue( cb.isOpen() ); |
115 |
|
Thread.sleep(DELAY*2); |
116 |
|
|
117 |
|
cb.invoke( new SucceedingInvocation() ); |
118 |
|
assertTrue( cb.isClosed() ); |
119 |
|
} |
120 |
|
|
121 |
|
@Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception { |
122 |
|
cb.tripBreaker(); |
123 |
|
|
124 |
|
assertTrue( cb.isOpen() ); |
125 |
|
Thread.sleep(DELAY*2); |
126 |
|
|
127 |
|
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 { |
131 |
|
cb.tripBreaker(); |
132 |
|
|
133 |
|
assertTrue( cb.isOpen() ); |
134 |
|
Thread.sleep(DELAY*2); |
135 |
|
|
136 |
|
try{ |
137 |
|
cb.invoke( new FailingInvocation() ); |
138 |
|
} catch (CircuitBreakerException e) {} |
139 |
|
|
140 |
|
assertTrue( cb.isOpen() ); //after failing in half-open go back to open |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
} |
} |