move rolloverFrequency directly to secretProvider.init

wrap the waiting mechanism into the TRolloverSignerSecretProvider class
This commit is contained in:
isa 2020-06-22 11:39:50 +04:30
parent 03c1cbb676
commit 6f6e923fc5
1 changed files with 25 additions and 11 deletions

View File

@ -13,14 +13,16 @@
*/
package org.apache.hadoop.security.authentication.util;
import org.apache.curator.shaded.com.google.common.annotations.VisibleForTesting;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
public class TestRolloverSignerSecretProvider {
@Test
public void testGetAndRollSecrets() throws Exception {
long rolloverFrequency = 2 * 1000; // rollover every 2 sec
byte[] secret1 = "doctor".getBytes();
byte[] secret2 = "who".getBytes();
byte[] secret3 = "tardis".getBytes();
@ -28,7 +30,7 @@ public class TestRolloverSignerSecretProvider {
new TRolloverSignerSecretProvider(
new byte[][]{secret1, secret2, secret3});
try {
secretProvider.init(null, null, rolloverFrequency);
secretProvider.init(null, null, TimeUnit.SECONDS.toMillis(2));
byte[] currentSecret = secretProvider.getCurrentSecret();
byte[][] allSecrets = secretProvider.getAllSecrets();
@ -36,9 +38,7 @@ public class TestRolloverSignerSecretProvider {
Assert.assertEquals(2, allSecrets.length);
Assert.assertArrayEquals(secret1, allSecrets[0]);
Assert.assertNull(allSecrets[1]);
synchronized (secretProvider.monitor){
secretProvider.monitor.wait();
}
secretProvider.waitUntilNewSecret();
currentSecret = secretProvider.getCurrentSecret();
allSecrets = secretProvider.getAllSecrets();
@ -46,9 +46,7 @@ public class TestRolloverSignerSecretProvider {
Assert.assertEquals(2, allSecrets.length);
Assert.assertArrayEquals(secret2, allSecrets[0]);
Assert.assertArrayEquals(secret1, allSecrets[1]);
synchronized (secretProvider.monitor){
secretProvider.monitor.wait();
}
secretProvider.waitUntilNewSecret();
currentSecret = secretProvider.getCurrentSecret();
allSecrets = secretProvider.getAllSecrets();
@ -65,7 +63,11 @@ public class TestRolloverSignerSecretProvider {
private byte[][] newSecretSequence;
private int newSecretSequenceIndex;
final Object monitor = new Object();
/**
* This monitor objects used to notify the thread waiting for new secret.
*/
private final Object rolloverMonitor = new Object();
TRolloverSignerSecretProvider(byte[][] newSecretSequence) {
super();
@ -75,11 +77,23 @@ public class TestRolloverSignerSecretProvider {
@Override
protected byte[] generateNewSecret() {
synchronized (monitor){
monitor.notify();
synchronized (rolloverMonitor) {
rolloverMonitor.notify();
}
return newSecretSequence[newSecretSequenceIndex++];
}
/**
* cause the current thread to sleep until the generateNewSecret method called
*
* @throws InterruptedException if any thread interrupted the current thread before or while the current thread
* was waiting for a notification.
*/
@VisibleForTesting
private void waitUntilNewSecret() throws InterruptedException {
synchronized (rolloverMonitor) {
rolloverMonitor.wait();
}
}
}
}