View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.recovery;
15  
16  import org.junit.jupiter.api.Test;
17  
18  import static org.junit.jupiter.api.Assertions.assertFalse;
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  public class RecoveryCoordinatorTest {
22  
23      long now = System.currentTimeMillis();
24      RecoveryCoordinator rc = new RecoveryCoordinator(now);
25  
26      @Test
27      public void recoveryNotNeededAfterInit() {
28          RecoveryCoordinator rc = new RecoveryCoordinator();
29          assertTrue(rc.isTooSoon());
30      }
31  
32      @Test
33      public void recoveryNotNeededIfAsleepForLessThanBackOffTime() throws InterruptedException {
34          rc.setCurrentTime(now + RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN / 2);
35          assertTrue(rc.isTooSoon());
36      }
37  
38      @Test
39      public void recoveryNeededIfAsleepForMoreThanBackOffTime() throws InterruptedException {
40          rc.setCurrentTime(now + RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN + 20);
41          assertFalse(rc.isTooSoon());
42      }
43  
44      @Test
45      public void recoveryNotNeededIfCurrentTimeSetToBackOffTime() throws InterruptedException {
46          rc.setCurrentTime(now + RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN);
47          assertTrue(rc.isTooSoon());
48      }
49  
50      @Test
51      public void recoveryNeededIfCurrentTimeSetToExceedBackOffTime() {
52          rc.setCurrentTime(now + RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN + 1);
53          assertFalse(rc.isTooSoon());
54      }
55  
56      @Test
57      public void recoveryConditionDetectedEvenAfterReallyLongTimesBetweenRecovery() {
58          // Since backoff time quadruples whenever recovery is needed,
59          // we double the offset on each for-loop iteration, causing
60          // every other iteration to trigger recovery.
61  
62          long offset = RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN;
63  
64          for (int i = 0; i < 16; i++) {
65              rc.setCurrentTime(now + offset);
66  
67              if (i % 2 == 0) {
68                  assertTrue(rc.isTooSoon(), "recovery should've been needed at " + offset);
69              } else {
70                  assertFalse(rc.isTooSoon(), "recovery should NOT have been needed at " + offset);
71              }
72              offset *= 2;
73          }
74      }
75  }