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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3212 - (show annotations) (download)
Thu Dec 28 09:34:47 2017 UTC (6 years, 4 months ago) by torben
File size: 3612 byte(s)
Use generics to encapsulate returned value
1 package dk.thoerup.circuitbreaker;
2
3
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.IOException;
8
9 import org.junit.Before;
10 import org.junit.Test;
11
12 import dk.thoerup.circuitbreaker.CircuitBreaker;
13 import dk.thoerup.circuitbreaker.CircuitBreakerException;
14 import dk.thoerup.circuitbreaker.config.StaticConfig;
15
16
17
18
19 public class TestCircuitBreaker {
20 public static final int DELAY = 50;
21 public static final int THRESHOLD = 2;
22
23
24 CircuitBreaker<String> cb;
25
26 @Before public void setup() {
27 cb = new CircuitBreaker<>("test", new StaticConfig(THRESHOLD, DELAY) );
28 }
29
30 @Test public void defaultState() {
31 assertTrue(cb.isClosed());
32 assertTrue(cb.getFailureCount() == 0);
33 }
34
35 @Test public void simpleTest() throws Exception {
36 String retval = (String) cb.invoke( new SucceedingInvocation() );
37 assertEquals(retval, "OK");
38 assertTrue( cb.isClosed() );
39 assertTrue(cb.getFailureCount() == 0);
40 }
41
42 @Test(expected=IOException.class) public void simpleFailingTest() throws Exception {
43 cb.invoke( new FailingInvocation() );
44 }
45
46 @Test public void failingTest() {
47 try {
48 cb.invoke( new FailingInvocation() );
49 }catch (Exception e) {}
50 assertTrue(cb.getFailureCount() == 1);
51 assertTrue(cb.isClosed());
52 }
53
54 @Test public void failAndResetTest() throws Exception {
55 try {
56 cb.invoke( new FailingInvocation() );
57 }catch (Exception e) {}
58
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 @Test public void normalOpenTest() throws Exception {
66 try{
67 cb.invoke( new FailingInvocation() );
68 } catch (IOException e) {}
69
70 try{
71 cb.invoke( new FailingInvocation() );
72 } catch (IOException e) {}
73
74 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 {
84 try{
85 cb.invoke( new FailingInvocation() );
86 } catch (IOException e) {}
87
88 try{
89 cb.invoke( new FailingInvocation() );
90 } catch (IOException e) {}
91
92 cb.reset();
93
94 assertTrue( cb.isClosed() );
95 assertTrue( cb.getFailureCount() == 0 );
96 }
97
98
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 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 {
131 cb.tripBreaker();
132
133 assertTrue( cb.isOpen() );
134 Thread.sleep(DELAY*2);
135
136 try{
137 cb.invoke( new FailingInvocation() );
138 } catch (CircuitBreakerException e) {}
139
140 assertTrue( cb.isOpen() ); //after failing in half-open go back to open
141 }
142
143
144 }

  ViewVC Help
Powered by ViewVC 1.1.20