Skip to content
Snippets Groups Projects
Commit 468db2a9 authored by akwizgran's avatar akwizgran
Browse files

Use a mock timer to test the database cleaner.

parent 44bb9134
No related branches found
No related tags found
No related merge requests found
package org.briarproject.db; package org.briarproject.db;
import static java.util.concurrent.TimeUnit.SECONDS; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CountDownLatch;
import org.briarproject.BriarTestCase; import org.briarproject.BriarTestCase;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.system.Timer; import org.briarproject.api.system.Timer;
import org.briarproject.db.DatabaseCleaner.Callback; import org.briarproject.db.DatabaseCleaner.Callback;
import org.briarproject.system.SystemTimer; import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.Test; import org.junit.Test;
// FIXME: Use a mock timer
public class DatabaseCleanerImplTest extends BriarTestCase { public class DatabaseCleanerImplTest extends BriarTestCase {
@Test @Test
public void testCleanerRunsPeriodically() throws Exception { public void testCleanerRunsPeriodically() throws Exception {
final CountDownLatch latch = new CountDownLatch(5); final AtomicInteger cleans = new AtomicInteger(0);
Callback callback = new Callback() { Callback callback = new Callback() {
public void checkFreeSpaceAndClean() throws DbException { boolean check = true;
latch.countDown();
}
public boolean shouldCheckFreeSpace() {
return true;
}
};
Timer timer = new SystemTimer();
DatabaseCleanerImpl cleaner = new DatabaseCleanerImpl(timer);
// Start the cleaner
cleaner.startCleaning(callback, 10);
// The database should be cleaned five times (allow 5s for system load)
assertTrue(latch.await(5, 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 { public void checkFreeSpaceAndClean() throws DbException {
latch.countDown(); cleans.incrementAndGet();
} }
public boolean shouldCheckFreeSpace() { public boolean shouldCheckFreeSpace() {
return true; // Alternate between true and false
check = !check;
return !check;
} }
}; };
Timer timer = new SystemTimer(); Mockery context = new Mockery();
DatabaseCleanerImpl cleaner = new DatabaseCleanerImpl(timer); final Timer timer = context.mock(Timer.class);
long start = System.currentTimeMillis(); final DatabaseCleanerImpl cleaner = new DatabaseCleanerImpl(timer);
// Start the cleaner context.checking(new Expectations() {{
cleaner.startCleaning(callback, 10 * 1000); oneOf(timer).scheduleAtFixedRate(cleaner, 0, 10);
// The database should be cleaned once at startup oneOf(timer).cancel();
assertTrue(latch.await(5, SECONDS)); }});
// Stop the cleaner (it should be waiting between sweeps) // Start the cleaner - it should schedule itself with the timer
cleaner.startCleaning(callback, 10);
// Call the cleaner's run method six times
for(int i = 0; i < 6; i++) cleaner.run();
// Stop the cleaner - it should cancel the timer
cleaner.stopCleaning(); cleaner.stopCleaning();
long end = System.currentTimeMillis(); // The database should have been cleaned three times
// Check that much less than 10 seconds expired assertEquals(3, cleans.get());
assertTrue(end - start < 10 * 1000); context.assertIsSatisfied();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment