1 |
torben |
463 |
package dk.thoerup.circuitbreaker;
|
2 |
|
|
|
3 |
torben |
1315 |
import static org.junit.Assert.assertTrue;
|
4 |
|
|
|
5 |
torben |
463 |
import java.io.IOException;
|
6 |
|
|
|
7 |
torben |
1315 |
import org.junit.Before;
|
8 |
|
|
import org.junit.Test;
|
9 |
torben |
463 |
|
10 |
torben |
1315 |
import dk.thoerup.circuitbreaker.config.StaticConfig;
|
11 |
torben |
463 |
|
12 |
|
|
public class TestAccountingCircuitBreaker {
|
13 |
|
|
public static final int DELAY = 50;
|
14 |
|
|
public static final int THRESHOLD = 2;
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
AccountingCircuitBreaker acb;
|
18 |
|
|
|
19 |
|
|
@Before public void setup() {
|
20 |
torben |
1315 |
acb = new AccountingCircuitBreaker("test", new StaticConfig(THRESHOLD, DELAY) );
|
21 |
torben |
463 |
}
|
22 |
|
|
|
23 |
|
|
@Test public void initialValues() {
|
24 |
|
|
assertTrue(acb.getBlockCount() == 0);
|
25 |
|
|
assertTrue(acb.getTotalCallCount() == 0);
|
26 |
|
|
assertTrue(acb.getTotalFailureCount() == 0);
|
27 |
|
|
assertTrue(acb.getTripCount() == 0 );
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
@Test public void callCounter() throws Exception{
|
31 |
|
|
acb.invoke( new SucceedingInvocation() );
|
32 |
|
|
assertTrue( acb.getTotalCallCount() == 1);
|
33 |
|
|
|
34 |
|
|
acb.invoke( new SucceedingInvocation() );
|
35 |
|
|
assertTrue( acb.getTotalCallCount() == 2);
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
@Test public void testFailing() throws Exception{
|
39 |
|
|
try{
|
40 |
|
|
acb.invoke( new FailingInvocation() );
|
41 |
|
|
} catch (IOException e) {}
|
42 |
|
|
|
43 |
|
|
assertTrue( acb.getTotalFailureCount() == 1);
|
44 |
|
|
|
45 |
|
|
try{
|
46 |
|
|
acb.invoke( new FailingInvocation() );
|
47 |
|
|
} catch (IOException e) {}
|
48 |
|
|
|
49 |
|
|
assertTrue( acb.getTripCount() == 1 );
|
50 |
|
|
assertTrue( acb.getTotalCallCount() == 2 );
|
51 |
|
|
assertTrue( acb.getTotalFailureCount() == 2);
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
@Test public void testBlockCounter() {
|
55 |
|
|
assertTrue( acb.getBlockCount() == 0);
|
56 |
|
|
|
57 |
|
|
acb.tripBreaker();
|
58 |
|
|
try {
|
59 |
|
|
acb.invoke(new SucceedingInvocation() );
|
60 |
|
|
} catch (Exception e) {}
|
61 |
|
|
|
62 |
|
|
assertTrue( acb.getBlockCount() == 1);
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
}
|
66 |
|
|
}
|