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

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

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

revision 449 by torben, Mon Oct 19 20:10:09 2009 UTC revision 452 by torben, Tue Oct 20 10:47: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    import dk.thoerup.curcuitbreaker.notification.SystemOutNotifier;
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                    cb.setNotifier( new SystemOutNotifier() );
40            }
41            
42            @Test public void defaultState() {
43                    assertTrue(cb.isClosed());
44                    assertTrue(cb.getFailureCount() == 0);
45          }          }
46                    
47          @Test public void simpleTest() throws Throwable {          @Test public void simpleTest() throws Exception {
48                  String retval = (String) cb.invoke( new SucceedingInvocation() );                  String retval = (String) cb.invoke( new SucceedingInvocation() );
49                  assertEquals(retval, "OK");                  assertEquals(retval, "OK");
50                    assertTrue( cb.isClosed() );
51                  assertTrue(cb.getFailureCount() == 0);                  assertTrue(cb.getFailureCount() == 0);
52          }          }
53                    
54          @Test(expected=IOException.class) public void simpleFailingTest() throws Throwable {          @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
55                  cb.invoke( new FailingInvocation() );                  cb.invoke( new FailingInvocation() );
56          }          }
57                    
58          @Test public void failingTest() {          @Test public void failingTest() {
59                  try {                  try {
60                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
61                  }catch (Throwable t) {}                  }catch (Exception e) {}
62                  assertTrue(cb.getFailureCount() == 1);                  assertTrue(cb.getFailureCount() == 1);
63                    assertTrue(cb.isClosed());
64          }          }
65                    
66          @Test public void failAndResetTest() throws Throwable {          @Test public void failAndResetTest() throws Exception {
67                  try {                  try {
68                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
69                  }catch (Throwable t) {}                  }catch (Exception e) {}
70                                    
71                  cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed                  cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed
72                                    
# Line 54  public class TestCircuitBreaker { Line 74  public class TestCircuitBreaker {
74                  assertTrue(cb.getFailureCount() == 0);                            assertTrue(cb.getFailureCount() == 0);          
75          }          }
76                    
77          @Test public void normalOpenTest() throws Throwable {          @Test public void normalOpenTest() throws Exception {
78                  try{                  try{
79                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
80                  } catch (IOException e) {}                  } catch (IOException e) {}
# Line 66  public class TestCircuitBreaker { Line 86  public class TestCircuitBreaker {
86                  assertTrue( cb.isOpen() );                                assertTrue( cb.isOpen() );              
87          }          }
88                    
89          @Test public void forcedResetTest() throws Throwable {          @Test public void forcedResetTest() throws Exception {
90                  try{                  try{
91                          cb.invoke( new FailingInvocation() );                          cb.invoke( new FailingInvocation() );
92                  } catch (IOException e) {}                  } catch (IOException e) {}
# Line 77  public class TestCircuitBreaker { Line 97  public class TestCircuitBreaker {
97                                    
98                  cb.reset();                  cb.reset();
99    
100                  assertTrue( cb.isClosed() );                              assertTrue( cb.isClosed() );
101                    assertTrue( cb.getFailureCount() == 0 ); //currently an externally triggered reset doesn't reset failure count - should a forced reset be possible at all ?
102            }
103    
104    
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  }  }

Legend:
Removed from v.449  
changed lines
  Added in v.452

  ViewVC Help
Powered by ViewVC 1.1.20