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