aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/FakeTimer.java
blob: 6c9cf6a2dbbce62b81a7508be594a0306e323b71 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core;

import java.util.logging.Level;
import com.yahoo.vespa.clustercontroller.core.testutils.LogFormatter;

import java.util.logging.Logger;

/**
 * FakeTimer
 *
 * Used to fake timing for unit test purposes.
 */
public class FakeTimer implements Timer {

    private static final Logger log = Logger.getLogger(FakeTimer.class.getName());

    static {
        LogFormatter.initializeLogging();
    }

    // Don't start at zero. Clock users may initialize a 'last run' entry with 0, and we want first time to always look like a timeout
    private long currentTime = (long) 30 * 365 * 24 * 60 * 60 * 1000;

    public synchronized long getCurrentTimeInMillis() {
        return currentTime;
    }

    public synchronized void advanceTime(long time) {
        long currentTime = getCurrentTimeInMillis();
        this.currentTime += time;
        log.log(Level.FINE, () -> "Time advanced by " + time + " ms. Time increased from " + currentTime + " to " + (currentTime + time));
        notifyAll();
    }

}