Refactored DatabaseCleanerImpl to use Timer and TimerTask.

This commit is contained in:
akwizgran
2012-09-07 16:36:48 +01:00
parent 7a0e22d44c
commit f8183a4ce3
2 changed files with 52 additions and 41 deletions

View File

@@ -11,29 +11,51 @@ import org.junit.Test;
public class DatabaseCleanerImplTest extends BriarTestCase {
@Test
public void testCleanerRunsPeriodically() throws Exception {
final CountDownLatch latch = new CountDownLatch(5);
Callback callback = new Callback() {
public void checkFreeSpaceAndClean() throws DbException {
latch.countDown();
}
public boolean shouldCheckFreeSpace() {
return true;
}
};
DatabaseCleanerImpl cleaner = new DatabaseCleanerImpl();
// Start the cleaner
cleaner.startCleaning(callback, 10L);
// The database should be cleaned five times (allow 5s for system load)
assertTrue(latch.await(5, TimeUnit.SECONDS));
// Stop the cleaner
cleaner.stopCleaning();
}
@Test
public void testStoppingCleanerWakesItUp() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Callback callback = new Callback() {
public void checkFreeSpaceAndClean() throws DbException {
throw new IllegalStateException();
latch.countDown();
}
public boolean shouldCheckFreeSpace() {
latch.countDown();
return false;
return true;
}
};
DatabaseCleanerImpl cleaner = new DatabaseCleanerImpl();
long start = System.currentTimeMillis();
// Start the cleaner and check that shouldCheckFreeSpace() is called
cleaner.startCleaning(callback, 30L * 1000L);
// Start the cleaner
cleaner.startCleaning(callback, 10L * 1000L);
// The database should be cleaned once at startup
assertTrue(latch.await(5, TimeUnit.SECONDS));
// Stop the cleaner (it should be waiting between sweeps)
cleaner.stopCleaning();
long end = System.currentTimeMillis();
// Check that much less than 30 seconds expired
// Check that much less than 10 seconds expired
assertTrue(end - start < 10L * 1000L);
}
}