mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
yesterday 6477a7e28301b6d5d9746cf4bfb29ae4b748258d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.server.util;
 
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;
 
import org.opends.server.DirectoryServerTestCase;
import org.testng.annotations.Test;
 
/** Tests for {@link FailureLogThrottle}. */
@SuppressWarnings("javadoc")
public class FailureLogThrottleTest extends DirectoryServerTestCase
{
  private static final long INTERVAL_NANOS = MINUTES.toNanos(5);
 
  private static FailureLogThrottle newThrottle()
  {
    return new FailureLogThrottle(5, MINUTES);
  }
 
  @Test
  public void theFirstFailureIsLoggedHoweverLongTheThrottleHasExisted() throws Exception
  {
    assertThat(newThrottle().record(System.nanoTime()))
        .as("the first failure is logged, and stands for itself alone").isEqualTo(0);
  }
 
  @Test
  public void failuresInsideTheIntervalAreSuppressedAndCounted() throws Exception
  {
    final FailureLogThrottle throttle = newThrottle();
    final long start = System.nanoTime();
 
    throttle.record(start);
    assertThat(throttle.record(start + 1))
        .as("the next failure is the first suppressed one").isEqualTo(-2);
    assertThat(throttle.record(start + INTERVAL_NANOS - 1))
        .as("a failure one nanosecond before the interval is up is the second").isEqualTo(-3);
  }
 
  @Test
  public void theNextFailureLoggedReportsTheSuppressedOnesBeforeIt() throws Exception
  {
    final FailureLogThrottle throttle = newThrottle();
    final long start = System.nanoTime();
 
    throttle.record(start);
    throttle.record(start + 1);
    throttle.record(start + 2);
 
    assertThat(throttle.record(start + INTERVAL_NANOS))
        .as("the failure ending the interval reports the two suppressed before it").isEqualTo(2);
    assertThat(throttle.record(start + 2 * INTERVAL_NANOS))
        .as("the count starts again from the failure last logged").isEqualTo(0);
  }
 
  @Test
  public void eachThrottleCountsOnItsOwn() throws Exception
  {
    final FailureLogThrottle throttle = newThrottle();
    final FailureLogThrottle otherThrottle = newThrottle();
    final long start = System.nanoTime();
 
    throttle.record(start);
    throttle.record(start + 1);
 
    assertThat(otherThrottle.record(start + 2))
        .as("a failure of another kind is logged with a count of its own").isEqualTo(0);
  }
}