/[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 1315 by torben, Tue Apr 19 17:13:40 2011 UTC
# Line 9  import java.io.IOException; Line 9  import java.io.IOException;
9  import org.junit.Before;  import org.junit.Before;
10  import org.junit.Test;  import org.junit.Test;
11    
12  import dk.thoerup.curcuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
13  import dk.thoerup.curcuitbreaker.CircuitBreakerException;  import dk.thoerup.circuitbreaker.CircuitBreakerException;
14  import dk.thoerup.curcuitbreaker.CircuitInvocation;  import dk.thoerup.circuitbreaker.config.StaticConfig;
15  import dk.thoerup.curcuitbreaker.notification.SystemOutNotifier;  
16    
17    
18    
19  public class TestCircuitBreaker {  public class TestCircuitBreaker {
20          public static final int DELAY = 50;          public static final int DELAY = 50;
21          public static final int THRESHOLD = 2;          public static final int THRESHOLD = 2;
22                    
         class SucceedingInvocation implements CircuitInvocation {  
                 public Object proceed() throws Exception {  
                         return "OK";  
                 }  
         }  
           
         class FailingInvocation implements CircuitInvocation {  
                 public Object proceed() throws Exception {  
                         throw new IOException("Error");  
                 }  
         }  
           
23                    
24          CircuitBreaker cb;          CircuitBreaker cb;
25                    
26          @Before public void setup() {          @Before public void setup() {
27                  cb = new CircuitBreaker("test", THRESHOLD, DELAY);                  cb = new CircuitBreaker("test", new StaticConfig(THRESHOLD, DELAY) );
                 cb.setNotifier( new SystemOutNotifier() );  
28          }          }
29                    
30          @Test public void defaultState() {          @Test public void defaultState() {
# Line 86  public class TestCircuitBreaker { Line 74  public class TestCircuitBreaker {
74                  assertTrue( cb.isOpen() );                                assertTrue( cb.isOpen() );              
75          }          }
76                    
77            @Test public void forcedTrip() {
78                    assertTrue( cb.isClosed() );
79                    cb.tripBreaker();
80                    assertTrue( cb.isOpen() );
81            }
82            
83          @Test public void forcedResetTest() throws Exception {          @Test public void forcedResetTest() throws Exception {
84                  try{                  try{
85                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
# Line 98  public class TestCircuitBreaker { Line 92  public class TestCircuitBreaker {
92                  cb.reset();                  cb.reset();
93                                    
94                  assertTrue( cb.isClosed() );                  assertTrue( cb.isClosed() );
95                  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 );
96          }          }
97    
98    
# Line 130  public class TestCircuitBreaker { Line 124  public class TestCircuitBreaker {
124                  assertTrue( cb.isOpen() );                  assertTrue( cb.isOpen() );
125                  Thread.sleep(DELAY*2);                  Thread.sleep(DELAY*2);
126                                    
127                  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              
128          }          }
129                    
130          @Test public void halfOpen3() throws Exception {          @Test public void halfOpen3() throws Exception {
# Line 140  public class TestCircuitBreaker { Line 134  public class TestCircuitBreaker {
134                  Thread.sleep(DELAY*2);                  Thread.sleep(DELAY*2);
135                                    
136                  try{                  try{
137                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
138                  } catch (CircuitBreakerException e) {}                  } catch (CircuitBreakerException e) {}
139    
140                  assertTrue( cb.isOpen() );                  assertTrue( cb.isOpen() ); //after failing in half-open go back to open
141          }          }
142    
143                    

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

  ViewVC Help
Powered by ViewVC 1.1.20