/[projects]/miscJava/CircuitBreaker/src/test/java/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
ViewVC logotype

Annotation of /miscJava/CircuitBreaker/src/test/java/dk/thoerup/circuitbreaker/TestCircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 467 - (hide annotations) (download)
Thu Oct 22 06:01:35 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
File size: 3528 byte(s)
Rename package
1 torben 445 package dk.thoerup.circuitbreaker;
2    
3    
4 torben 452 import static org.junit.Assert.assertEquals;
5     import static org.junit.Assert.assertTrue;
6    
7 torben 445 import java.io.IOException;
8    
9 torben 452 import org.junit.Before;
10     import org.junit.Test;
11 torben 445
12 torben 467 import dk.thoerup.circuitbreaker.CircuitBreaker;
13     import dk.thoerup.circuitbreaker.CircuitBreakerException;
14 torben 452
15    
16 torben 454
17 torben 464
18 torben 445 public class TestCircuitBreaker {
19 torben 452 public static final int DELAY = 50;
20     public static final int THRESHOLD = 2;
21    
22 torben 445
23     CircuitBreaker cb;
24    
25     @Before public void setup() {
26 torben 452 cb = new CircuitBreaker("test", THRESHOLD, DELAY);
27 torben 445 }
28    
29 torben 452 @Test public void defaultState() {
30     assertTrue(cb.isClosed());
31     assertTrue(cb.getFailureCount() == 0);
32     }
33    
34     @Test public void simpleTest() throws Exception {
35 torben 445 String retval = (String) cb.invoke( new SucceedingInvocation() );
36     assertEquals(retval, "OK");
37 torben 452 assertTrue( cb.isClosed() );
38 torben 445 assertTrue(cb.getFailureCount() == 0);
39     }
40    
41 torben 452 @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
42 torben 445 cb.invoke( new FailingInvocation() );
43     }
44    
45 torben 449 @Test public void failingTest() {
46 torben 445 try {
47     cb.invoke( new FailingInvocation() );
48 torben 452 }catch (Exception e) {}
49 torben 445 assertTrue(cb.getFailureCount() == 1);
50 torben 452 assertTrue(cb.isClosed());
51 torben 445 }
52    
53 torben 452 @Test public void failAndResetTest() throws Exception {
54 torben 449 try {
55     cb.invoke( new FailingInvocation() );
56 torben 452 }catch (Exception e) {}
57 torben 449
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 torben 452 @Test public void normalOpenTest() throws Exception {
65 torben 445 try{
66     cb.invoke( new FailingInvocation() );
67     } catch (IOException e) {}
68    
69     try{
70     cb.invoke( new FailingInvocation() );
71     } catch (IOException e) {}
72    
73 torben 449 assertTrue( cb.isOpen() );
74 torben 445 }
75 torben 449
76 torben 455 @Test public void forcedTrip() {
77     assertTrue( cb.isClosed() );
78     cb.tripBreaker();
79     assertTrue( cb.isOpen() );
80     }
81    
82 torben 452 @Test public void forcedResetTest() throws Exception {
83 torben 449 try{
84     cb.invoke( new FailingInvocation() );
85     } catch (IOException e) {}
86    
87     try{
88     cb.invoke( new FailingInvocation() );
89     } catch (IOException e) {}
90    
91 torben 453 cb.reset();
92    
93 torben 452 assertTrue( cb.isClosed() );
94 torben 456 assertTrue( cb.getFailureCount() == 0 );
95 torben 449 }
96    
97 torben 452
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 torben 459 cb.invoke( new FailingInvocation() ); //in half-open this will cause a CircuitBreakerException, as if it was in open mode
127 torben 452 }
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 torben 459 cb.invoke( new FailingInvocation() );
137 torben 452 } catch (CircuitBreakerException e) {}
138    
139 torben 459 assertTrue( cb.isOpen() ); //after failing in half-open go back to open
140 torben 452 }
141    
142    
143 torben 445 }

  ViewVC Help
Powered by ViewVC 1.1.20