Skip to content

Commit

Permalink
#9622 replace wait loops with awaitility & add log on condition timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Apr 4, 2023
1 parent a87784c commit 8f33389
Showing 1 changed file with 14 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.awaitility.core.ConditionEvaluationListener;
import org.awaitility.core.EvaluatedCondition;
import org.awaitility.core.TimeoutEvent;
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.component.LifeCycle;
Expand All @@ -37,6 +40,7 @@
import org.slf4j.LoggerFactory;

import static org.awaitility.Awaitility.await;
import static org.awaitility.Awaitility.with;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -700,51 +704,29 @@ public void testMaxStopTime() throws Exception

private void waitForIdle(QueuedThreadPool tp, int idle)
{
long start = NanoTime.now();
while (tp.getIdleThreads() != idle && NanoTime.millisSince(start) < 10000)
with().conditionEvaluationListener(new ConditionEvaluationListener<String>()
{
try
@Override
public void conditionEvaluated(EvaluatedCondition condition)
{
Thread.sleep(50);
}
catch (InterruptedException ignored)

@Override
public void onTimeout(TimeoutEvent timeoutEvent)
{
LOG.info(timeoutEvent.getDescription() + " " + tp);
}
}
assertThat(tp.getIdleThreads(), is(idle));
}).await().atMost(10, TimeUnit.SECONDS).until(tp::getIdleThreads, is(idle));
}

private void waitForReserved(QueuedThreadPool tp, int reserved)
{
long start = NanoTime.now();
ReservedThreadExecutor reservedThreadExecutor = tp.getBean(ReservedThreadExecutor.class);
while (reservedThreadExecutor.getAvailable() != reserved && NanoTime.millisSince(start) < 10000)
{
try
{
Thread.sleep(50);
}
catch (InterruptedException ignored)
{
}
}
assertThat(reservedThreadExecutor.getAvailable(), is(reserved));
await().atMost(10, TimeUnit.SECONDS).until(() -> tp.getBean(ReservedThreadExecutor.class).getAvailable(), is(reserved));
}

private void waitForThreads(QueuedThreadPool tp, int threads)
{
long start = NanoTime.now();
while (tp.getThreads() != threads && NanoTime.millisSince(start) < 10000)
{
try
{
Thread.sleep(50);
}
catch (InterruptedException ignored)
{
}
}
assertThat(tp.getThreads(), is(threads));
await().atMost(10, TimeUnit.SECONDS).until(tp::getThreads, is(threads));
}

@Test
Expand Down

0 comments on commit 8f33389

Please sign in to comment.