/[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 453 by torben, Tue Oct 20 10:48:47 2009 UTC revision 464 by torben, Wed Oct 21 10:49:26 2009 UTC
# Line 11  import org.junit.Test; Line 11  import org.junit.Test;
11    
12  import dk.thoerup.curcuitbreaker.CircuitBreaker;  import dk.thoerup.curcuitbreaker.CircuitBreaker;
13  import dk.thoerup.curcuitbreaker.CircuitBreakerException;  import dk.thoerup.curcuitbreaker.CircuitBreakerException;
14  import dk.thoerup.curcuitbreaker.CircuitInvocation;  
15  import dk.thoerup.curcuitbreaker.notification.SystemOutNotifier;  
16    
17    
18  public class TestCircuitBreaker {  public class TestCircuitBreaker {
19          public static final int DELAY = 50;          public static final int DELAY = 50;
20          public static final int THRESHOLD = 2;          public static final int THRESHOLD = 2;
21                    
         class SucceedingInvocation implements CircuitInvocation {  
                 public Object proceed() throws Exception {  
                         return "OK";  
                 }  
         }  
           
         class FailingInvocation implements CircuitInvocation {  
                 public Object proceed() throws Exception {  
                         throw new IOException("Error");  
                 }  
         }  
           
22                    
23          CircuitBreaker cb;          CircuitBreaker cb;
24                    
25          @Before public void setup() {          @Before public void setup() {
26                  cb = new CircuitBreaker("test", THRESHOLD, DELAY);                  cb = new CircuitBreaker("test", THRESHOLD, DELAY);
                 cb.setNotifier( new SystemOutNotifier() );  
27          }          }
28                    
29          @Test public void defaultState() {          @Test public void defaultState() {
# Line 86  public class TestCircuitBreaker { Line 73  public class TestCircuitBreaker {
73                  assertTrue( cb.isOpen() );                                assertTrue( cb.isOpen() );              
74          }          }
75                    
76            @Test public void forcedTrip() {
77                    assertTrue( cb.isClosed() );
78                    cb.tripBreaker();
79                    assertTrue( cb.isOpen() );
80            }
81            
82          @Test public void forcedResetTest() throws Exception {          @Test public void forcedResetTest() throws Exception {
83                  try{                  try{
84                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
# Line 98  public class TestCircuitBreaker { Line 91  public class TestCircuitBreaker {
91                  cb.reset();                  cb.reset();
92                                    
93                  assertTrue( cb.isClosed() );                  assertTrue( cb.isClosed() );
94                  assertTrue( cb.getFailureCount() == 0 ); //TODO: currently an externally triggered reset doesn't reset failure count - should a forced reset be possible at all ?                  assertTrue( cb.getFailureCount() == 0 );
95          }          }
96    
97    
# Line 130  public class TestCircuitBreaker { Line 123  public class TestCircuitBreaker {
123                  assertTrue( cb.isOpen() );                  assertTrue( cb.isOpen() );
124                  Thread.sleep(DELAY*2);                  Thread.sleep(DELAY*2);
125                                    
126                  cb.invoke( new FailingInvocation() ); //in half open this will cause a CircuitBreakerException                            cb.invoke( new FailingInvocation() ); //in half-open this will cause a CircuitBreakerException, as if it was in open mode              
127          }          }
128                    
129          @Test public void halfOpen3() throws Exception {          @Test public void halfOpen3() throws Exception {
# Line 140  public class TestCircuitBreaker { Line 133  public class TestCircuitBreaker {
133                  Thread.sleep(DELAY*2);                  Thread.sleep(DELAY*2);
134                                    
135                  try{                  try{
136                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
137                  } catch (CircuitBreakerException e) {}                  } catch (CircuitBreakerException e) {}
138    
139                  assertTrue( cb.isOpen() );                  assertTrue( cb.isOpen() ); //after failing in half-open go back to open
140          }          }
141    
142                    

Legend:
Removed from v.453  
changed lines
  Added in v.464

  ViewVC Help
Powered by ViewVC 1.1.20