Factored out the database cleaner.

This commit is contained in:
akwizgran
2011-07-04 18:11:27 +01:00
parent 390b316724
commit eb752ada62
9 changed files with 242 additions and 49 deletions

View File

@@ -13,6 +13,7 @@
<path refid='test-classes'/>
<path refid='util-classes'/>
</classpath>
<test name='net.sf.briar.db.DatabaseCleanerImplTest'/>
<test name='net.sf.briar.db.H2DatabaseTest'/>
<test name='net.sf.briar.i18n.FontManagerTest'/>
<test name='net.sf.briar.i18n.I18nTest'/>

View File

@@ -0,0 +1,45 @@
package net.sf.briar.db;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import net.sf.briar.api.db.DbException;
import net.sf.briar.db.DatabaseCleaner.Callback;
import org.junit.Test;
public class DatabaseCleanerImplTest extends TestCase {
@Test
public void testStoppingCleanerWakesItUp() throws DbException {
final CountDownLatch latch = new CountDownLatch(1);
Callback callback = new Callback() {
public void checkFreeSpaceAndClean() throws DbException {
throw new IllegalStateException();
}
public boolean shouldCheckFreeSpace() {
latch.countDown();
return false;
}
};
// Configure the cleaner to wait for 30 seconds between sweeps
DatabaseCleanerImpl cleaner =
new DatabaseCleanerImpl(callback, 30 * 1000);
long start = System.currentTimeMillis();
// Start the cleaner and check that shouldCheckFreeSpace() is called
cleaner.startCleaning();
try {
assertTrue(latch.await(5, TimeUnit.SECONDS));
} catch(InterruptedException e) {
assertTrue(false);
}
// Stop the cleaner (it should be waiting between sweeps)
cleaner.stopCleaning();
long end = System.currentTimeMillis();
// Check that much less than 30 seconds expired
assertTrue(end - start < 10 * 1000);
}
}

View File

@@ -7,6 +7,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import junit.framework.TestCase;
import net.sf.briar.TestUtils;
@@ -586,6 +587,88 @@ public class H2DatabaseTest extends TestCase {
context.assertIsSatisfied();
}
@Test
public void testCloseWaitsForCommit() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
final AtomicBoolean transactionFinished = new AtomicBoolean(false);
final AtomicBoolean closed = new AtomicBoolean(false);
final AtomicBoolean error = new AtomicBoolean(false);
// Create a new database
final Database<Connection> db = open(false, messageFactory);
// Start a transaction
Connection txn = db.startTransaction();
// In another thread, close the database
Thread t = new Thread() {
public void run() {
try {
db.close();
closed.set(true);
if(!transactionFinished.get()) error.set(true);
} catch(DbException e) {
error.set(true);
}
}
};
t.start();
// Do whatever the transaction needs to do
try {
Thread.sleep(100);
} catch(InterruptedException ignored) {}
transactionFinished.set(true);
// Commit the transaction
db.commitTransaction(txn);
// The other thread should now terminate
try {
t.join(10000);
} catch(InterruptedException ignored) {}
assertTrue(closed.get());
// Check that the other thread didn't encounter an error
assertFalse(error.get());
}
@Test
public void testCloseWaitsForAbort() throws DbException {
Mockery context = new Mockery();
MessageFactory messageFactory = context.mock(MessageFactory.class);
final AtomicBoolean transactionFinished = new AtomicBoolean(false);
final AtomicBoolean closed = new AtomicBoolean(false);
final AtomicBoolean error = new AtomicBoolean(false);
// Create a new database
final Database<Connection> db = open(false, messageFactory);
// Start a transaction
Connection txn = db.startTransaction();
// In another thread, close the database
Thread t = new Thread() {
public void run() {
try {
db.close();
closed.set(true);
if(!transactionFinished.get()) error.set(true);
} catch(DbException e) {
error.set(true);
}
}
};
t.start();
// Do whatever the transaction needs to do
try {
Thread.sleep(100);
} catch(InterruptedException ignored) {}
transactionFinished.set(true);
// Abort the transaction
db.abortTransaction(txn);
// The other thread should now terminate
try {
t.join(10000);
} catch(InterruptedException ignored) {}
assertTrue(closed.get());
// Check that the other thread didn't encounter an error
assertFalse(error.get());
}
private Database<Connection> open(boolean resume,
MessageFactory messageFactory) throws DbException {
final char[] passwordArray = passwordString.toCharArray();