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 static org.junit.Assert.*;
17  
18  import org.junit.Test;
19  
20  public class RecoveryCoordinatorTest {
21  
22      long now = System.currentTimeMillis();
23      RecoveryCoordinator rc = new RecoveryCoordinator(now);
24  
25      @Test
26      public void recoveryNotNeededAfterInit() {
27          RecoveryCoordinator rc = new RecoveryCoordinator();
28          assertTrue(rc.isTooSoon());
29      }
30  
31      @Test
32      public void recoveryNotNeededIfAsleepForLessThanBackOffTime() throws InterruptedException {
33          rc.setCurrentTime(now + RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN / 2);
34          assertTrue(rc.isTooSoon());
35      }
36  
37      @Test
38      public void recoveryNeededIfAsleepForMoreThanBackOffTime() throws InterruptedException {
39          rc.setCurrentTime(now + RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN + 20);
40          assertFalse(rc.isTooSoon());
41      }
42  
43      @Test
44      public void recoveryNotNeededIfCurrentTimeSetToBackOffTime() throws InterruptedException {
45          rc.setCurrentTime(now + RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN);
46          assertTrue(rc.isTooSoon());
47      }
48  
49      @Test
50      public void recoveryNeededIfCurrentTimeSetToExceedBackOffTime() {
51          rc.setCurrentTime(now + RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN + 1);
52          assertFalse(rc.isTooSoon());
53      }
54  
55      @Test
56      public void recoveryConditionDetectedEvenAfterReallyLongTimesBetweenRecovery() {
57          // Since backoff time quadruples whenever recovery is needed,
58          // we double the offset on each for-loop iteration, causing
59          // every other iteration to trigger recovery.
60  
61          long offset = RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN;
62  
63          for (int i = 0; i < 16; i++) {
64              rc.setCurrentTime(now + offset);
65  
66              if (i % 2 == 0) {
67                  assertTrue("recovery should've been needed at " + offset, rc.isTooSoon());
68              } else {
69                  assertFalse("recovery should NOT have been needed at " + offset, rc.isTooSoon());
70              }
71              offset *= 2;
72          }
73      }
74  }