From de9787551854d71da5180e0266f19404e65d124f Mon Sep 17 00:00:00 2001 From: kcichonczyk <88378534+kcichonczyk@users.noreply.github.com> Date: Fri, 4 Aug 2023 09:03:56 +0200 Subject: [PATCH] [ACS-5756] Introduce locks to make sure the methods are not interfering with each other --- ...nticatorFactoryAdminConsoleAccessTest.java | 63 ++++++++++++++++--- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/remote-api/src/test/java/org/alfresco/repo/web/scripts/servlet/RemoteAuthenticatorFactoryAdminConsoleAccessTest.java b/remote-api/src/test/java/org/alfresco/repo/web/scripts/servlet/RemoteAuthenticatorFactoryAdminConsoleAccessTest.java index 2ab0b7fe95..0014ec6920 100644 --- a/remote-api/src/test/java/org/alfresco/repo/web/scripts/servlet/RemoteAuthenticatorFactoryAdminConsoleAccessTest.java +++ b/remote-api/src/test/java/org/alfresco/repo/web/scripts/servlet/RemoteAuthenticatorFactoryAdminConsoleAccessTest.java @@ -63,6 +63,11 @@ import javax.servlet.http.HttpServletResponse; import java.util.Collections; import java.util.HashSet; import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.Supplier; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.doAnswer; @@ -501,40 +506,78 @@ class BlockingRemoteUserMapper implements RemoteUserMapper, ActivateableBean private volatile int timePassed; private boolean isEnabled = true; + private final ReadWriteLock lock = new ReentrantReadWriteLock(); @Override public String getRemoteUser(HttpServletRequest request) { - long t1 = System.currentTimeMillis(); + doWithLock(lock.writeLock(), () -> { + long t1 = System.currentTimeMillis(); + try + { + Thread.sleep(BLOCKING_FOR_MILLIS); + } catch (InterruptedException ie) + { + wasInterrupted = true; + } finally + { + timePassed = (int) (System.currentTimeMillis() - t1); + } + }); + return null; + } + + private static void doWithLock(Lock lock, Runnable action) + { + getWithLock(lock, () -> + { + action.run(); + return null; + }); + } + + private static T getWithLock(Lock lock, Supplier supplier) + { + final boolean locked; try { - Thread.sleep(BLOCKING_FOR_MILLIS); + locked = lock.tryLock(BLOCKING_FOR_MILLIS * 2, TimeUnit.MILLISECONDS); } - catch (InterruptedException ie) + catch (InterruptedException e) { - wasInterrupted = true; + Thread.currentThread().interrupt(); + throw new IllegalStateException("Unexpected InterruptedException while acquiring " + lock); + } + + if (!locked) throw new IllegalStateException("Unexpected failure while acquiring " + lock); + + try + { + return supplier.get(); } finally { - timePassed = (int) (System.currentTimeMillis() - t1); + lock.unlock(); } - return null; } public boolean isWasInterrupted() { - return wasInterrupted; + return getWithLock(lock.readLock(), () -> wasInterrupted); } public int getTimePassed() { - return timePassed; + return getWithLock(lock.readLock(), () -> timePassed); } public void reset() { - wasInterrupted = false; - timePassed = 0; + doWithLock(lock.writeLock(), () -> + { + wasInterrupted = false; + timePassed = 0; + }); } @Override