Don't store subscription or transport updates that are older than those already received. Also some small changes to DatabaseComponent impls for readability.

This commit is contained in:
akwizgran
2011-07-14 13:53:13 +01:00
parent 836d30f6df
commit fcedc34d10
10 changed files with 397 additions and 216 deletions

View File

@@ -71,8 +71,6 @@ public abstract class DatabaseComponentTest extends TestCase {
@Test
public void testSimpleCalls() throws DbException {
final Map<String, String> transports1 =
Collections.singletonMap("foo", "bar baz");
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
@@ -98,10 +96,6 @@ public abstract class DatabaseComponentTest extends TestCase {
will(returnValue(true));
oneOf(database).getTransports(txn, contactId);
will(returnValue(transports));
// setTransports(contactId, transports1)
oneOf(database).containsContact(txn, contactId);
will(returnValue(true));
oneOf(database).setTransports(txn, contactId, transports1);
// subscribe(groupId)
oneOf(database).addSubscription(txn, groupId);
// getSubscriptions()
@@ -122,7 +116,6 @@ public abstract class DatabaseComponentTest extends TestCase {
assertEquals(contactId, db.addContact(transports));
assertEquals(contacts, db.getContacts());
assertEquals(transports, db.getTransports(contactId));
db.setTransports(contactId, transports1);
db.subscribe(groupId);
assertEquals(subs, db.getSubscriptions());
db.unsubscribe(groupId);
@@ -509,7 +502,10 @@ public abstract class DatabaseComponentTest extends TestCase {
final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final BundleReader bundleReader = context.mock(BundleReader.class);
final Header header = context.mock(Header.class);
context.checking(new Expectations() {{
oneOf(bundleReader).getHeader();
will(returnValue(header));
// Check that the contact is still in the DB
oneOf(database).startTransaction();
will(returnValue(txn));
@@ -552,11 +548,16 @@ public abstract class DatabaseComponentTest extends TestCase {
// Subscriptions
oneOf(header).getSubscriptions();
will(returnValue(subs));
oneOf(database).setSubscriptions(txn, contactId, subs);
oneOf(header).getTimestamp();
will(returnValue(timestamp));
oneOf(database).setSubscriptions(txn, contactId, subs, timestamp);
// Transports
oneOf(header).getTransports();
will(returnValue(transports));
oneOf(database).setTransports(txn, contactId, transports);
oneOf(header).getTimestamp();
will(returnValue(timestamp));
oneOf(database).setTransports(txn, contactId, transports,
timestamp);
// Batches
oneOf(bundleReader).getNextBatch();
will(returnValue(batch));

View File

@@ -195,7 +195,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.setSubscriptions(txn, contactId, Collections.singleton(groupId), 1);
db.addMessage(txn, message);
db.setStatus(txn, contactId, messageId, Status.NEW);
db.commitTransaction(txn);
@@ -234,7 +234,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.setSubscriptions(txn, contactId, Collections.singleton(groupId), 1);
db.addMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.commitTransaction(txn);
@@ -293,7 +293,7 @@ public class H2DatabaseTest extends TestCase {
// The contact subscribing should make the message sendable
txn = db.startTransaction();
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.setSubscriptions(txn, contactId, Collections.singleton(groupId), 1);
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertTrue(it.hasNext());
assertEquals(messageId, it.next());
@@ -301,7 +301,7 @@ public class H2DatabaseTest extends TestCase {
// The contact unsubscribing should make the message unsendable
txn = db.startTransaction();
db.setSubscriptions(txn, contactId, Collections.<GroupId>emptySet());
db.setSubscriptions(txn, contactId, Collections.<GroupId>emptySet(), 2);
it = db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
assertFalse(it.hasNext());
db.commitTransaction(txn);
@@ -317,7 +317,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.setSubscriptions(txn, contactId, Collections.singleton(groupId), 1);
db.addMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -377,7 +377,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.setSubscriptions(txn, contactId, Collections.singleton(groupId), 1);
db.addMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -417,7 +417,7 @@ public class H2DatabaseTest extends TestCase {
Connection txn = db.startTransaction();
assertEquals(contactId, db.addContact(txn, null));
db.addSubscription(txn, groupId);
db.setSubscriptions(txn, contactId, Collections.singleton(groupId));
db.setSubscriptions(txn, contactId, Collections.singleton(groupId), 1);
db.addMessage(txn, message);
db.setSendability(txn, messageId, 1);
db.setStatus(txn, contactId, messageId, Status.NEW);
@@ -740,10 +740,10 @@ public class H2DatabaseTest extends TestCase {
transports = new TreeMap<String, String>();
transports.put("foo", "bar baz");
transports.put("bar", "baz quux");
db.setTransports(txn, contactId, transports);
db.setTransports(txn, contactId, transports, 1);
assertEquals(transports, db.getTransports(txn, contactId));
// Remove the transport details
db.setTransports(txn, contactId, null);
db.setTransports(txn, contactId, null, 2);
assertEquals(Collections.emptyMap(), db.getTransports(txn, contactId));
// Set the local transport details
db.setTransports(txn, transports);
@@ -756,6 +756,85 @@ public class H2DatabaseTest extends TestCase {
db.close();
}
@Test
public void testTransportsNotUpdatedIfTimestampIsOld() throws DbException {
Database<Connection> db = open(false);
// Add a contact with some transport details
Connection txn = db.startTransaction();
Map<String, String> transports = Collections.singletonMap("foo", "bar");
assertEquals(contactId, db.addContact(txn, transports));
assertEquals(transports, db.getTransports(txn, contactId));
// Replace the transport details using a timestamp of 2
Map<String, String> transports1 = new TreeMap<String, String>();
transports1.put("foo", "bar baz");
transports1.put("bar", "baz quux");
db.setTransports(txn, contactId, transports1, 2);
assertEquals(transports1, db.getTransports(txn, contactId));
// Try to replace the transport details using a timestamp of 1
Map<String, String> transports2 = new TreeMap<String, String>();
transports2.put("bar", "baz");
transports2.put("quux", "fnord");
db.setTransports(txn, contactId, transports2, 1);
// The old transports should still be there
assertEquals(transports1, db.getTransports(txn, contactId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testUpdateSubscriptions() throws DbException {
Database<Connection> db = open(false);
// Add a contact
Connection txn = db.startTransaction();
Map<String, String> transports = Collections.emptyMap();
assertEquals(contactId, db.addContact(txn, transports));
// Add some subscriptions
Set<GroupId> subs = new HashSet<GroupId>();
subs.add(new GroupId(TestUtils.getRandomId()));
subs.add(new GroupId(TestUtils.getRandomId()));
db.setSubscriptions(txn, contactId, subs, 1);
assertEquals(subs, db.getSubscriptions(txn, contactId));
// Update the subscriptions
Set<GroupId> subs1 = new HashSet<GroupId>();
subs1.add(new GroupId(TestUtils.getRandomId()));
subs1.add(new GroupId(TestUtils.getRandomId()));
db.setSubscriptions(txn, contactId, subs1, 2);
assertEquals(subs1, db.getSubscriptions(txn, contactId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testSubscriptionsNotUpdatedIfTimestampIsOld()
throws DbException {
Database<Connection> db = open(false);
// Add a contact
Connection txn = db.startTransaction();
Map<String, String> transports = Collections.emptyMap();
assertEquals(contactId, db.addContact(txn, transports));
// Add some subscriptions
Set<GroupId> subs = new HashSet<GroupId>();
subs.add(new GroupId(TestUtils.getRandomId()));
subs.add(new GroupId(TestUtils.getRandomId()));
db.setSubscriptions(txn, contactId, subs, 2);
assertEquals(subs, db.getSubscriptions(txn, contactId));
// Try to update the subscriptions using a timestamp of 1
Set<GroupId> subs1 = new HashSet<GroupId>();
subs1.add(new GroupId(TestUtils.getRandomId()));
subs1.add(new GroupId(TestUtils.getRandomId()));
db.setSubscriptions(txn, contactId, subs1, 1);
// The old subscriptions should still be there
assertEquals(subs, db.getSubscriptions(txn, contactId));
db.commitTransaction(txn);
db.close();
}
private Database<Connection> open(boolean resume) throws DbException {
final char[] passwordArray = passwordString.toCharArray();
Mockery context = new Mockery();