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

Diff of /CircuitBreaker/test/dk/thoerup/circuitbreaker/TestCircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 445 by torben, Mon Oct 19 13:01:46 2009 UTC revision 456 by torben, Tue Oct 20 20:42:36 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.circuitbreaker;  package dk.thoerup.circuitbreaker;
2    
3    
4  import org.junit.*;  import static org.junit.Assert.assertEquals;
5  import static org.junit.Assert.*;  import static org.junit.Assert.assertTrue;
6  import dk.thoerup.curcuitbreaker.*;  
7  import java.io.IOException;  import java.io.IOException;
8    
9    import org.junit.Before;
10    import org.junit.Test;
11    
12    import dk.thoerup.curcuitbreaker.CircuitBreaker;
13    import dk.thoerup.curcuitbreaker.CircuitBreakerException;
14    import dk.thoerup.curcuitbreaker.CircuitInvocation;
15    
16    
17    
18  public class TestCircuitBreaker {  public class TestCircuitBreaker {
19            public static final int DELAY = 50;
20            public static final int THRESHOLD = 2;
21            
22          class SucceedingInvocation implements CircuitInvocation {          class SucceedingInvocation implements CircuitInvocation {
23                  public Object proceed() throws Exception {                  public Object proceed() throws Exception {
24                          return "OK";                          return "OK";
# Line 20  public class TestCircuitBreaker { Line 31  public class TestCircuitBreaker {
31                  }                  }
32          }          }
33                    
34            
35          CircuitBreaker cb;          CircuitBreaker cb;
36                    
37          @Before public void setup() {          @Before public void setup() {
38                  cb = new CircuitBreaker("test",2,500);                  cb = new CircuitBreaker("test", THRESHOLD, DELAY);
39          }          }
40                    
41          @Test public void simpleTest() throws Throwable {          @Test public void defaultState() {
42                    assertTrue(cb.isClosed());
43                    assertTrue(cb.getFailureCount() == 0);
44            }
45            
46            @Test public void simpleTest() throws Exception {
47                  String retval = (String) cb.invoke( new SucceedingInvocation() );                  String retval = (String) cb.invoke( new SucceedingInvocation() );
48                  assertEquals(retval, "OK");                  assertEquals(retval, "OK");
49                    assertTrue( cb.isClosed() );
50                  assertTrue(cb.getFailureCount() == 0);                  assertTrue(cb.getFailureCount() == 0);
51          }          }
52                    
53          @Test(expected=IOException.class) public void simpleFailingTest() throws Throwable {          @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
54                  cb.invoke( new FailingInvocation() );                  cb.invoke( new FailingInvocation() );
55          }          }
56                    
57          @Test public void failingTest() throws Throwable {          @Test public void failingTest() {
58                  try {                  try {
59                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
60                  }catch (Throwable t) {}                  }catch (Exception e) {}
61                  assertTrue(cb.getFailureCount() == 1);                  assertTrue(cb.getFailureCount() == 1);
62                    assertTrue(cb.isClosed());
63            }
64            
65            @Test public void failAndResetTest() throws Exception {
66                    try {
67                            cb.invoke( new FailingInvocation() );
68                    }catch (Exception e) {}
69                    
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            @Test public void normalOpenTest() throws Exception {
77                    try{
78                            cb.invoke( new FailingInvocation() );
79                    } catch (IOException e) {}
80                    
81                    try{
82                            cb.invoke( new FailingInvocation() );
83                    } catch (IOException e) {}
84    
85                    assertTrue( cb.isOpen() );              
86          }          }
87                    
88          @Test public void normalOpenTest() throws Throwable {          @Test public void forcedTrip() {
89                    assertTrue( cb.isClosed() );
90                    cb.tripBreaker();
91                    assertTrue( cb.isOpen() );
92            }
93            
94            @Test public void forcedResetTest() throws Exception {
95                  try{                  try{
96                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
97                  } catch (IOException e) {}                  } catch (IOException e) {}
# Line 51  public class TestCircuitBreaker { Line 99  public class TestCircuitBreaker {
99                  try{                  try{
100                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
101                  } catch (IOException e) {}                  } catch (IOException e) {}
102                    
103                    cb.reset();
104                    
105                    assertTrue( cb.isClosed() );
106                    assertTrue( cb.getFailureCount() == 0 );
107            }
108    
109    
110                  assertTrue( cb.getStateName().equalsIgnoreCase("open") );                        @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  }  }

Legend:
Removed from v.445  
changed lines
  Added in v.456

  ViewVC Help
Powered by ViewVC 1.1.20