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    *      http://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.assertTrue;
22  
23  import org.apache.commons.lang3.AbstractLangTest;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test class for {@code ThresholdCircuitBreaker}.
28   */
29  public class ThresholdCircuitBreakerTest extends AbstractLangTest {
30  
31      /**
32       * Threshold used in tests.
33       */
34      private static final long threshold = 10L;
35  
36      private static final long zeroThreshold = 0L;
37  
38      /**
39       * Tests that closing a {@code ThresholdCircuitBreaker} resets the internal counter.
40       */
41      @Test
42      public void testClosingThresholdCircuitBreaker() {
43          final ThresholdCircuitBreaker circuit = new ThresholdCircuitBreaker(threshold);
44          circuit.incrementAndCheckState(9L);
45          circuit.close();
46          // now the internal counter is back at zero, not 9 anymore. So it is safe to increment 9 again
47          assertTrue(circuit.incrementAndCheckState(9L), "Internal counter was not reset back to zero");
48      }
49  
50      /**
51       * Tests that we can get the threshold value correctly.
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       * Tests that the threshold is working as expected when incremented and no exception is thrown.
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       * Tests that exceeding the threshold raises an exception.
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       * Test that when threshold is zero, the circuit breaker is always open.
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  }