aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/test/java/com/yahoo/vespa/service/executor/RunletExecutorImplTest.java
blob: b87b770f25bcb573bd1728621a724f120d6217bd (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
// 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 org.junit.After;
import org.junit.Test;

import java.time.Duration;

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

/**
 * @author hakonhall
 */
public class RunletExecutorImplTest {
    private final RunletExecutorImpl executor = new RunletExecutorImpl(2);

    @After
    public void tearDown() {
        executor.close();
    }

    @Test
    public void testAFewCancellations() {
        for (int i = 0; i < 10; ++i) {
            TestRunlet runlet = new TestRunlet();
            Cancellable cancellable = schedule(runlet);
            runlet.waitUntilCompleted(5);
            cancellable.cancel();
            runlet.waitUntilClosed();
        }
    }

    @Test
    public void testCongestedThreadPool() {
        TestRunlet runlet1 = new TestRunlet();
        runlet1.shouldWaitInRun(true);
        Cancellable cancellable1 = schedule(runlet1);
        runlet1.waitUntilInRun();

        TestRunlet runlet2 = new TestRunlet();
        runlet2.shouldWaitInRun(true);
        Cancellable cancellable2 = schedule(runlet2);
        runlet2.waitUntilInRun();

        TestRunlet runlet3 = new TestRunlet();
        Cancellable cancellable3 = schedule(runlet3);
        try { Thread.sleep(10); } catch (InterruptedException ignored) { }
        assertEquals(0, runlet3.getRunsStarted());

        cancellable3.cancel();
        assertTrue(runlet3.isClosed());
        assertEquals(0, runlet3.getRunsStarted());

        runlet1.shouldWaitInRun(false);
        runlet2.shouldWaitInRun(false);
        cancellable1.cancel();
        cancellable2.cancel();
    }

    @Test
    public void testWithoutCancellation() {
        TestRunlet runlet = new TestRunlet();
        Cancellable toBeIgnored = schedule(runlet);
        runlet.waitUntilCompleted(2);
    }

    private Cancellable schedule(Runlet runlet) {
        return executor.scheduleWithFixedDelay(runlet, Duration.ofMillis(20));
    }
}