/[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 3212 - (hide annotations) (download)
Thu Dec 28 09:34:47 2017 UTC (6 years, 5 months ago) by torben
File size: 3612 byte(s)
Use generics to encapsulate returned value
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 467 import dk.thoerup.circuitbreaker.CircuitBreaker;
13     import dk.thoerup.circuitbreaker.CircuitBreakerException;
14 torben 1315 import dk.thoerup.circuitbreaker.config.StaticConfig;
15 torben 452
16    
17 torben 454
18 torben 464
19 torben 445 public class TestCircuitBreaker {
20 torben 452 public static final int DELAY = 50;
21     public static final int THRESHOLD = 2;
22    
23 torben 445
24 torben 3212 CircuitBreaker<String> cb;
25 torben 445
26     @Before public void setup() {
27 torben 3212 cb = new CircuitBreaker<>("test", new StaticConfig(THRESHOLD, DELAY) );
28 torben 445 }
29    
30 torben 452 @Test public void defaultState() {
31     assertTrue(cb.isClosed());
32     assertTrue(cb.getFailureCount() == 0);
33     }
34    
35     @Test public void simpleTest() throws Exception {
36 torben 445 String retval = (String) cb.invoke( new SucceedingInvocation() );
37     assertEquals(retval, "OK");
38 torben 452 assertTrue( cb.isClosed() );
39 torben 445 assertTrue(cb.getFailureCount() == 0);
40     }
41    
42 torben 452 @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
43 torben 445 cb.invoke( new FailingInvocation() );
44     }
45    
46 torben 449 @Test public void failingTest() {
47 torben 445 try {
48     cb.invoke( new FailingInvocation() );
49 torben 452 }catch (Exception e) {}
50 torben 445 assertTrue(cb.getFailureCount() == 1);
51 torben 452 assertTrue(cb.isClosed());
52 torben 445 }
53    
54 torben 452 @Test public void failAndResetTest() throws Exception {
55 torben 449 try {
56     cb.invoke( new FailingInvocation() );
57 torben 452 }catch (Exception e) {}
58 torben 449
59     cb.invoke(new SucceedingInvocation() ); //after one good it should reset back to closed
60    
61     assertTrue(cb.isClosed());
62     assertTrue(cb.getFailureCount() == 0);
63     }
64    
65 torben 452 @Test public void normalOpenTest() throws Exception {
66 torben 445 try{
67     cb.invoke( new FailingInvocation() );
68     } catch (IOException e) {}
69    
70     try{
71     cb.invoke( new FailingInvocation() );
72     } catch (IOException e) {}
73    
74 torben 449 assertTrue( cb.isOpen() );
75 torben 445 }
76 torben 449
77 torben 455 @Test public void forcedTrip() {
78     assertTrue( cb.isClosed() );
79     cb.tripBreaker();
80     assertTrue( cb.isOpen() );
81     }
82    
83 torben 452 @Test public void forcedResetTest() throws Exception {
84 torben 449 try{
85     cb.invoke( new FailingInvocation() );
86     } catch (IOException e) {}
87    
88     try{
89     cb.invoke( new FailingInvocation() );
90     } catch (IOException e) {}
91    
92 torben 453 cb.reset();
93    
94 torben 452 assertTrue( cb.isClosed() );
95 torben 456 assertTrue( cb.getFailureCount() == 0 );
96 torben 449 }
97    
98 torben 452
99     @Test(expected=CircuitBreakerException.class) public void openAndFailingTest1() throws Exception {
100     cb.tripBreaker();
101     assertTrue(cb.isOpen());
102     cb.invoke( new FailingInvocation() );
103     }
104    
105     @Test(expected=CircuitBreakerException.class) public void openAndFailingTest2() throws Exception {
106     cb.tripBreaker();
107     assertTrue(cb.isOpen());
108     cb.invoke( new SucceedingInvocation() );
109     }
110    
111     @Test public void halfOpen1() throws Exception {
112     cb.tripBreaker();
113    
114     assertTrue( cb.isOpen() );
115     Thread.sleep(DELAY*2);
116    
117     cb.invoke( new SucceedingInvocation() );
118     assertTrue( cb.isClosed() );
119     }
120    
121     @Test(expected=CircuitBreakerException.class) public void halfOpen2() throws Exception {
122     cb.tripBreaker();
123    
124     assertTrue( cb.isOpen() );
125     Thread.sleep(DELAY*2);
126    
127 torben 459 cb.invoke( new FailingInvocation() ); //in half-open this will cause a CircuitBreakerException, as if it was in open mode
128 torben 452 }
129    
130     @Test public void halfOpen3() throws Exception {
131     cb.tripBreaker();
132    
133     assertTrue( cb.isOpen() );
134     Thread.sleep(DELAY*2);
135    
136     try{
137 torben 459 cb.invoke( new FailingInvocation() );
138 torben 452 } catch (CircuitBreakerException e) {}
139    
140 torben 459 assertTrue( cb.isOpen() ); //after failing in half-open go back to open
141 torben 452 }
142    
143    
144 torben 445 }

  ViewVC Help
Powered by ViewVC 1.1.20