Further progress towards incremental subscription updates.

This commit is contained in:
akwizgran
2012-05-18 23:54:03 +02:00
parent b4f0da53b5
commit 1ca4ea9dcd
16 changed files with 340 additions and 405 deletions

View File

@@ -25,6 +25,7 @@ import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageFactory;
import net.sf.briar.api.protocol.MessageId;
@@ -172,8 +173,8 @@ public class ProtocolIntegrationTest extends BriarTestCase {
Map<Group, Long> subs = new LinkedHashMap<Group, Long>();
subs.put(group, 0L);
subs.put(group1, 0L);
SubscriptionUpdate s = packetFactory.createSubscriptionUpdate(subs,
timestamp);
SubscriptionUpdate s = packetFactory.createSubscriptionUpdate(
Collections.<GroupId, GroupId>emptyMap(), subs, 0L, timestamp);
writer.writeSubscriptionUpdate(s);
TransportUpdate t = packetFactory.createTransportUpdate(transports,

View File

@@ -802,35 +802,6 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
context.assertIsSatisfied();
}
@Test
public void testSubscriptionUpdateNotSentUnlessDue() throws Exception {
final long now = System.currentTimeMillis();
Mockery context = new Mockery();
@SuppressWarnings("unchecked")
final Database<Object> database = context.mock(Database.class);
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
final PacketFactory packetFactory = context.mock(PacketFactory.class);
context.checking(new Expectations() {{
allowing(database).startTransaction();
will(returnValue(txn));
allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId);
will(returnValue(true));
// Check whether an update is due
oneOf(database).getSubscriptionsModified(txn, contactId);
will(returnValue(now - 1L));
oneOf(database).getSubscriptionsSent(txn, contactId);
will(returnValue(now));
}});
DatabaseComponent db = createDatabaseComponent(database, cleaner,
shutdown, packetFactory);
assertNull(db.generateSubscriptionUpdate(contactId));
context.assertIsSatisfied();
}
@Test
public void testGenerateSubscriptionUpdate() throws Exception {
Mockery context = new Mockery();
@@ -847,19 +818,21 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
allowing(database).commitTransaction(txn);
allowing(database).containsContact(txn, contactId);
will(returnValue(true));
// Check whether an update is due
oneOf(database).getSubscriptionsModified(txn, contactId);
will(returnValue(0L));
oneOf(database).getSubscriptionsSent(txn, contactId);
will(returnValue(0L));
// Get the visible subscriptions
oneOf(database).getVisibleSubscriptions(txn, contactId);
will(returnValue(Collections.singletonMap(group, 0L)));
oneOf(database).setSubscriptionsSent(with(txn), with(contactId),
// Get the visible holes and subscriptions
oneOf(database).getVisibleHoles(with(txn), with(contactId),
with(any(long.class)));
will(returnValue(Collections.<GroupId, GroupId>emptyMap()));
oneOf(database).getVisibleSubscriptions(with(txn), with(contactId),
with(any(long.class)));
will(returnValue(Collections.singletonMap(group, 0L)));
// Get the expiry time
oneOf(database).getExpiryTime(txn);
will(returnValue(0L));
// Create the packet
oneOf(packetFactory).createSubscriptionUpdate(
with(Collections.<GroupId, GroupId>emptyMap()),
with(Collections.singletonMap(group, 0L)),
with(any(long.class)),
with(any(long.class)));
will(returnValue(subscriptionUpdate));
}});
@@ -1557,9 +1530,6 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
oneOf(database).getContacts(txn);
will(returnValue(both));
oneOf(database).removeVisibility(txn, contactId1, groupId);
oneOf(database).setSubscriptionsModified(with(txn),
with(Collections.singletonList(contactId1)),
with(any(long.class)));
oneOf(database).commitTransaction(txn);
oneOf(listener).eventOccurred(with(any(
SubscriptionsUpdatedEvent.class)));

View File

@@ -1628,37 +1628,6 @@ public class H2DatabaseTest extends BriarTestCase {
db.close();
}
@Test
public void testTimestamps() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact
assertEquals(contactId, db.addContact(txn, inSecret, outSecret, erase));
// The subscription and transport timestamps should be initialised to 0
assertEquals(0L, db.getSubscriptionsModified(txn, contactId));
assertEquals(0L, db.getSubscriptionsSent(txn, contactId));
assertEquals(0L, db.getTransportsModified(txn));
assertEquals(0L, db.getTransportsSent(txn, contactId));
// Update the timestamps
db.setSubscriptionsModified(txn,
Collections.singletonList(contactId), 1L);
db.setSubscriptionsSent(txn, contactId, 2L);
db.setTransportsModified(txn, 3L);
db.setTransportsSent(txn, contactId, 4L);
// Check that the updated values were stored
assertEquals(1L, db.getSubscriptionsModified(txn, contactId));
assertEquals(2L, db.getSubscriptionsSent(txn, contactId));
assertEquals(3L, db.getTransportsModified(txn));
assertEquals(4L, db.getTransportsSent(txn, contactId));
db.commitTransaction(txn);
db.close();
}
@Test
public void testGetMessageBody() throws Exception {
Database<Connection> db = open(false);

View File

@@ -2,7 +2,6 @@ package net.sf.briar.protocol;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_AUTHOR_NAME_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_BODY_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_GROUPS;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_GROUP_NAME_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PACKET_LENGTH;
import static net.sf.briar.api.protocol.ProtocolConstants.MAX_PROPERTIES_PER_TRANSPORT;
@@ -16,8 +15,6 @@ import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import net.sf.briar.BriarTestCase;
import net.sf.briar.TestUtils;
@@ -36,7 +33,6 @@ import net.sf.briar.api.protocol.PacketFactory;
import net.sf.briar.api.protocol.ProtocolWriter;
import net.sf.briar.api.protocol.ProtocolWriterFactory;
import net.sf.briar.api.protocol.RawBatch;
import net.sf.briar.api.protocol.SubscriptionUpdate;
import net.sf.briar.api.protocol.Transport;
import net.sf.briar.api.protocol.TransportId;
import net.sf.briar.api.protocol.TransportIndex;
@@ -155,30 +151,6 @@ public class ConstantsTest extends BriarTestCase {
assertTrue(out.size() <= length);
}
@Test
public void testSubscriptionsFitIntoUpdate() throws Exception {
// Create the maximum number of maximum-length subscriptions
Map<Group, Long> subs = new HashMap<Group, Long>(MAX_GROUPS);
byte[] publicKey = new byte[MAX_PUBLIC_KEY_LENGTH];
for(int i = 0; i < MAX_GROUPS; i++) {
String name = createRandomString(MAX_GROUP_NAME_LENGTH);
Group group = groupFactory.createGroup(name, publicKey);
subs.put(group, Long.MAX_VALUE);
}
// Add the subscriptions to an update
ByteArrayOutputStream out =
new ByteArrayOutputStream(MAX_PACKET_LENGTH);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out,
true);
SubscriptionUpdate s = packetFactory.createSubscriptionUpdate(subs,
Long.MAX_VALUE);
writer.writeSubscriptionUpdate(s);
// Check the size of the serialised update
assertTrue(out.size() > MAX_GROUPS *
(MAX_GROUP_NAME_LENGTH + MAX_PUBLIC_KEY_LENGTH + 8) + 8);
assertTrue(out.size() <= MAX_PACKET_LENGTH);
}
@Test
public void testTransportsFitIntoUpdate() throws Exception {
// Create the maximum number of plugins, each with the maximum number

View File

@@ -14,6 +14,7 @@ import net.sf.briar.api.protocol.Batch;
import net.sf.briar.api.protocol.BatchId;
import net.sf.briar.api.protocol.Group;
import net.sf.briar.api.protocol.GroupFactory;
import net.sf.briar.api.protocol.GroupId;
import net.sf.briar.api.protocol.Message;
import net.sf.briar.api.protocol.MessageFactory;
import net.sf.briar.api.protocol.Offer;
@@ -97,7 +98,8 @@ public class ProtocolReadWriteTest extends BriarTestCase {
writer.writeRequest(r);
SubscriptionUpdate s = packetFactory.createSubscriptionUpdate(
subscriptions, timestamp);
Collections.<GroupId, GroupId>emptyMap(), subscriptions, 0L,
timestamp);
writer.writeSubscriptionUpdate(s);
TransportUpdate t = packetFactory.createTransportUpdate(transports,

View File

@@ -114,8 +114,8 @@ public class SimplexConnectionReadWriteTest extends BriarTestCase {
alice.getInstance(ConnectionWriterFactory.class);
ProtocolWriterFactory protoFactory =
alice.getInstance(ProtocolWriterFactory.class);
TestSimplexTransportWriter transport = new TestSimplexTransportWriter(out,
Long.MAX_VALUE, false);
TestSimplexTransportWriter transport = new TestSimplexTransportWriter(
out, Long.MAX_VALUE, false);
OutgoingSimplexConnection simplex = new OutgoingSimplexConnection(db,
connRegistry, connFactory, protoFactory, contactId, transportId,
transportIndex, transport);