KeyRotatorImpl looks a *lot* like DatabaseCleanerImpl.

This commit is contained in:
akwizgran
2012-09-07 16:58:03 +01:00
parent f8183a4ce3
commit eb360475aa
7 changed files with 125 additions and 10 deletions

View File

@@ -0,0 +1,53 @@
package net.sf.briar.db;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.db.DbException;
import net.sf.briar.db.KeyRotator.Callback;
import org.junit.Test;
public class KeyRotatorImplTest extends BriarTestCase {
@Test
public void testCleanerRunsPeriodically() throws Exception {
final CountDownLatch latch = new CountDownLatch(5);
Callback callback = new Callback() {
public void rotateKeys() throws DbException {
latch.countDown();
}
};
KeyRotatorImpl cleaner = new KeyRotatorImpl();
// Start the rotator
cleaner.startRotating(callback, 10L);
// The keys should be rotated five times (allow 5 secs for system load)
assertTrue(latch.await(5, TimeUnit.SECONDS));
// Stop the rotator
cleaner.stopRotating();
}
@Test
public void testStoppingCleanerWakesItUp() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Callback callback = new Callback() {
public void rotateKeys() throws DbException {
latch.countDown();
}
};
KeyRotatorImpl cleaner = new KeyRotatorImpl();
long start = System.currentTimeMillis();
// Start the rotator
cleaner.startRotating(callback, 10L * 1000L);
// The keys should be rotated once at startup
assertTrue(latch.await(5, TimeUnit.SECONDS));
// Stop the rotator (it should be waiting between rotations)
cleaner.stopRotating();
long end = System.currentTimeMillis();
// Check that much less than 10 seconds expired
assertTrue(end - start < 10L * 1000L);
}
}