ACS 9256 improve async tests stability (#3191)

* ACS-9256 Improved stability in DynamicallySizedThreadPoolExecutorTest

* ACS-9256 Removed unused unstable test in SpringAwareUserTransactionTest

* ACS-9256 Improved stability in DynamicallySizedThreadPoolExecutorTest

* ACS-9256 Improved stability in ActionServiceImplTest and RuleServiceCoverageTest

* ACS-9256 Improved stability in ActionTrackingServiceImplTest

* ACS-9256 Improved performance in ComparePropertyValueEvaluatorTest

* ACS-9256 Improved performance in LockServiceImplTest

* ACS-9256 Improved stability in LockBehaviourImplTest

* ACS-9256 Improved stability in ContentMetadataExtracterTest

* ACS-9256 Removed unstable and unused tests

* ACS-9256 Improve stability in CachedContentCleanupJobTest

* ACS-9256 Pre-commit fixes
This commit is contained in:
Gerard Olenski
2025-02-17 08:58:04 +01:00
committed by GitHub
parent 9e5a030b6f
commit 75d5201af1
14 changed files with 7654 additions and 8320 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
* Copyright (C) 2005-2025 Alfresco Software Limited.
*
* This file is part of Alfresco
*
@@ -18,6 +18,9 @@
*/
package org.alfresco.util;
import static org.awaitility.Awaitility.await;
import java.time.Duration;
import java.util.Map.Entry;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
@@ -26,20 +29,20 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import junit.framework.TestCase;
/**
* Tests for our instance of {@link java.util.concurrent.ThreadPoolExecutor}
*
*
* @author Nick Burch
*/
public class DynamicallySizedThreadPoolExecutorTest extends TestCase
{
private static Log logger = LogFactory.getLog(DynamicallySizedThreadPoolExecutorTest.class);
private static final Duration MAX_WAIT_TIMEOUT = Duration.ofSeconds(1);
private static final Log logger = LogFactory.getLog(DynamicallySizedThreadPoolExecutorTest.class);
private static final int DEFAULT_KEEP_ALIVE_TIME = 90;
@Override
@@ -48,9 +51,9 @@ public class DynamicallySizedThreadPoolExecutorTest extends TestCase
SleepUntilAllWake.reset();
}
public void testUpToCore() throws Exception
public void testUpToCore()
{
DynamicallySizedThreadPoolExecutor exec = createInstance(5,10, DEFAULT_KEEP_ALIVE_TIME);
DynamicallySizedThreadPoolExecutor exec = createInstance(5, 10, DEFAULT_KEEP_ALIVE_TIME);
assertEquals(0, exec.getPoolSize());
exec.execute(new SleepUntilAllWake());
@@ -61,15 +64,15 @@ public class DynamicallySizedThreadPoolExecutorTest extends TestCase
assertEquals(4, exec.getPoolSize());
exec.execute(new SleepUntilAllWake());
assertEquals(5, exec.getPoolSize());
SleepUntilAllWake.wakeAll();
Thread.sleep(100);
waitForPoolSizeEquals(exec, 5);
assertEquals(5, exec.getPoolSize());
}
public void testPastCoreButNotHugeQueue() throws Exception
public void testPastCoreButNotHugeQueue()
{
DynamicallySizedThreadPoolExecutor exec = createInstance(5,10, DEFAULT_KEEP_ALIVE_TIME);
DynamicallySizedThreadPoolExecutor exec = createInstance(5, 10, DEFAULT_KEEP_ALIVE_TIME);
assertEquals(0, exec.getPoolSize());
assertEquals(0, exec.getQueue().size());
@@ -80,7 +83,7 @@ public class DynamicallySizedThreadPoolExecutorTest extends TestCase
exec.execute(new SleepUntilAllWake());
assertEquals(5, exec.getPoolSize());
assertEquals(0, exec.getQueue().size());
// Need to hit max pool size before it adds more
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
@@ -89,20 +92,20 @@ public class DynamicallySizedThreadPoolExecutorTest extends TestCase
exec.execute(new SleepUntilAllWake());
assertEquals(5, exec.getPoolSize());
assertEquals(5, exec.getQueue().size());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
assertEquals(5, exec.getPoolSize());
assertEquals(7, exec.getQueue().size());
SleepUntilAllWake.wakeAll();
Thread.sleep(100);
waitForPoolSizeEquals(exec, 5);
assertEquals(5, exec.getPoolSize());
}
public void testToExpandQueue() throws Exception
{
DynamicallySizedThreadPoolExecutor exec = createInstance(2,4,1);
DynamicallySizedThreadPoolExecutor exec = createInstance(2, 4, 5);
assertEquals(0, exec.getPoolSize());
assertEquals(0, exec.getQueue().size());
@@ -110,166 +113,37 @@ public class DynamicallySizedThreadPoolExecutorTest extends TestCase
exec.execute(new SleepUntilAllWake());
assertEquals(2, exec.getPoolSize());
assertEquals(0, exec.getQueue().size());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
assertEquals(2, exec.getPoolSize());
assertEquals(3, exec.getQueue().size());
// Next should add one
exec.execute(new SleepUntilAllWake());
Thread.sleep(20); // Let the new thread spin up
waitForPoolSizeEquals(exec, 3); // Let the new thread spin up
assertEquals(3, exec.getPoolSize());
assertEquals(3, exec.getQueue().size());
// And again
exec.execute(new SleepUntilAllWake());
Thread.sleep(20); // Let the new thread spin up
waitForPoolSizeEquals(exec, 4); // Let the new thread spin up
assertEquals(4, exec.getPoolSize());
assertEquals(3, exec.getQueue().size());
// But no more will be added, as we're at max
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
assertEquals(4, exec.getPoolSize());
assertEquals(6, exec.getQueue().size());
SleepUntilAllWake.wakeAll();
Thread.sleep(100);
// All threads still running, as 1 second timeout
assertEquals(4, exec.getPoolSize());
}
public void offTestToExpandThenContract() throws Exception
{
DynamicallySizedThreadPoolExecutor exec = createInstance(2,4,1);
exec.setKeepAliveTime(30, TimeUnit.MILLISECONDS);
assertEquals(0, exec.getPoolSize());
assertEquals(0, exec.getQueue().size());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
assertEquals(2, exec.getPoolSize());
assertEquals(0, exec.getQueue().size());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
assertEquals(2, exec.getPoolSize());
assertEquals(3, exec.getQueue().size());
// Next should add one
exec.execute(new SleepUntilAllWake());
Thread.sleep(20); // Let the new thread spin up
assertEquals(3, exec.getPoolSize());
assertEquals(3, exec.getQueue().size());
SleepUntilAllWake.wakeAll();
Thread.sleep(100);
// And again
exec.execute(new SleepUntilAllWake());
Thread.sleep(20); // Let the new thread spin up
// All threads still running, as 5 second timeout
assertEquals(4, exec.getPoolSize());
assertEquals(3, exec.getQueue().size());
// But no more will be added, as we're at max
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
assertEquals(4, exec.getPoolSize());
assertEquals(6, exec.getQueue().size());
SleepUntilAllWake.wakeAll();
Thread.sleep(100);
// Wait longer than the timeout without any work, which should
// let all the extra threads go away
// (Depending on how closely your JVM follows the specification,
// we may fall back to the core size which is correct, or we
// may go to zero which is wrong, but hey, it's the JVM...)
logger.debug("Core pool size is " + exec.getCorePoolSize());
logger.debug("Current pool size is " + exec.getPoolSize());
logger.debug("Queue size is " + exec.getQueue().size());
assertTrue(
"Pool size should be 0-2 as everything is idle, was " + exec.getPoolSize(),
exec.getPoolSize() >= 0
);
assertTrue(
"Pool size should be 0-2 as everything is idle, was " + exec.getPoolSize(),
exec.getPoolSize() <= 2
);
SleepUntilAllWake.reset();
// Add 2 new jobs, will stay/ go to at 2 threads
assertEquals(0, exec.getQueue().size());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
// Let the idle threads grab them, then check
Thread.sleep(20);
assertEquals(2, exec.getPoolSize());
assertEquals(0, exec.getQueue().size());
// 3 more, still at 2 threads
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
assertEquals(2, exec.getPoolSize());
assertEquals(3, exec.getQueue().size());
// And again wait for it all
SleepUntilAllWake.wakeAll();
Thread.sleep(100);
assertEquals(2, exec.getPoolSize());
// Now decrease the overall pool size
// Will rise and fall to there now
exec.setCorePoolSize(1);
// Run a quick job, to ensure that the
// "can I kill one yet" logic is applied
SleepUntilAllWake.reset();
exec.execute(new SleepUntilAllWake());
SleepUntilAllWake.wakeAll();
Thread.sleep(100);
assertEquals(1, exec.getPoolSize());
assertEquals(0, exec.getQueue().size());
SleepUntilAllWake.reset();
// Push enough on to go up to 4 active threads
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
exec.execute(new SleepUntilAllWake());
Thread.sleep(20); // Let the new threads spin up
assertEquals(4, exec.getPoolSize());
assertEquals(6, exec.getQueue().size());
// Wait for them all to finish, should drop back to 1 now
// (Or zero, if your JVM can't read the specification...)
SleepUntilAllWake.wakeAll();
Thread.sleep(100);
assertTrue(
"Pool size should be 0 or 1 as everything is idle, was " + exec.getPoolSize(),
exec.getPoolSize() >= 0
);
assertTrue(
"Pool size should be 0 or 1 as everything is idle, was " + exec.getPoolSize(),
exec.getPoolSize() <= 1
);
}
private DynamicallySizedThreadPoolExecutor createInstance(int corePoolSize, int maximumPoolSize, int keepAliveTime)
@@ -291,6 +165,11 @@ public class DynamicallySizedThreadPoolExecutorTest extends TestCase
new ThreadPoolExecutor.CallerRunsPolicy());
}
private void waitForPoolSizeEquals(DynamicallySizedThreadPoolExecutor exec, int expectedSize)
{
await().atMost(MAX_WAIT_TIMEOUT).until(() -> exec.getPoolSize() == expectedSize);
}
public static class SleepUntilAllWake implements Runnable
{
private static ConcurrentMap<String, Thread> sleeping = new ConcurrentHashMap<String, Thread>();
@@ -299,31 +178,33 @@ public class DynamicallySizedThreadPoolExecutorTest extends TestCase
@Override
public void run()
{
if(allAwake) return;
if (allAwake)
return;
// Track us, and wait for the bang
logger.debug("Adding thread: " + Thread.currentThread().getName());
sleeping.put(Thread.currentThread().getName(), Thread.currentThread());
try
{
Thread.sleep(30*1000);
Thread.sleep(30 * 1000);
System.err.println("Warning - Thread finished sleeping without wake!");
}
catch(InterruptedException e)
catch (InterruptedException e)
{
logger.debug("Interrupted thread: " + Thread.currentThread().getName());
}
}
public static void wakeAll()
{
allAwake = true;
for(Entry<String, Thread> t : sleeping.entrySet())
for (Entry<String, Thread> t : sleeping.entrySet())
{
logger.debug("Interrupting thread: " + t.getKey());
t.getValue().interrupt();
}
}
public static void reset()
{
logger.debug("Resetting.");

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2023 Alfresco Software Limited.
* Copyright (C) 2005-2025 Alfresco Software Limited.
*
* This file is part of Alfresco
*
@@ -20,13 +20,11 @@ package org.alfresco.util.transaction;
import java.util.NoSuchElementException;
import java.util.Objects;
import jakarta.transaction.RollbackException;
import jakarta.transaction.Status;
import jakarta.transaction.UserTransaction;
import junit.framework.TestCase;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.TransactionDefinition;
@@ -35,21 +33,20 @@ import org.springframework.transaction.support.AbstractPlatformTransactionManage
import org.springframework.transaction.support.DefaultTransactionStatus;
/**
* @see org.alfresco.util.transaction.SpringAwareUserTransaction
*
* @author Derek Hulley
* @see org.alfresco.util.transaction.SpringAwareUserTransaction
*/
public class SpringAwareUserTransactionTest extends TestCase
{
private DummyTransactionManager transactionManager;
private FailingTransactionManager failingTransactionManager;
private UserTransaction txn;
public SpringAwareUserTransactionTest()
{
super();
}
@Override
protected void setUp() throws Exception
{
@@ -57,7 +54,7 @@ public class SpringAwareUserTransactionTest extends TestCase
failingTransactionManager = new FailingTransactionManager();
txn = getTxn();
}
private UserTransaction getTxn()
{
return new SpringAwareUserTransaction(
@@ -67,13 +64,13 @@ public class SpringAwareUserTransactionTest extends TestCase
TransactionDefinition.PROPAGATION_REQUIRED,
TransactionDefinition.TIMEOUT_DEFAULT);
}
public void testSetUp() throws Exception
{
assertNotNull(transactionManager);
assertNotNull(txn);
}
private void checkNoStatusOnThread()
{
try
@@ -86,7 +83,7 @@ public class SpringAwareUserTransactionTest extends TestCase
// expected
}
}
public void testNoTxnStatus() throws Exception
{
checkNoStatusOnThread();
@@ -134,7 +131,7 @@ public class SpringAwareUserTransactionTest extends TestCase
}
checkNoStatusOnThread();
}
public void testSimpleTxnWithRollback() throws Exception
{
testNoTxnStatus();
@@ -156,7 +153,7 @@ public class SpringAwareUserTransactionTest extends TestCase
transactionManager.getStatus());
checkNoStatusOnThread();
}
public void testNoBeginCommit() throws Exception
{
testNoTxnStatus();
@@ -171,7 +168,7 @@ public class SpringAwareUserTransactionTest extends TestCase
}
checkNoStatusOnThread();
}
public void testPostRollbackCommitDetection() throws Exception
{
testNoTxnStatus();
@@ -189,7 +186,7 @@ public class SpringAwareUserTransactionTest extends TestCase
}
checkNoStatusOnThread();
}
public void testPostSetRollbackOnlyCommitDetection() throws Exception
{
testNoTxnStatus();
@@ -208,7 +205,7 @@ public class SpringAwareUserTransactionTest extends TestCase
}
checkNoStatusOnThread();
}
public void testMismatchedBeginCommit() throws Exception
{
UserTransaction txn1 = getTxn();
@@ -218,18 +215,18 @@ public class SpringAwareUserTransactionTest extends TestCase
txn1.begin();
txn2.begin();
txn2.commit();
txn1.commit();
checkNoStatusOnThread();
txn1 = getTxn();
txn2 = getTxn();
txn1.begin();
txn2.begin();
try
{
txn1.commit();
@@ -245,58 +242,6 @@ public class SpringAwareUserTransactionTest extends TestCase
checkNoStatusOnThread();
}
/**
* Test for leaked transactions (no guarantee it will succeed due to reliance
* on garbage collector), so disabled by default.
*
* Also, if it succeeds, transaction call stack tracing will be enabled
* potentially hitting the performance of all subsequent tests.
*
* @throws Exception
*/
public void xtestLeakedTransactionLogging() throws Exception
{
assertFalse(SpringAwareUserTransaction.isCallStackTraced());
TrxThread t1 = new TrxThread();
t1.start();
System.gc();
Thread.sleep(1000);
TrxThread t2 = new TrxThread();
t2.start();
System.gc();
Thread.sleep(1000);
assertTrue(SpringAwareUserTransaction.isCallStackTraced());
TrxThread t3 = new TrxThread();
t3.start();
System.gc();
Thread.sleep(3000);
System.gc();
Thread.sleep(3000);
}
private class TrxThread extends Thread
{
public void run()
{
try
{
getTrx();
}
catch (Exception e) {}
}
public void getTrx() throws Exception
{
UserTransaction txn = getTxn();
txn.begin();
txn = null;
}
}
public void testConnectionPoolException() throws Exception
{
testNoTxnStatus();
@@ -311,7 +256,7 @@ public class SpringAwareUserTransactionTest extends TestCase
// Expected fail
}
}
private UserTransaction getFailingTxn()
{
return new SpringAwareUserTransaction(
@@ -321,7 +266,7 @@ public class SpringAwareUserTransactionTest extends TestCase
TransactionDefinition.PROPAGATION_REQUIRED,
TransactionDefinition.TIMEOUT_DEFAULT);
}
public void testTransactionListenerOrder() throws Throwable
{
testNoTxnStatus();
@@ -360,12 +305,12 @@ public class SpringAwareUserTransactionTest extends TestCase
}
checkNoStatusOnThread();
}
private static class TestTransactionListener extends TransactionListenerAdapter
{
private final String name;
private final StringBuffer buffer;
public TestTransactionListener(String name, StringBuffer buffer)
{
Objects.requireNonNull(name);
@@ -373,18 +318,18 @@ public class SpringAwareUserTransactionTest extends TestCase
this.name = name;
this.buffer = buffer;
}
@Override
public void beforeCommit(boolean readOnly)
{
buffer.append(name);
}
public String getName()
{
return name;
}
@Override
public boolean equals(Object obj)
{
@@ -394,17 +339,17 @@ public class SpringAwareUserTransactionTest extends TestCase
}
return false;
}
@Override
public int hashCode()
{
return name.hashCode();
}
}
/**
* Used to check that the transaction manager is being called correctly
*
*
* @author Derek Hulley
*/
@SuppressWarnings("serial")
@@ -412,7 +357,7 @@ public class SpringAwareUserTransactionTest extends TestCase
{
private int status = Status.STATUS_NO_TRANSACTION;
private Object txn = new Object();
/**
* @return Returns one of the {@link Status Status.STATUS_XXX} constants
*/
@@ -441,10 +386,10 @@ public class SpringAwareUserTransactionTest extends TestCase
status = Status.STATUS_ROLLEDBACK;
}
}
/**
* Throws {@link NoSuchElementException} on begin()
*
*
* @author alex.mukha
*/
private static class FailingTransactionManager extends AbstractPlatformTransactionManager
@@ -452,7 +397,7 @@ public class SpringAwareUserTransactionTest extends TestCase
private static final long serialVersionUID = 1L;
private int status = Status.STATUS_NO_TRANSACTION;
private Object txn = new Object();
/**
* @return Returns one of the {@link Status Status.STATUS_XXX} constants
*/