summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/concurrent/maintenance/TestMaintainer.java
blob: d8191b98a51f913975cc2b98973ad2e2c9baf697 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent.maintenance;

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

/**
 * @author mpolden
 */
class TestMaintainer extends Maintainer {

    private int totalRuns = 0;
    private double success = 1.0;
    private RuntimeException exceptionToThrow = null;

    public TestMaintainer(String name, JobControl jobControl, JobMetrics jobMetrics) {
        super(name, Duration.ofDays(1), Clock.systemUTC(), jobControl, jobMetrics, List.of(), false);
    }

    public int totalRuns() {
        return totalRuns;
    }

    public TestMaintainer successOnNextRun(double success) {
        this.success = success;
        return this;
    }

    public TestMaintainer throwOnNextRun(RuntimeException e) {
        this.exceptionToThrow = e;
        return this;
    }

    @Override
    protected double maintain() {
        if (exceptionToThrow != null) throw exceptionToThrow;
        totalRuns++;
        return success;
    }

}