aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/test/java/com/yahoo/vespa/service/executor/TestExecutor.java
blob: 0cae62736cc65e1e27bf497d01ce4de093fe0aec (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.executor;

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

/**
 * @author hakonhall
 */
public class TestExecutor implements RunletExecutor {
    private List<Thread> threads = new ArrayList<>();

    private Runlet runlet;
    private CancellableImpl cancellable;

    private final Object monitor = new Object();
    private boolean afterRun = false;
    private boolean waitAfterRun = false;
    private int runsCompleted = 0;

    private final Runnable cancelExecution = () -> executionRunning = false;
    private volatile boolean executionRunning = true;

    @Override
    public Cancellable scheduleWithFixedDelay(Runlet runlet, Duration delay) {
        if (this.runlet != null) {
            throw new IllegalStateException("TestExecutor only supports execution of one runlet");
        }

        this.runlet = runlet;
        this.cancellable = new CancellableImpl(runlet);
        this.cancellable.setPeriodicExecutionCancellationCallback(cancelExecution);
        return this::cancel;
    }

    private void cancel() {
        cancellable.cancel();
    }

    boolean isExecutionRunning() {
        return executionRunning;
    }

    void runAsync() {
        Thread thread = new Thread(this::threadMain);
        thread.start();
        threads.add(thread);
    }

    void runToCompletion(int run) {
        runAsync();
        waitUntilRunCompleted(run);
    }

    private void threadMain() {
        cancellable.run();

        synchronized (monitor) {
            ++runsCompleted;
            afterRun = true;
            monitor.notifyAll();

            while (waitAfterRun) {
                monitor.notifyAll();
            }
            afterRun = false;
        }
    }

    void setWaitAfterRun(boolean waitAfterRun) {
        synchronized (monitor) {
            this.waitAfterRun = waitAfterRun;
        }
    }

    void waitUntilAfterRun() {
        synchronized (monitor) {
            while (!afterRun) {
                uncheckedWait();
            }
        }
    }

    void waitUntilRunCompleted(int run) {
        synchronized (monitor) {
            while (runsCompleted < run) {
                uncheckedWait();
            }
        }
    }

    void uncheckedWait() {
        try {
            monitor.wait();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void close() {
        threads.forEach(thread -> { try { thread.join(); } catch (InterruptedException ignored) {} });
    }
}