summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/MaintainerTest.java
blob: 604c29e7289c6c5058195f39da3835649a852b9f (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
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent.maintenance;

import com.yahoo.concurrent.UncheckedTimeoutException;
import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.List;

import static org.junit.Assert.assertEquals;

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

    private static final double delta = 0.000001;

    private final JobControl jobControl = new JobControl(new JobControlStateMock());

    @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() {
        TestJobMetrics jobMetrics = new TestJobMetrics();
        TestMaintainer maintainer = new TestMaintainer(null, jobControl, jobMetrics);

        maintainer.successOnNextRun(1.0).run();
        assertEquals(1, jobMetrics.successFactor, delta);
        maintainer.successOnNextRun(0.0).run();
        assertEquals(0, jobMetrics.successFactor, delta);
        maintainer.successOnNextRun(0.1).run();
        assertEquals(0.1, jobMetrics.successFactor, delta);

        // Maintainer throws
        maintainer.throwOnNextRun(new RuntimeException()).run();
        assertEquals(0, jobMetrics.successFactor, delta);

        // Maintainer recovers
        maintainer.throwOnNextRun(null).run();
        maintainer.successOnNextRun(1.0).run();
        assertEquals(1, jobMetrics.successFactor, delta);

        // Lock exception is treated as a failure
        maintainer.throwOnNextRun(new UncheckedTimeoutException()).run();
        assertEquals(0, jobMetrics.successFactor, delta);
    }

    private static class TestJobMetrics extends JobMetrics {

        double successFactor = 0.0;

        @Override
        public void completed(String job, double successFactor) {
            this.successFactor = successFactor;
        }

    }

}