/[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 454 - (hide annotations) (download)
Tue Oct 20 11:08:53 2009 UTC (14 years, 8 months ago) by torben
Original Path: CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
File size: 3787 byte(s)
No need for the notifier
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 452 @Test public void forcedResetTest() throws Exception {
89 torben 449 try{
90     cb.invoke( new FailingInvocation() );
91     } catch (IOException e) {}
92    
93     try{
94     cb.invoke( new FailingInvocation() );
95     } catch (IOException e) {}
96    
97 torben 453 cb.reset();
98    
99 torben 452 assertTrue( cb.isClosed() );
100 torben 453 assertTrue( cb.getFailureCount() == 0 ); //TODO: currently an externally triggered reset doesn't reset failure count - should a forced reset be possible at all ?
101 torben 449 }
102    
103 torben 452
104     @Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception {
105     cb.tripBreaker();
106     assertTrue(cb.isOpen());
107     cb.invoke( new FailingInvocation() );
108     }
109    
110     @Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception {
111     cb.tripBreaker();
112     assertTrue(cb.isOpen());
113     cb.invoke( new SucceedingInvocation() );
114     }
115    
116     @Test public void halfOpen1() throws Exception {
117     cb.tripBreaker();
118    
119     assertTrue( cb.isOpen() );
120     Thread.sleep(DELAY*2);
121    
122     cb.invoke( new SucceedingInvocation() );
123     assertTrue( cb.isClosed() );
124     }
125    
126     @Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception {
127     cb.tripBreaker();
128    
129     assertTrue( cb.isOpen() );
130     Thread.sleep(DELAY*2);
131    
132     cb.invoke( new FailingInvocation() ); //in half open this will cause a CircuitBreakerException
133     }
134    
135     @Test public void halfOpen3() throws Exception {
136     cb.tripBreaker();
137    
138     assertTrue( cb.isOpen() );
139     Thread.sleep(DELAY*2);
140    
141     try{
142     cb.invoke( new FailingInvocation() );
143     } catch (CircuitBreakerException e) {}
144    
145     assertTrue( cb.isOpen() );
146     }
147    
148    
149 torben 445 }

  ViewVC Help
Powered by ViewVC 1.1.20