1
2
3
4
5
6
7
8
9
10
11
12
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
59
60
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 }