/[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 456 - (hide annotations) (download)
Tue Oct 20 20:42:36 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
File size: 3792 byte(s)
When doing a reset, make sure to also reset ClosedState's fail count
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 452 import dk.thoerup.curcuitbreaker.CircuitBreaker;
13     import dk.thoerup.curcuitbreaker.CircuitBreakerException;
14     import dk.thoerup.curcuitbreaker.CircuitInvocation;
15    
16    
17 torben 454
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 class SucceedingInvocation implements CircuitInvocation {
23     public Object proceed() throws Exception {
24     return "OK";
25     }
26     }
27    
28     class FailingInvocation implements CircuitInvocation {
29     public Object proceed() throws Exception {
30     throw new IOException("Error");
31     }
32     }
33    
34 torben 452
35 torben 445 CircuitBreaker cb;
36    
37     @Before public void setup() {
38 torben 452 cb = new CircuitBreaker("test", THRESHOLD, DELAY);
39 torben 445 }
40    
41 torben 452 @Test public void defaultState() {
42     assertTrue(cb.isClosed());
43     assertTrue(cb.getFailureCount() == 0);
44     }
45    
46     @Test public void simpleTest() throws Exception {
47 torben 445 String retval = (String) cb.invoke( new SucceedingInvocation() );
48     assertEquals(retval, "OK");
49 torben 452 assertTrue( cb.isClosed() );
50 torben 445 assertTrue(cb.getFailureCount() == 0);
51     }
52    
53 torben 452 @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
54 torben 445 cb.invoke( new FailingInvocation() );
55     }
56    
57 torben 449 @Test public void failingTest() {
58 torben 445 try {
59     cb.invoke( new FailingInvocation() );
60 torben 452 }catch (Exception e) {}
61 torben 445 assertTrue(cb.getFailureCount() == 1);
62 torben 452 assertTrue(cb.isClosed());
63 torben 445 }
64    
65 torben 452 @Test public void failAndResetTest() throws Exception {
66 torben 449 try {
67     cb.invoke( new FailingInvocation() );
68 torben 452 }catch (Exception e) {}
69 torben 449
70     cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed
71    
72     assertTrue(cb.isClosed());
73     assertTrue(cb.getFailureCount() == 0);
74     }
75    
76 torben 452 @Test public void normalOpenTest() throws Exception {
77 torben 445 try{
78     cb.invoke( new FailingInvocation() );
79     } catch (IOException e) {}
80    
81     try{
82     cb.invoke( new FailingInvocation() );
83     } catch (IOException e) {}
84    
85 torben 449 assertTrue( cb.isOpen() );
86 torben 445 }
87 torben 449
88 torben 455 @Test public void forcedTrip() {
89     assertTrue( cb.isClosed() );
90     cb.tripBreaker();
91     assertTrue( cb.isOpen() );
92     }
93    
94 torben 452 @Test public void forcedResetTest() throws Exception {
95 torben 449 try{
96     cb.invoke( new FailingInvocation() );
97     } catch (IOException e) {}
98    
99     try{
100     cb.invoke( new FailingInvocation() );
101     } catch (IOException e) {}
102    
103 torben 453 cb.reset();
104    
105 torben 452 assertTrue( cb.isClosed() );
106 torben 456 assertTrue( cb.getFailureCount() == 0 );
107 torben 449 }
108    
109 torben 452
110     @Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception {
111     cb.tripBreaker();
112     assertTrue(cb.isOpen());
113     cb.invoke( new FailingInvocation() );
114     }
115    
116     @Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception {
117     cb.tripBreaker();
118     assertTrue(cb.isOpen());
119     cb.invoke( new SucceedingInvocation() );
120     }
121    
122     @Test public void halfOpen1() throws Exception {
123     cb.tripBreaker();
124    
125     assertTrue( cb.isOpen() );
126     Thread.sleep(DELAY*2);
127    
128     cb.invoke( new SucceedingInvocation() );
129     assertTrue( cb.isClosed() );
130     }
131    
132     @Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception {
133     cb.tripBreaker();
134    
135     assertTrue( cb.isOpen() );
136     Thread.sleep(DELAY*2);
137    
138     cb.invoke( new FailingInvocation() ); //in half open this will cause a CircuitBreakerException
139     }
140    
141     @Test public void halfOpen3() throws Exception {
142     cb.tripBreaker();
143    
144     assertTrue( cb.isOpen() );
145     Thread.sleep(DELAY*2);
146    
147     try{
148     cb.invoke( new FailingInvocation() );
149     } catch (CircuitBreakerException e) {}
150    
151     assertTrue( cb.isOpen() );
152     }
153    
154    
155 torben 445 }

  ViewVC Help
Powered by ViewVC 1.1.20