20
20
21
21
import javaslang .circuitbreaker .CircuitBreaker ;
22
22
import javaslang .circuitbreaker .CircuitBreakerConfig ;
23
+ import javaslang .circuitbreaker .CircuitBreakerRegistry ;
23
24
import org .junit .Before ;
24
25
import org .junit .Test ;
25
26
26
27
import java .time .Duration ;
28
+ import java .util .concurrent .TimeUnit ;
27
29
28
30
import static java .lang .Thread .sleep ;
29
31
import static org .assertj .core .api .BDDAssertions .assertThat ;
32
+ import static org .junit .Assert .assertFalse ;
33
+ import static org .junit .Assert .assertTrue ;
30
34
31
35
public class CircuitBreakerStateMachineTest {
32
36
@@ -37,7 +41,7 @@ public void setUp(){
37
41
circuitBreaker = new CircuitBreakerStateMachine ("testName" , CircuitBreakerConfig .custom ()
38
42
.failureRateThreshold (50 )
39
43
.ringBufferSizeInClosedState (5 )
40
- .ringBufferSizeInHalfOpenState (2 )
44
+ .ringBufferSizeInHalfOpenState (3 )
41
45
.waitDurationInOpenState (Duration .ofSeconds (1 ))
42
46
.build ());
43
47
}
@@ -121,13 +125,16 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {
121
125
122
126
// Call 2 is a failure
123
127
circuitBreaker .recordFailure (new RuntimeException ());
128
+ // Call 3 is a success
129
+ circuitBreaker .recordSuccess ();
130
+
124
131
// The ring buffer is filled and the failure rate is above 50%
125
132
// The state machine transitions back to OPEN state
126
133
assertThat (circuitBreaker .isCallPermitted ()).isEqualTo (false );
127
134
assertThat (circuitBreaker .getState ()).isEqualTo (CircuitBreaker .State .OPEN );
128
- assertThat (circuitBreaker .getMetrics ().getNumberOfBufferedCalls ()).isEqualTo (2 );
135
+ assertThat (circuitBreaker .getMetrics ().getNumberOfBufferedCalls ()).isEqualTo (3 );
129
136
assertThat (circuitBreaker .getMetrics ().getNumberOfFailedCalls ()).isEqualTo (2 );
130
- assertThat (circuitBreaker .getMetrics ().getFailureRate ()).isEqualTo ( 100f );
137
+ assertThat (circuitBreaker .getMetrics ().getFailureRate ()).isGreaterThan ( 50f );
131
138
132
139
sleep (1300 );
133
140
@@ -145,12 +152,33 @@ public void testCircuitBreakerStateMachine() throws InterruptedException {
145
152
146
153
// Call 2 is a success
147
154
circuitBreaker .recordSuccess ();
148
- // The ring buffer is filled and the failure rate is equal to 50%
155
+ // Call 3 is a success
156
+ circuitBreaker .recordSuccess ();
157
+
158
+ // The ring buffer is filled and the failure rate is below 50%
149
159
// The state machine transitions back to CLOSED state
150
160
assertThat (circuitBreaker .isCallPermitted ()).isEqualTo (true );
151
161
assertThat (circuitBreaker .getState ()).isEqualTo (CircuitBreaker .State .CLOSED );
152
162
assertThat (circuitBreaker .getMetrics ().getNumberOfBufferedCalls ()).isEqualTo (0 );
153
163
assertThat (circuitBreaker .getMetrics ().getNumberOfFailedCalls ()).isEqualTo (0 );
154
164
assertThat (circuitBreaker .getMetrics ().getFailureRate ()).isEqualTo (-1f );
155
165
}
166
+
167
+ @ Test
168
+ public void testCircuitBreakerBehaviour () throws InterruptedException {
169
+ CircuitBreakerRegistry registry = CircuitBreakerRegistry .ofDefaults ();
170
+ int times = 3 ;
171
+ int waitSeconds = 2 ;
172
+ CircuitBreakerConfig config = CircuitBreakerConfig .custom ().ringBufferSizeInHalfOpenState (times )
173
+ .failureRateThreshold (100 ).ringBufferSizeInClosedState (times )
174
+ .waitDurationInOpenState (Duration .ofSeconds (waitSeconds )).build ();
175
+ CircuitBreaker circuitBreaker = registry .circuitBreaker ("something" , config );
176
+ for (int i = 0 ; i < times ; i ++) {
177
+ assertTrue ("Circuit-breaker call should be permitted" , circuitBreaker .isCallPermitted ());
178
+ circuitBreaker .recordFailure (new RuntimeException ("Fail! " + i ));
179
+ }
180
+ assertFalse ("Circuit-breaker call should not be permitted" , circuitBreaker .isCallPermitted ());
181
+ Thread .sleep (TimeUnit .SECONDS .toMillis (waitSeconds ));
182
+ assertTrue ("Circuit-breaker call should be permitted" , circuitBreaker .isCallPermitted ());
183
+ }
156
184
}
0 commit comments