Search in sources :

Example 1 with ThreadReuseExecutor

use of org.teiid.dqp.internal.process.ThreadReuseExecutor in project teiid by teiid.

the class TestThreadReuseExecutor method testPriorities.

@Test
public void testPriorities() throws Exception {
    // $NON-NLS-1$
    pool = new ThreadReuseExecutor("test", 1);
    FutureWork<Boolean> work1 = new FutureWork<Boolean>(new Callable<Boolean>() {

        public Boolean call() throws Exception {
            synchronized (pool) {
                while (pool.getSubmittedCount() < 4) {
                    pool.wait();
                }
            }
            return true;
        }
    }, 0);
    final ConcurrentLinkedQueue<Integer> order = new ConcurrentLinkedQueue<Integer>();
    FutureWork<Boolean> work2 = new FutureWork<Boolean>(new Callable<Boolean>() {

        public Boolean call() throws Exception {
            order.add(2);
            return true;
        }
    }, 2);
    FutureWork<Boolean> work3 = new FutureWork<Boolean>(new Callable<Boolean>() {

        public Boolean call() throws Exception {
            order.add(3);
            return false;
        }
    }, 1);
    // ensure a later timestamp
    Thread.sleep(20);
    FutureWork<Boolean> work4 = new FutureWork<Boolean>(new Callable<Boolean>() {

        public Boolean call() throws Exception {
            order.add(4);
            return false;
        }
    }, 2);
    pool.execute(work1);
    pool.execute(work2);
    pool.execute(work3);
    pool.execute(work4);
    synchronized (pool) {
        pool.notifyAll();
    }
    work1.get();
    work2.get();
    work3.get();
    work4.get();
    assertEquals(Integer.valueOf(3), order.remove());
    assertEquals(Integer.valueOf(2), order.remove());
    assertEquals(Integer.valueOf(4), order.remove());
}
Also used : ThreadReuseExecutor(org.teiid.dqp.internal.process.ThreadReuseExecutor) FutureWork(org.teiid.dqp.internal.process.FutureWork) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Example 2 with ThreadReuseExecutor

use of org.teiid.dqp.internal.process.ThreadReuseExecutor in project teiid by teiid.

the class TestThreadReuseExecutor method testQueuing.

@Test
public void testQueuing() throws Exception {
    final long SINGLE_WAIT = 50;
    final int WORK_ITEMS = 10;
    final int MAX_THREADS = 5;
    // $NON-NLS-1$
    pool = new ThreadReuseExecutor("test", MAX_THREADS);
    for (int i = 0; i < WORK_ITEMS; i++) {
        pool.execute(new FakeWorkItem(SINGLE_WAIT));
    }
    pool.shutdown();
    pool.awaitTermination(1000, TimeUnit.MILLISECONDS);
    assertTrue(pool.isTerminated());
    WorkerPoolStatisticsMetadata stats = pool.getStats();
    assertEquals(10, stats.getTotalCompleted());
    // $NON-NLS-1$
    assertEquals("Expected threads to be maxed out", MAX_THREADS, stats.getHighestActiveThreads());
}
Also used : ThreadReuseExecutor(org.teiid.dqp.internal.process.ThreadReuseExecutor) WorkerPoolStatisticsMetadata(org.teiid.adminapi.impl.WorkerPoolStatisticsMetadata) Test(org.junit.Test)

Example 3 with ThreadReuseExecutor

use of org.teiid.dqp.internal.process.ThreadReuseExecutor in project teiid by teiid.

the class TestThreadReuseExecutor method testFailingWork.

@Test
public void testFailingWork() throws Exception {
    // $NON-NLS-1$
    pool = new ThreadReuseExecutor("test", 5);
    final Semaphore signal = new Semaphore(1);
    pool.execute(new Work() {

        @Override
        public void run() {
            signal.release();
            throw new RuntimeException();
        }

        @Override
        public void release() {
        }
    });
    assertTrue(signal.tryAcquire(2, TimeUnit.SECONDS));
}
Also used : ThreadReuseExecutor(org.teiid.dqp.internal.process.ThreadReuseExecutor) Work(javax.resource.spi.work.Work) FutureWork(org.teiid.dqp.internal.process.FutureWork) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 4 with ThreadReuseExecutor

use of org.teiid.dqp.internal.process.ThreadReuseExecutor in project teiid by teiid.

the class TestThreadReuseExecutor method testThreadReuse.

@Test
public void testThreadReuse() throws Exception {
    final long SINGLE_WAIT = 50;
    final long NUM_THREADS = 5;
    // $NON-NLS-1$
    pool = new ThreadReuseExecutor("test", 5);
    for (int i = 0; i < NUM_THREADS; i++) {
        pool.execute(new FakeWorkItem(SINGLE_WAIT));
        try {
            Thread.sleep(SINGLE_WAIT * 3);
        } catch (InterruptedException e) {
        }
    }
    pool.shutdown();
    WorkerPoolStatisticsMetadata stats = pool.getStats();
    // $NON-NLS-1$
    assertTrue("Expected approximately 1 thread for serial execution", stats.getHighestActiveThreads() <= 2);
    pool.awaitTermination(1000, TimeUnit.MILLISECONDS);
}
Also used : ThreadReuseExecutor(org.teiid.dqp.internal.process.ThreadReuseExecutor) WorkerPoolStatisticsMetadata(org.teiid.adminapi.impl.WorkerPoolStatisticsMetadata) Test(org.junit.Test)

Example 5 with ThreadReuseExecutor

use of org.teiid.dqp.internal.process.ThreadReuseExecutor in project teiid by teiid.

the class TestThreadReuseExecutor method testShutdown.

@Test(expected = RejectedExecutionException.class)
public void testShutdown() throws Exception {
    // $NON-NLS-1$
    pool = new ThreadReuseExecutor("test", 5);
    pool.shutdown();
    pool.execute(new FakeWorkItem(1));
}
Also used : ThreadReuseExecutor(org.teiid.dqp.internal.process.ThreadReuseExecutor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 ThreadReuseExecutor (org.teiid.dqp.internal.process.ThreadReuseExecutor)5 WorkerPoolStatisticsMetadata (org.teiid.adminapi.impl.WorkerPoolStatisticsMetadata)2 FutureWork (org.teiid.dqp.internal.process.FutureWork)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 Semaphore (java.util.concurrent.Semaphore)1 Work (javax.resource.spi.work.Work)1