Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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()))
Expand All @@ -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();
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -455,35 +461,33 @@ 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();
}

private class RacingRetryingEncryptionServiceForTest extends RetryingEncryptionService {

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
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
Expand Down