mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
Refactored DatabaseCleanerImpl to use Timer and TimerTask.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user