fix AbstractThreadPoolTest from merge from 9.4.x

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-05-27 19:35:38 +10:00
parent 65dbcc7c86
commit 423777ed02
2 changed files with 21 additions and 28 deletions

View File

@ -18,11 +18,7 @@
package org.eclipse.jetty.util.thread;
import java.time.Duration;
import org.eclipse.jetty.util.ProcessorUtils;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.ThreadPool.SizedThreadPool;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterAll;
@ -30,8 +26,6 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.fail;
public abstract class AbstractThreadPoolTest
@ -93,26 +87,4 @@ public abstract class AbstractThreadPoolTest
assertThat(pool.getMaxThreads(), Matchers.is(3));
}
@Test
public void testJoinWithStopTimeout()
{
// ThreadPool must be an implement ContainerLifeCycle for this test to be valid.
SizedThreadPool threadPool = newPool(3);
if (!(threadPool instanceof ContainerLifeCycle))
return;
final long stopTimeout = 100;
((ContainerLifeCycle)threadPool).setStopTimeout(100);
LifeCycle.start(threadPool);
// Verify that join does not timeout after waiting twice the stopTimeout.
assertThrows(Throwable.class, () ->
assertTimeoutPreemptively(Duration.ofMillis(stopTimeout * 2), threadPool::join)
);
// After stopping the ThreadPool join should unblock.
LifeCycle.stop(threadPool);
assertTimeoutPreemptively(Duration.ofMillis(stopTimeout), threadPool::join);
}
}

View File

@ -19,11 +19,13 @@
package org.eclipse.jetty.util.thread;
import java.io.Closeable;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.thread.ThreadPool.SizedThreadPool;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
@ -36,6 +38,7 @@ import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class QueuedThreadPoolTest extends AbstractThreadPoolTest
@ -753,6 +756,24 @@ public class QueuedThreadPoolTest extends AbstractThreadPoolTest
assertThrows(IllegalArgumentException.class, () -> new QueuedThreadPool(4, 8));
}
@Test
public void testJoinWithStopTimeout() throws Exception
{
final long stopTimeout = 100;
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setStopTimeout(100);
threadPool.start();
// Verify that join does not timeout after waiting twice the stopTimeout.
assertThrows(Throwable.class, () ->
assertTimeoutPreemptively(Duration.ofMillis(stopTimeout * 2), threadPool::join)
);
// After stopping the ThreadPool join should unblock.
LifeCycle.stop(threadPool);
assertTimeoutPreemptively(Duration.ofMillis(stopTimeout), threadPool::join);
}
@Test
public void testDump() throws Exception
{