summaryrefslogtreecommitdiffstats
path: root/docproc/src/test/java/com/yahoo/docproc/jdisc/DocprocThreadPoolExecutorTestCase.java
blob: f928ec4febd824855d84c6885409f1c757449a27 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.docproc.jdisc;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

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

/**
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public class DocprocThreadPoolExecutorTestCase {
    private final Set<Long> threadIds = Collections.synchronizedSet(new HashSet<Long>());

    @Test
    public void threadPool() throws InterruptedException {
        int numThreads = 8;
        int numTasks = 200;

        LinkedBlockingQueue<Runnable> q = new LinkedBlockingQueue<>();
        DocprocThreadManager mgr = new DocprocThreadManager(1000l);
        DocprocThreadPoolExecutor pool = new DocprocThreadPoolExecutor(numThreads, q, mgr);

        List<MockedDocumentProcessingTask> tasks = new ArrayList<>(numTasks);
        for (int i = 0; i < numTasks; i++) {
            tasks.add(new MockedDocumentProcessingTask());
        }

        for (int i = 0; i < numTasks; i++) {
            pool.execute(tasks.get(i));
        }
        pool.shutdown();
        pool.awaitTermination(120L, TimeUnit.SECONDS);

        for (int i = 0; i < numTasks; i++) {
            assertTrue(tasks.get(i).hasBeenRun());
        }

        System.err.println(threadIds);
        assertEquals(numThreads, threadIds.size());
    }

    private class MockedDocumentProcessingTask extends DocumentProcessingTask {
        private boolean hasBeenRun = false;

        public MockedDocumentProcessingTask() {
            super(null, null, null);
        }

        @Override
        public void run() {
            threadIds.add(Thread.currentThread().getId());
            System.err.println(System.currentTimeMillis() + "   MOCK Thread " + Thread.currentThread().getId() + " running task " + this);
            for (int i = 0; i < 100000; i++) {
                Math.sin((double) (System.currentTimeMillis() / 10000L));
            }
            System.err.println(System.currentTimeMillis() + "   MOCK Thread " + Thread.currentThread().getId() + " DONE task " + this);
            hasBeenRun = true;
        }

        @Override
        public int getApproxSize() {
            return 333;
        }

        @Override
        public String toString() {
            return "seqNum " + getSeqNum();
        }

        public boolean hasBeenRun() {
            return hasBeenRun;
        }
    }
}