summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/MaintainerTest.java
blob: 47ed010e95e7991e74b314c10fa4dfab1393d952 (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
// 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 com.yahoo.test.ManualClock;
import org.junit.Test;

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * @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() {
        ManualClock clock = new ManualClock();
        AtomicReference<Instant> lastSuccess = new AtomicReference<>();
        JobMetrics jobMetrics = new JobMetrics(clock, (job, instant) -> lastSuccess.set(instant));
        TestMaintainer maintainer = new TestMaintainer(jobMetrics);

        // Maintainer not successful yet
        maintainer.successOnNextRun(false).run();
        assertNull(lastSuccess.get());

        // Maintainer runs successfully
        clock.advance(Duration.ofHours(1));
        Instant lastSuccess0 = clock.instant();
        maintainer.successOnNextRun(true).run();
        assertEquals(lastSuccess0, lastSuccess.get());

        // Maintainer runs successfully again
        clock.advance(Duration.ofHours(2));
        Instant lastSuccess1 = clock.instant();
        maintainer.run();
        assertEquals(lastSuccess1, lastSuccess.get());

        // Maintainer throws
        clock.advance(Duration.ofHours(5));
        maintainer.throwOnNextRun(true).run();
        assertEquals("Time of successful run is unchanged", lastSuccess1, lastSuccess.get());

        // Maintainer recovers
        clock.advance(Duration.ofHours(3));
        Instant lastSuccess2 = clock.instant();
        maintainer.throwOnNextRun(false).run();
        assertEquals(lastSuccess2, lastSuccess.get());
    }

}