1 |
package dk.thoerup.circuitbreaker;
|
2 |
|
3 |
|
4 |
import org.junit.*;
|
5 |
import static org.junit.Assert.*;
|
6 |
import dk.thoerup.curcuitbreaker.*;
|
7 |
import java.io.IOException;
|
8 |
|
9 |
|
10 |
public class TestCircuitBreaker {
|
11 |
class SucceedingInvocation implements CircuitInvocation {
|
12 |
public Object proceed() throws Exception {
|
13 |
return "OK";
|
14 |
}
|
15 |
}
|
16 |
|
17 |
class FailingInvocation implements CircuitInvocation {
|
18 |
public Object proceed() throws Exception {
|
19 |
throw new IOException("Error");
|
20 |
}
|
21 |
}
|
22 |
|
23 |
CircuitBreaker cb;
|
24 |
|
25 |
@Before public void setup() {
|
26 |
cb = new CircuitBreaker("test",2,500);
|
27 |
}
|
28 |
|
29 |
@Test public void simpleTest() throws Throwable {
|
30 |
String retval = (String) cb.invoke( new SucceedingInvocation() );
|
31 |
assertEquals(retval, "OK");
|
32 |
assertTrue(cb.getFailureCount() == 0);
|
33 |
}
|
34 |
|
35 |
@Test(expected=IOException.class) public void simpleFailingTest() throws Throwable {
|
36 |
cb.invoke( new FailingInvocation() );
|
37 |
}
|
38 |
|
39 |
@Test public void failingTest() {
|
40 |
try {
|
41 |
cb.invoke( new FailingInvocation() );
|
42 |
}catch (Throwable t) {}
|
43 |
assertTrue(cb.getFailureCount() == 1);
|
44 |
}
|
45 |
|
46 |
@Test public void failAndResetTest() throws Throwable {
|
47 |
try {
|
48 |
cb.invoke( new FailingInvocation() );
|
49 |
}catch (Throwable t) {}
|
50 |
|
51 |
cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed
|
52 |
|
53 |
assertTrue(cb.isClosed());
|
54 |
assertTrue(cb.getFailureCount() == 0);
|
55 |
}
|
56 |
|
57 |
@Test public void normalOpenTest() throws Throwable {
|
58 |
try{
|
59 |
cb.invoke( new FailingInvocation() );
|
60 |
} catch (IOException e) {}
|
61 |
|
62 |
try{
|
63 |
cb.invoke( new FailingInvocation() );
|
64 |
} catch (IOException e) {}
|
65 |
|
66 |
assertTrue( cb.isOpen() );
|
67 |
}
|
68 |
|
69 |
@Test public void forcedResetTest() throws Throwable {
|
70 |
try{
|
71 |
cb.invoke( new FailingInvocation() );
|
72 |
} catch (IOException e) {}
|
73 |
|
74 |
try{
|
75 |
cb.invoke( new FailingInvocation() );
|
76 |
} catch (IOException e) {}
|
77 |
|
78 |
cb.reset();
|
79 |
|
80 |
assertTrue( cb.isClosed() );
|
81 |
}
|
82 |
|
83 |
}
|