View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import org.apache.commons.lang3.AbstractLangTest;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Test class for {@code ThresholdCircuitBreaker}.
29   */
30  class ThresholdCircuitBreakerTest extends AbstractLangTest {
31  
32      /**
33       * Threshold used in tests.
34       */
35      private static final long threshold = 10L;
36  
37      private static final long zeroThreshold = 0L;
38  
39      /**
40       * Tests that closing a {@code ThresholdCircuitBreaker} resets the internal counter.
41       */
42      @Test
43      void testClosingThresholdCircuitBreaker() {
44          final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
45          circuit.incrementAndCheckState(9L);
46          circuit.close();
47          // now the internal counter is back at zero, not 9 anymore. So it is safe to increment 9 again
48          assertTrue(circuit.incrementAndCheckState(9L), "Internal counter was not reset back to zero");
49      }
50  
51      /**
52       * Tests that we can get the threshold value correctly.
53       */
54      @Test
55      void testGettingThreshold() {
56          final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
57          assertEquals(Long.valueOf(threshold), Long.valueOf(circuit.getThreshold()), "Wrong value of threshold");
58      }
59  
60      /**
61       * Tests that a negative increment is rejected: the counter must only move toward the threshold.
62       */
63      @Test
64      void testNegativeIncrementRejected() {
65          final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
66          assertThrows(IllegalArgumentException.class, () -> circuit.incrementAndCheckState(-1L), "Negative increments must be rejected");
67      }
68  
69      /**
70       * Tests that overflowing the internal counter saturates and opens the circuit breaker instead of wrapping negative and silently disabling the trip
71       * condition.
72       */
73      @Test
74      void testOverflowSaturatesAndOpens() {
75          final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(Long.MAX_VALUE);
76          circuit.incrementAndCheckState(Long.MAX_VALUE);
77          // Without saturation this would wrap negative and report the circuit closed forever.
78          assertFalse(circuit.incrementAndCheckState(1L), "Overflow must open the circuit, not wrap the counter");
79          assertFalse(circuit.checkState(), "Circuit must stay open after overflow");
80      }
81  
82      /**
83       * Tests that the threshold is working as expected when incremented and no exception is thrown.
84       */
85      @Test
86      void testThreshold() {
87          final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
88          circuit.incrementAndCheckState(9L);
89          assertTrue(circuit.incrementAndCheckState(1L), "Circuit opened before reaching the threshold");
90      }
91  
92      /**
93       * Tests that exceeding the threshold raises an exception.
94       */
95      @Test
96      void testThresholdCircuitBreakingException() {
97          final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
98          circuit.incrementAndCheckState(9L);
99          assertFalse(circuit.incrementAndCheckState(2L), "The circuit was supposed to be open after increment above the threshold");
100     }
101 
102     /**
103      * Test that when threshold is zero, the circuit breaker is always open.
104      */
105     @Test
106     void testThresholdEqualsZero() {
107         final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(zeroThreshold);
108         assertFalse(circuit.incrementAndCheckState(0L), "When the threshold is zero, the circuit is supposed to be always open");
109     }
110 
111 }