diff --git a/components/encryption/src/test/java/org/cloudfoundry/credhub/services/RetryingEncryptionServiceTest.java b/components/encryption/src/test/java/org/cloudfoundry/credhub/services/RetryingEncryptionServiceTest.java index 4659fccb9..84457d6cd 100644 --- a/components/encryption/src/test/java/org/cloudfoundry/credhub/services/RetryingEncryptionServiceTest.java +++ b/components/encryption/src/test/java/org/cloudfoundry/credhub/services/RetryingEncryptionServiceTest.java @@ -2,6 +2,8 @@ import java.security.ProviderException; import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantReadWriteLock; import javax.crypto.IllegalBlockSizeException; @@ -17,6 +19,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -221,7 +224,7 @@ public void encryption_encryptionLocks_acquiresALunaUsageReadLock() throws Excep @Test public void whenUsingTwoThreads_wontRetryTwice() throws Exception { - final Object lock = new Object(); + final CountDownLatch latch = new CountDownLatch(1); final Thread firstThread = new Thread("first") { @Override public void run() { @@ -243,7 +246,8 @@ public void run() { } }; - subject = new RacingRetryingEncryptionServiceForTest(firstThread, secondThread, lock); + final RacingRetryingEncryptionServiceForTest racingSubject = new RacingRetryingEncryptionServiceForTest(firstThread, secondThread, latch); + subject = racingSubject; when(keySet.getActive()) .thenReturn(firstActiveKey); when(firstActiveKey.encrypt(anyString())) @@ -256,6 +260,7 @@ public void run() { firstThread.join(); secondThread.join(); + assertTrue(racingSubject.latchSignalled, "secondThread did not signal the latch within the timeout period"); verify(keySet, times(1)).reload(); } @@ -413,7 +418,7 @@ public void decryptionLocks_acquiresALunaUsageReadLock() throws Exception { @Test public void usingTwoThread_wontRetryTwice() throws Exception { - final Object lock = new Object(); + final CountDownLatch latch = new CountDownLatch(1); final Thread firstThread = new Thread("first") { @Override public void run() { @@ -435,7 +440,8 @@ public void run() { } }; - subject = new RacingRetryingEncryptionServiceForTest(firstThread, secondThread, lock); + final RacingRetryingEncryptionServiceForTest racingSubject = new RacingRetryingEncryptionServiceForTest(firstThread, secondThread, latch); + subject = racingSubject; when(keySet.get(activeKeyUuid)) .thenReturn(firstActiveKey); @@ -455,6 +461,7 @@ public void run() { firstThread.join(); secondThread.join(); + assertTrue(racingSubject.latchSignalled, "secondThread did not signal the latch within the timeout period"); verify(keySet, times(1)).reload(); } @@ -462,13 +469,14 @@ private class RacingRetryingEncryptionServiceForTest extends RetryingEncryptionS private final Thread firstThread; private final Thread secondThread; - private final Object lock; + private final CountDownLatch latch; + volatile boolean latchSignalled = true; - RacingRetryingEncryptionServiceForTest(final Thread firstThread, final Thread secondThread, final Object lock) { + RacingRetryingEncryptionServiceForTest(final Thread firstThread, final Thread secondThread, final CountDownLatch latch) { super(RetryingEncryptionServiceTest.this.keySet); this.firstThread = firstThread; this.secondThread = secondThread; - this.lock = lock; + this.latch = latch; } @Override @@ -476,14 +484,10 @@ public void setNeedsReconnectFlag() { try { if (Thread.currentThread().equals(firstThread)) { secondThread.start(); - synchronized (lock) { - lock.wait(); // pause the first thread - } + latchSignalled = latch.await(10, TimeUnit.SECONDS); // pause the first thread until secondThread signals Thread.sleep(10); // give thread two a chance to get all the way through the retry } else { - synchronized (lock) { - lock.notify(); // unpause the first thread - } + latch.countDown(); // unpause the first thread } } catch (final Exception e) { //do nothing