summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/MaintainerTest.java
blob: 2bfaad894a5370c3e2b9985a583f0f97e954a18b (plain) (blame)
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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent.maintenance;

import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.assertEquals;

/**
 * @author freva
 */
public class MaintainerTest {

    @Test
    public void staggering() {
        List<String> cluster = List.of("cfg1", "cfg2", "cfg3");
        Duration interval = Duration.ofMillis(300);
        Instant now = Instant.ofEpochMilli(1000);
        assertEquals(200, Maintainer.staggeredDelay(interval, now, "cfg1", cluster).toMillis());
        assertEquals(0, Maintainer.staggeredDelay(interval, now, "cfg2", cluster).toMillis());
        assertEquals(100, Maintainer.staggeredDelay(interval, now, "cfg3", cluster).toMillis());

        now = Instant.ofEpochMilli(1001);
        assertEquals(199, Maintainer.staggeredDelay(interval, now, "cfg1", cluster).toMillis());
        assertEquals(299, Maintainer.staggeredDelay(interval, now, "cfg2", cluster).toMillis());
        assertEquals(99, Maintainer.staggeredDelay(interval, now, "cfg3", cluster).toMillis());

        now = Instant.ofEpochMilli(1101);
        assertEquals(99, Maintainer.staggeredDelay(interval, now, "cfg1", cluster).toMillis());
        assertEquals(199, Maintainer.staggeredDelay(interval, now, "cfg2", cluster).toMillis());
        assertEquals(299, Maintainer.staggeredDelay(interval, now, "cfg3", cluster).toMillis());

        assertEquals(300, Maintainer.staggeredDelay(interval, now, "cfg0", cluster).toMillis());
    }

    @Test
    public void success_metric() {
        AtomicLong consecutiveFailures = new AtomicLong();
        JobMetrics jobMetrics = new JobMetrics((job, count) -> consecutiveFailures.set(count));
        TestMaintainer maintainer = new TestMaintainer(jobMetrics);

        // Maintainer fails twice in a row
        maintainer.successOnNextRun(false).run();
        assertEquals(1, consecutiveFailures.get());
        maintainer.successOnNextRun(false).run();
        assertEquals(2, consecutiveFailures.get());

        // Maintainer runs successfully
        maintainer.successOnNextRun(true).run();
        assertEquals(0, consecutiveFailures.get());

        // Maintainer runs successfully again
        maintainer.run();
        assertEquals(0, consecutiveFailures.get());

        // Maintainer throws
        maintainer.throwOnNextRun(true).run();
        assertEquals(1, consecutiveFailures.get());

        // Maintainer recovers
        maintainer.throwOnNextRun(false).run();
        assertEquals(0, consecutiveFailures.get());
    }

}