1 |
torben |
445 |
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() throws Throwable {
|
40 |
|
|
try {
|
41 |
|
|
cb.invoke( new FailingInvocation() );
|
42 |
|
|
}catch (Throwable t) {}
|
43 |
|
|
assertTrue(cb.getFailureCount() == 1);
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
@Test public void normalOpenTest() throws Throwable {
|
47 |
|
|
try{
|
48 |
|
|
cb.invoke( new FailingInvocation() );
|
49 |
|
|
} catch (IOException e) {}
|
50 |
|
|
|
51 |
|
|
try{
|
52 |
|
|
cb.invoke( new FailingInvocation() );
|
53 |
|
|
} catch (IOException e) {}
|
54 |
|
|
|
55 |
|
|
assertTrue( cb.getStateName().equalsIgnoreCase("open") );
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
}
|