From 172bf279221df316fa86974a229fdd3a70a3fbbf Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Fri, 8 Sep 2017 15:40:34 +0200 Subject: [PATCH] Fixed test after #1805. --- .../thread/ReservedThreadExecutorTest.java | 174 ++++++++---------- 1 file changed, 77 insertions(+), 97 deletions(-) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/thread/ReservedThreadExecutorTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/thread/ReservedThreadExecutorTest.java index 83702750ec5..3a3f73325dd 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/thread/ReservedThreadExecutorTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/thread/ReservedThreadExecutorTest.java @@ -25,6 +25,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -33,159 +34,138 @@ import static org.junit.Assert.assertThat; public class ReservedThreadExecutorTest { - final static int SIZE = 2; - TestExecutor _executor; - ReservedThreadExecutor _pae; - + private static final int SIZE = 2; + private static final Runnable NOOP = () -> {}; + + private TestExecutor _executor; + private ReservedThreadExecutor _reservedExecutor; + @Before public void before() throws Exception { _executor = new TestExecutor(); - _pae = new ReservedThreadExecutor(_executor,SIZE); - _pae.start(); + _reservedExecutor = new ReservedThreadExecutor(_executor, SIZE); + _reservedExecutor.start(); } - + @After public void after() throws Exception { - _pae.stop(); + _reservedExecutor.stop(); } @Test public void testStarted() throws Exception { - assertThat(_executor._queue.size(),is(SIZE)); - while(!_executor._queue.isEmpty()) - _executor.execute(); - - assertThat(_pae.getCapacity(),is(SIZE)); - - long started = System.nanoTime(); - while (_pae.getAvailable()10) - break; - Thread.sleep(100); - } - assertThat(_pae.getAvailable(),is(SIZE)); + // Reserved threads are lazily started. + assertThat(_executor._queue.size(), is(0)); } @Test public void testPending() throws Exception { - assertThat(_executor._queue.size(),is(SIZE)); - assertThat(_pae.tryExecute(new NOOP()),is(false)); - assertThat(_executor._queue.size(),is(SIZE)); - - _executor.execute(); - assertThat(_executor._queue.size(),is(SIZE-1)); - while (!_executor._queue.isEmpty()) + assertThat(_executor._queue.size(), is(0)); + + for (int i = 0; i < SIZE; i++) + _reservedExecutor.tryExecute(NOOP); + assertThat(_executor._queue.size(), is(SIZE)); + + for (int i = 0; i < SIZE; i++) _executor.execute(); + assertThat(_executor._queue.size(), is(0)); - long started = System.nanoTime(); - while (_pae.getAvailable()10) - break; - Thread.sleep(100); - } - assertThat(_executor._queue.size(),is(0)); - assertThat(_pae.getAvailable(),is(SIZE)); - - for (int i=SIZE;i-->0;) - assertThat(_pae.tryExecute(new Task()),is(true)); - assertThat(_executor._queue.size(),is(1)); - assertThat(_pae.getAvailable(),is(0)); + waitForAllAvailable(); - for (int i=SIZE;i-->0;) - assertThat(_pae.tryExecute(new NOOP()),is(false)); - assertThat(_executor._queue.size(),is(SIZE)); - assertThat(_pae.getAvailable(),is(0)); - - assertThat(_pae.tryExecute(new NOOP()),is(false)); - assertThat(_executor._queue.size(),is(SIZE)); - assertThat(_pae.getAvailable(),is(0)); + for (int i = 0; i < SIZE; i++) + assertThat(_reservedExecutor.tryExecute(new Task()), is(true)); + assertThat(_executor._queue.size(), is(1)); + assertThat(_reservedExecutor.getAvailable(), is(0)); + + for (int i = 0; i < SIZE; i++) + assertThat(_reservedExecutor.tryExecute(NOOP), is(false)); + assertThat(_executor._queue.size(), is(SIZE)); + assertThat(_reservedExecutor.getAvailable(), is(0)); } @Test public void testExecuted() throws Exception { - while(!_executor._queue.isEmpty()) + assertThat(_executor._queue.size(), is(0)); + + for (int i = 0; i < SIZE; i++) + _reservedExecutor.tryExecute(NOOP); + assertThat(_executor._queue.size(), is(SIZE)); + + for (int i = 0; i < SIZE; i++) _executor.execute(); - long started = System.nanoTime(); - while (_pae.getAvailable()10) - break; - Thread.sleep(100); - } - assertThat(_pae.getAvailable(),is(SIZE)); - - Task[] task = new Task[SIZE]; - for (int i=SIZE;i-->0;) - { - task[i] = new Task(); - assertThat(_pae.tryExecute(task[i]),is(true)); + tasks[i] = new Task(); + assertThat(_reservedExecutor.tryExecute(tasks[i]), is(true)); } - for (int i=SIZE;i-->0;) - { - task[i]._ran.await(10,TimeUnit.SECONDS); - } + for (int i = 0; i < SIZE; i++) + tasks[i]._ran.await(10, TimeUnit.SECONDS); + + assertThat(_executor._queue.size(), is(1)); - assertThat(_executor._queue.size(),is(1)); Task extra = new Task(); - assertThat(_pae.tryExecute(extra),is(false)); - assertThat(_executor._queue.size(),is(2)); - Thread.sleep(100); - assertThat(extra._ran.getCount(),is(1L)); + assertThat(_reservedExecutor.tryExecute(extra), is(false)); + assertThat(_executor._queue.size(), is(2)); - for (int i=SIZE;i-->0;) + Thread.sleep(500); + assertThat(extra._ran.getCount(), is(1L)); + + for (int i = 0; i < SIZE; i++) + tasks[i]._complete.countDown(); + + waitForAllAvailable(); + } + + protected void waitForAllAvailable() throws InterruptedException + { + long started = System.nanoTime(); + while (_reservedExecutor.getAvailable() < SIZE) { - task[i]._complete.countDown(); + long elapsed = System.nanoTime() - started; + if (elapsed > TimeUnit.SECONDS.toNanos(10)) + Assert.fail(); + Thread.sleep(10); } - - started = System.nanoTime(); - while (_pae.getAvailable()10) - break; - Thread.sleep(100); - } - assertThat(_pae.getAvailable(),is(SIZE)); + assertThat(_reservedExecutor.getAvailable(), is(SIZE)); } private static class TestExecutor implements Executor { - Deque _queue = new ArrayDeque<>(); + private final Deque _queue = new ArrayDeque<>(); @Override public void execute(Runnable task) { _queue.addLast(task); } - + public void execute() { Runnable task = _queue.pollFirst(); - if (task!=null) + if (task != null) new Thread(task).start(); } } - - private static class NOOP implements Runnable - { - @Override - public void run() {} - } - + private static class Task implements Runnable { private CountDownLatch _ran = new CountDownLatch(1); private CountDownLatch _complete = new CountDownLatch(1); + @Override - public void run() - { + public void run() + { _ran.countDown(); try {