/[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 453 - (hide annotations) (download)
Tue Oct 20 10:48:47 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java
File size: 3897 byte(s)
Marked comment as TODO:
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     import dk.thoerup.curcuitbreaker.notification.SystemOutNotifier;
16    
17    
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     cb.setNotifier( new SystemOutNotifier() );
40 torben 445 }
41    
42 torben 452 @Test public void defaultState() {
43     assertTrue(cb.isClosed());
44     assertTrue(cb.getFailureCount() == 0);
45     }
46    
47     @Test public void simpleTest() throws Exception {
48 torben 445 String retval = (String) cb.invoke( new SucceedingInvocation() );
49     assertEquals(retval, "OK");
50 torben 452 assertTrue( cb.isClosed() );
51 torben 445 assertTrue(cb.getFailureCount() == 0);
52     }
53    
54 torben 452 @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
55 torben 445 cb.invoke( new FailingInvocation() );
56     }
57    
58 torben 449 @Test public void failingTest() {
59 torben 445 try {
60     cb.invoke( new FailingInvocation() );
61 torben 452 }catch (Exception e) {}
62 torben 445 assertTrue(cb.getFailureCount() == 1);
63 torben 452 assertTrue(cb.isClosed());
64 torben 445 }
65    
66 torben 452 @Test public void failAndResetTest() throws Exception {
67 torben 449 try {
68     cb.invoke( new FailingInvocation() );
69 torben 452 }catch (Exception e) {}
70 torben 449
71     cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed
72    
73     assertTrue(cb.isClosed());
74     assertTrue(cb.getFailureCount() == 0);
75     }
76    
77 torben 452 @Test public void normalOpenTest() throws Exception {
78 torben 445 try{
79     cb.invoke( new FailingInvocation() );
80     } catch (IOException e) {}
81    
82     try{
83     cb.invoke( new FailingInvocation() );
84     } catch (IOException e) {}
85    
86 torben 449 assertTrue( cb.isOpen() );
87 torben 445 }
88 torben 449
89 torben 452 @Test public void forcedResetTest() throws Exception {
90 torben 449 try{
91     cb.invoke( new FailingInvocation() );
92     } catch (IOException e) {}
93    
94     try{
95     cb.invoke( new FailingInvocation() );
96     } catch (IOException e) {}
97    
98 torben 453 cb.reset();
99    
100 torben 452 assertTrue( cb.isClosed() );
101 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 ?
102 torben 449 }
103    
104 torben 452
105     @Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception {
106     cb.tripBreaker();
107     assertTrue(cb.isOpen());
108     cb.invoke( new FailingInvocation() );
109     }
110    
111     @Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception {
112     cb.tripBreaker();
113     assertTrue(cb.isOpen());
114     cb.invoke( new SucceedingInvocation() );
115     }
116    
117     @Test public void halfOpen1() throws Exception {
118     cb.tripBreaker();
119    
120     assertTrue( cb.isOpen() );
121     Thread.sleep(DELAY*2);
122    
123     cb.invoke( new SucceedingInvocation() );
124     assertTrue( cb.isClosed() );
125     }
126    
127     @Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception {
128     cb.tripBreaker();
129    
130     assertTrue( cb.isOpen() );
131     Thread.sleep(DELAY*2);
132    
133     cb.invoke( new FailingInvocation() ); //in half open this will cause a CircuitBreakerException
134     }
135    
136     @Test public void halfOpen3() throws Exception {
137     cb.tripBreaker();
138    
139     assertTrue( cb.isOpen() );
140     Thread.sleep(DELAY*2);
141    
142     try{
143     cb.invoke( new FailingInvocation() );
144     } catch (CircuitBreakerException e) {}
145    
146     assertTrue( cb.isOpen() );
147     }
148    
149    
150 torben 445 }

  ViewVC Help
Powered by ViewVC 1.1.20