1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.concurrent;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import org.apache.commons.lang3.AbstractLangTest;
24 import org.junit.jupiter.api.Test;
25
26
27
28
29 public class ThresholdCircuitBreakerTest extends AbstractLangTest {
30
31
32
33
34 private static final long threshold = 10L;
35
36 private static final long zeroThreshold = 0L;
37
38
39
40
41 @Test
42 public void testClosingThresholdCircuitBreaker() {
43 final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
44 circuit.incrementAndCheckState(9L);
45 circuit.close();
46
47 assertTrue(circuit.incrementAndCheckState(9L), "Internal counter was not reset back to zero");
48 }
49
50
51
52
53 @Test
54 public void testGettingThreshold() {
55 final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
56 assertEquals(Long.valueOf(threshold), Long.valueOf(circuit.getThreshold()), "Wrong value of threshold");
57 }
58
59
60
61
62 @Test
63 public void testThreshold() {
64 final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
65 circuit.incrementAndCheckState(9L);
66 assertTrue(circuit.incrementAndCheckState(1L), "Circuit opened before reaching the threshold");
67 }
68
69
70
71
72 @Test
73 public void testThresholdCircuitBreakingException() {
74 final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
75 circuit.incrementAndCheckState(9L);
76 assertFalse(circuit.incrementAndCheckState(2L), "The circuit was supposed to be open after increment above the threshold");
77 }
78
79
80
81
82 @Test
83 public void testThresholdEqualsZero() {
84 final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(zeroThreshold);
85 assertFalse(circuit.incrementAndCheckState(0L), "When the threshold is zero, the circuit is supposed to be always open");
86 }
87
88 }