From fac93fa7f8976acd401de40345edbbb9e4cb17c7 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 17 Apr 2018 16:29:46 +0200 Subject: [PATCH] Fixes #2451 - Review ReservedThreadExecutor.getAvailable(). Fixed by having getAvailable() returning _stack.size(). Signed-off-by: Simone Bordet --- .../util/thread/ReservedThreadExecutor.java | 2 +- .../thread/ReservedThreadExecutorTest.java | 25 ++++--------------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java index a0959c90f9e..61a5fd9c8ef 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/ReservedThreadExecutor.java @@ -120,7 +120,7 @@ public class ReservedThreadExecutor extends AbstractLifeCycle implements TryExec @ManagedAttribute(value = "available reserved threads", readonly = true) public int getAvailable() { - return _size.get(); + return _stack.size(); } @ManagedAttribute(value = "pending reserved threads", readonly = true) 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 f8cfffcf97a..4b416c6c2d0 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 @@ -18,13 +18,8 @@ package org.eclipse.jetty.util.thread; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - import java.util.ArrayDeque; import java.util.Deque; -import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @@ -36,6 +31,10 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Test; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + public class ReservedThreadExecutorTest { private static final int SIZE = 2; @@ -71,7 +70,7 @@ public class ReservedThreadExecutorTest } @Test - public void testStarted() throws Exception + public void testStarted() { // Reserved threads are lazily started. assertThat(_executor._queue.size(), is(0)); @@ -167,19 +166,6 @@ public class ReservedThreadExecutorTest assertThat(_reservedExecutor.getAvailable(),is(0)); } - protected void waitForNoPending() throws InterruptedException - { - long started = System.nanoTime(); - while (_reservedExecutor.getPending() > 0) - { - long elapsed = System.nanoTime() - started; - if (elapsed > TimeUnit.SECONDS.toNanos(10)) - Assert.fail("pending="+_reservedExecutor.getPending()); - Thread.sleep(10); - } - assertThat(_reservedExecutor.getPending(), is(0)); - } - protected void waitForAvailable(int size) throws InterruptedException { long started = System.nanoTime(); @@ -248,7 +234,6 @@ public class ReservedThreadExecutorTest reserved.start(); final int LOOPS = 1000000; - final Random random = new Random(); final AtomicInteger executions = new AtomicInteger(LOOPS); final CountDownLatch executed = new CountDownLatch(executions.get()); final AtomicInteger usedReserved = new AtomicInteger(0);