View Javadoc
1   package ch.qos.logback.core.util;
2   
3   import static ch.qos.logback.core.util.DefaultInvocationGate.DEFAULT_MASK;
4   import static ch.qos.logback.core.util.DefaultInvocationGate.MASK_DECREASE_RIGHT_SHIFT_COUNT;
5   import static org.junit.jupiter.api.Assertions.assertEquals;
6   import static org.junit.jupiter.api.Assertions.assertFalse;
7   import static org.junit.jupiter.api.Assertions.assertTrue;
8   
9   import org.junit.jupiter.api.Test;
10  
11  public class DefaultInvocationGateTest {
12  
13      @Test
14      public void smoke() {
15          long currentTime = 0;
16          long minDelayThreshold = 4;
17          long maxDelayThreshold = 8;
18  
19          DefaultInvocationGate gate = new DefaultInvocationGate(minDelayThreshold, maxDelayThreshold, currentTime);
20          assertTrue(gate.isTooSoon(0));
21      }
22  
23      @Test
24      public void closelyRepeatedCallsShouldCauseMaskToIncrease() {
25          long currentTime = 0;
26          long minDelayThreshold = 4;
27          long maxDelayThreshold = 8;
28  
29          DefaultInvocationGate gate = new DefaultInvocationGate(minDelayThreshold, maxDelayThreshold, currentTime);
30          for (int i = 0; i < DEFAULT_MASK; i++) {
31              assertTrue(gate.isTooSoon(0));
32          }
33          assertFalse(gate.isTooSoon(0));
34          assertTrue(gate.getMask() > DEFAULT_MASK);
35      }
36  
37      @Test
38      public void stableAtSteadyRate() {
39          long currentTime = 0;
40          long minDelayThreshold = DEFAULT_MASK;
41          long maxDelayThreshold = DEFAULT_MASK * 2;
42  
43          DefaultInvocationGate gate = new DefaultInvocationGate(minDelayThreshold, maxDelayThreshold, currentTime);
44  
45          for (int t = 0; t < 4 * minDelayThreshold; t++) {
46              gate.isTooSoon(currentTime++);
47              assertEquals(DEFAULT_MASK, gate.getMask());
48          }
49      }
50  
51      @Test
52      public void intermittentCallsShouldCauseMaskToDecrease() {
53          long currentTime = 0;
54          long minDelayThreshold = 4;
55          long maxDelayThreshold = 8;
56  
57          DefaultInvocationGate gate = new DefaultInvocationGate(minDelayThreshold, maxDelayThreshold, currentTime);
58          int currentMask = DEFAULT_MASK;
59  
60          currentTime += maxDelayThreshold + 1;
61          assertFalse(gate.isTooSoon(currentTime));
62          assertTrue(gate.getMask() < currentMask);
63      }
64  
65      @Test
66      public void maskCanDropToZeroForInfrequentInvocations() {
67          long currentTime = 0;
68          long minDelayThreshold = 4;
69          long maxDelayThreshold = 8;
70  
71          DefaultInvocationGate gate = new DefaultInvocationGate(minDelayThreshold, maxDelayThreshold, currentTime);
72          int currentMask = DEFAULT_MASK;
73  
74          do {
75              currentTime += maxDelayThreshold + 1;
76              assertFalse(gate.isTooSoon(currentTime));
77              assertTrue(gate.getMask() < currentMask);
78              currentMask = currentMask >> MASK_DECREASE_RIGHT_SHIFT_COUNT;
79          } while (currentMask > 0);
80  
81          assertEquals(0, gate.getMask());
82          assertFalse(gate.isTooSoon(currentTime));
83      }
84  }