Issue #2619 - Test Failure: QueuedThreadPoolTest.testThreadPool().

Cleanup of QueuedThreadPoolTest.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2018-06-04 17:44:11 +02:00
parent 7af2eecf6a
commit c6cc33805f
2 changed files with 43 additions and 55 deletions

View File

@ -18,6 +18,16 @@
package org.eclipse.jetty.util.thread;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.toolchain.test.AdvancedRunner;
import org.eclipse.jetty.util.log.StacklessLogging;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
@ -25,23 +35,12 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.toolchain.test.AdvancedRunner;
import org.eclipse.jetty.toolchain.test.annotation.Slow;
import org.eclipse.jetty.util.log.StacklessLogging;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AdvancedRunner.class)
public class QueuedThreadPoolTest
{
final AtomicInteger _jobs=new AtomicInteger();
private final AtomicInteger _jobs=new AtomicInteger();
class RunningJob implements Runnable
private class RunningJob implements Runnable
{
private final CountDownLatch _run = new CountDownLatch(1);
private final CountDownLatch _stopping = new CountDownLatch(1);
@ -72,11 +71,9 @@ public class QueuedThreadPoolTest
if (!_stopped.await(10,TimeUnit.SECONDS))
throw new IllegalStateException();
}
};
}
@Test
@Slow
public void testThreadPool() throws Exception
{
QueuedThreadPool tp= new QueuedThreadPool();
@ -96,7 +93,7 @@ public class QueuedThreadPoolTest
waitForThreads(tp,2);
waitForIdle(tp,2);
// Run a single job
// Run job0
RunningJob job0=new RunningJob();
tp.execute(job0);
assertTrue(job0._run.await(10,TimeUnit.SECONDS));
@ -151,29 +148,22 @@ public class QueuedThreadPoolTest
waitForThreads(tp,2);
waitForIdle(tp,2);
}
@Test
@Slow
public void testShrink() throws Exception
{
final AtomicInteger sleep = new AtomicInteger(100);
Runnable job = new Runnable()
Runnable job = () ->
{
@Override
public void run()
try
{
try
{
Thread.sleep(sleep.get());
}
catch(Exception e)
{
e.printStackTrace();
}
Thread.sleep(sleep.get());
}
catch(Exception e)
{
e.printStackTrace();
}
};
QueuedThreadPool tp= new QueuedThreadPool();
@ -211,13 +201,16 @@ public class QueuedThreadPoolTest
QueuedThreadPool tp= new QueuedThreadPool();
tp.setStopTimeout(500);
tp.start();
tp.execute(new Runnable(){
@Override
public void run () {
while (true) {
try {
Thread.sleep(10000);
} catch (InterruptedException ie) {}
tp.execute(() ->
{
while (true)
{
try
{
Thread.sleep(10000);
}
catch (InterruptedException expected)
{
}
}
});
@ -229,7 +222,6 @@ public class QueuedThreadPoolTest
assertTrue(afterStop - beforeStop < 1000);
}
private void waitForIdle(QueuedThreadPool tp, int idle)
{
long now=TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
@ -240,8 +232,9 @@ public class QueuedThreadPoolTest
{
Thread.sleep(50);
}
catch(InterruptedException e)
{}
catch(InterruptedException ignored)
{
}
now=TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
}
Assert.assertEquals(idle, tp.getIdleThreads());
@ -257,8 +250,9 @@ public class QueuedThreadPoolTest
{
Thread.sleep(50);
}
catch(InterruptedException e)
{}
catch(InterruptedException ignored)
{
}
now=TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
}
assertEquals(threads,tp.getThreads());
@ -274,10 +268,10 @@ public class QueuedThreadPoolTest
tp.start();
try (StacklessLogging stackless = new StacklessLogging(QueuedThreadPool.class))
{
tp.execute(new Runnable(){ @Override public void run () { throw new IllegalStateException(); } });
tp.execute(new Runnable(){ @Override public void run () { throw new Error(); } });
tp.execute(new Runnable(){ @Override public void run () { throw new RuntimeException(); } });
tp.execute(new Runnable(){ @Override public void run () { throw new ThreadDeath(); } });
tp.execute(() -> { throw new IllegalStateException(); });
tp.execute(() -> { throw new Error(); });
tp.execute(() -> { throw new RuntimeException(); });
tp.execute(() -> { throw new ThreadDeath(); });
Thread.sleep(100);
assertThat(tp.getThreads(),greaterThanOrEqualTo(5));
@ -293,14 +287,7 @@ public class QueuedThreadPoolTest
pool.start();
final CountDownLatch latch = new CountDownLatch(1);
pool.execute(new Runnable()
{
@Override
public void run()
{
latch.countDown();
}
});
pool.execute(latch::countDown);
assertTrue(latch.await(5, TimeUnit.SECONDS));
}

View File

@ -2,4 +2,5 @@
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
#org.eclipse.jetty.util.LEVEL=DEBUG
#org.eclipse.jetty.util.PathWatcher.LEVEL=DEBUG
#org.eclipse.jetty.util.thread.QueuedThreadPool.LEVEL=DEBUG
#org.eclipse.jetty.util.thread.ReservedThreadExecutor.LEVEL=DEBUG