diff --git a/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java b/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java index ecdf5a54e..0b7c35a80 100644 --- a/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java +++ b/plugins/casutg/src/main/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroup.java @@ -71,14 +71,29 @@ public void stop() { @Override public void tellThreadsToStop() { super.tellThreadsToStop(); + // stopThread() looks only through active threads, so wake idle pool threads directly. + // No OS interrupt here: a thread being released concurrently would get it mid-sample for (DynamicThread thread : poolThreads) { - stopThread(thread.getThreadName(), true); + thread.stop(); + //noinspection SynchronizationOnLocalVariableOrMethodParameter + synchronized (thread) { + thread.notify(); + } } } @Override public boolean verifyThreadsStopped() { boolean parent = super.verifyThreadsStopped(); + for (DynamicThread thread : poolThreads) { + if (thread.getOSThread() != null) { + try { + thread.getOSThread().join(WAIT_TO_DIE); + } catch (InterruptedException e) { + log.warn("Interrupted", e); + } + } + } log.info("Verify shutdown thread counts: " + threads.size() + "/" + poolThreads.size()); return parent && poolThreads.isEmpty(); } @@ -97,11 +112,16 @@ public boolean movedToPool(DynamicThread thread) { //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (thread) { try { - thread.wait(); + // releasedPoolThread() removes the thread before notify, so the guard also covers + // a release or stop that comes before this wait, and spurious wakeups + while (running && !thread.isStopping() && poolThreads.contains(thread)) { + thread.wait(); + } } catch (InterruptedException e) { log.debug("Interrupted", e); } } + poolThreads.remove(thread); ThreadCountsAccessor.incrNumberOfThreads(); return running; } diff --git a/plugins/casutg/src/test/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroupTest.java b/plugins/casutg/src/test/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroupTest.java index 841d26875..b38500529 100644 --- a/plugins/casutg/src/test/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroupTest.java +++ b/plugins/casutg/src/test/java/com/blazemeter/jmeter/threads/arrivals/ArrivalsThreadGroupTest.java @@ -127,5 +127,48 @@ public void testFlowRefael() throws IOException { atg.tellThreadsToStop(); atg.verifyThreadsStopped(); } -} + @Test + public void testStopWithIdlePoolThread() throws Exception { + ArrivalsThreadGroupEmul atg = startWithIdlePoolThread(); + try { + atg.tellThreadsToStop(); + assertTrue(atg.verifyThreadsStopped()); + } finally { + atg.stop(); + atg.testEnded(); + } + } + + @Test + public void testStopNowWithIdlePoolThread() throws Exception { + ArrivalsThreadGroupEmul atg = startWithIdlePoolThread(); + try { + atg.stop(); + assertTrue(atg.verifyThreadsStopped()); + } finally { + atg.testEnded(); + } + } + + private static ArrivalsThreadGroupEmul startWithIdlePoolThread() throws InterruptedException { + ArrivalsThreadGroupEmul atg = new ArrivalsThreadGroupEmul(); + atg.setName("TEST"); + atg.setTargetLevel("1"); + atg.setHold("60"); + ListedHashTree tree = new ListedHashTree(); + TestAction pauser = new TestAction(); + pauser.setAction(TestAction.PAUSE); + pauser.setTarget(TestAction.THREAD); + pauser.setDuration("100"); + tree.add(atg, pauser); + atg.testStarted(); + atg.start(0, null, tree, null); + long deadline = System.currentTimeMillis() + 5000; + while (atg.poolThreads.isEmpty()) { + assertTrue("no thread has moved to the pool", System.currentTimeMillis() < deadline); + Thread.sleep(10); + } + return atg; + } +}