aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/concurrent/CachedThreadPoolWithFallbackTest.java
blob: fea2d39f666af8b9f0a3428a314570bc4db04270 (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 Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.concurrent;

import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.assertEquals;

public class CachedThreadPoolWithFallbackTest {
    private static void countAndBlock(AtomicLong counter, long waitLimit) {
        counter.incrementAndGet();
        try {
            synchronized (counter) {
                while (counter.get() < waitLimit) {
                    counter.wait();
                }
            }
        } catch (InterruptedException e) {}
    }

    @Test
    public void testThatTaskAreQueued() throws InterruptedException {
        CachedThreadPoolWithFallback executor = new CachedThreadPoolWithFallback("test", 1, 30, 1, TimeUnit.SECONDS);
        AtomicLong counter = new AtomicLong(0);
        for (int i = 0; i < 1000; i++) {
            executor.execute(() -> countAndBlock(counter, 100));
        }
        while (counter.get() < 30) {
            Thread.sleep(1);
        }
        Thread.sleep(1);
        assertEquals(30L, counter.get());
        counter.set(100);
        synchronized (counter) {
            counter.notifyAll();
        }
        executor.close();
        assertEquals(1070L, counter.get());
    }
}