mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Store message count, unread count and timestamp of latest message
in group metadata to be able to speed up group listings. Closes #584, #586, #585
This commit is contained in:
@@ -4,28 +4,24 @@ import org.briarproject.BriarTestCase;
|
||||
import org.briarproject.TestUtils;
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.clients.ClientHelper;
|
||||
import org.briarproject.api.clients.MessageQueueManager;
|
||||
import org.briarproject.api.clients.ContactGroupFactory;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.contact.ContactId;
|
||||
import org.briarproject.api.data.BdfDictionary;
|
||||
import org.briarproject.api.data.BdfEntry;
|
||||
import org.briarproject.api.data.BdfList;
|
||||
import org.briarproject.api.data.MetadataEncoder;
|
||||
import org.briarproject.api.data.MetadataParser;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.clients.SessionId;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.Message;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
import org.briarproject.api.sync.MessageStatus;
|
||||
import org.briarproject.api.system.Clock;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.Mockery;
|
||||
import org.jmock.lib.legacy.ClassImposteriser;
|
||||
@@ -46,30 +42,29 @@ import static org.briarproject.api.introduction.IntroductionConstants.TYPE;
|
||||
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_REQUEST;
|
||||
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_RESPONSE;
|
||||
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
||||
import static org.briarproject.clients.BdfConstants.GROUP_KEY_LATEST_MSG;
|
||||
import static org.briarproject.clients.BdfConstants.GROUP_KEY_MSG_COUNT;
|
||||
import static org.briarproject.clients.BdfConstants.GROUP_KEY_UNREAD_COUNT;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
|
||||
final Mockery context;
|
||||
final IntroductionManagerImpl introductionManager;
|
||||
final IntroducerManager introducerManager;
|
||||
final IntroduceeManager introduceeManager;
|
||||
final DatabaseComponent db;
|
||||
final ContactGroupFactory contactGroupFactory;
|
||||
final ClientHelper clientHelper;
|
||||
final MetadataEncoder metadataEncoder;
|
||||
final MessageQueueManager messageQueueManager;
|
||||
final IntroductionGroupFactory introductionGroupFactory;
|
||||
final Clock clock;
|
||||
final SessionId sessionId = new SessionId(TestUtils.getRandomId());
|
||||
final long time = 42L;
|
||||
final Contact introducee1;
|
||||
final Contact introducee2;
|
||||
final Group localGroup0;
|
||||
final Group introductionGroup1;
|
||||
final Group introductionGroup2;
|
||||
final Message message1;
|
||||
Transaction txn;
|
||||
private final Mockery context;
|
||||
private final IntroductionManagerImpl introductionManager;
|
||||
private final IntroducerManager introducerManager;
|
||||
private final IntroduceeManager introduceeManager;
|
||||
private final DatabaseComponent db;
|
||||
private final ClientHelper clientHelper;
|
||||
private final IntroductionGroupFactory introductionGroupFactory;
|
||||
private final SessionId sessionId = new SessionId(TestUtils.getRandomId());
|
||||
private final long time = 42L;
|
||||
private final Contact introducee1;
|
||||
private final Contact introducee2;
|
||||
private final Group introductionGroup1;
|
||||
private final Group introductionGroup2;
|
||||
private final Message message1;
|
||||
private Transaction txn;
|
||||
private BdfDictionary metadataBefore, metadataAfter;
|
||||
|
||||
public IntroductionManagerImplTest() {
|
||||
AuthorId authorId1 = new AuthorId(TestUtils.getRandomId());
|
||||
@@ -89,8 +84,6 @@ public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
new Contact(contactId2, author2, localAuthorId2, true, true);
|
||||
|
||||
ClientId clientId = new ClientId(TestUtils.getRandomId());
|
||||
localGroup0 = new Group(new GroupId(TestUtils.getRandomId()),
|
||||
clientId, new byte[0]);
|
||||
introductionGroup1 = new Group(new GroupId(TestUtils.getRandomId()),
|
||||
clientId, new byte[0]);
|
||||
introductionGroup2 = new Group(new GroupId(TestUtils.getRandomId()),
|
||||
@@ -102,6 +95,14 @@ public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
time,
|
||||
TestUtils.getRandomBytes(MESSAGE_HEADER_LENGTH + 1)
|
||||
);
|
||||
metadataBefore = BdfDictionary.of(
|
||||
new BdfEntry(GROUP_KEY_MSG_COUNT, 41L),
|
||||
new BdfEntry(GROUP_KEY_LATEST_MSG, 0L)
|
||||
);
|
||||
metadataAfter = BdfDictionary.of(
|
||||
new BdfEntry(GROUP_KEY_MSG_COUNT, 42L),
|
||||
new BdfEntry(GROUP_KEY_LATEST_MSG, time)
|
||||
);
|
||||
|
||||
// mock ALL THE THINGS!!!
|
||||
context = new Mockery();
|
||||
@@ -109,15 +110,9 @@ public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
introducerManager = context.mock(IntroducerManager.class);
|
||||
introduceeManager = context.mock(IntroduceeManager.class);
|
||||
db = context.mock(DatabaseComponent.class);
|
||||
contactGroupFactory = context.mock(ContactGroupFactory.class);
|
||||
clientHelper = context.mock(ClientHelper.class);
|
||||
metadataEncoder =
|
||||
context.mock(MetadataEncoder.class);
|
||||
messageQueueManager =
|
||||
context.mock(MessageQueueManager.class);
|
||||
MetadataParser metadataParser = context.mock(MetadataParser.class);
|
||||
introductionGroupFactory = context.mock(IntroductionGroupFactory.class);
|
||||
clock = context.mock(Clock.class);
|
||||
|
||||
introductionManager = new IntroductionManagerImpl(
|
||||
db, clientHelper, metadataParser, introducerManager,
|
||||
@@ -135,6 +130,27 @@ public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
oneOf(introducerManager)
|
||||
.makeIntroduction(txn, introducee1, introducee2, null,
|
||||
time);
|
||||
// get both introduction groups
|
||||
oneOf(introductionGroupFactory)
|
||||
.createIntroductionGroup(introducee1);
|
||||
will(returnValue(introductionGroup1));
|
||||
oneOf(introductionGroupFactory)
|
||||
.createIntroductionGroup(introducee2);
|
||||
will(returnValue(introductionGroup2));
|
||||
// track message for group 1
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
introductionGroup1.getId());
|
||||
will(returnValue(metadataBefore));
|
||||
oneOf(clientHelper)
|
||||
.mergeGroupMetadata(txn, introductionGroup1.getId(),
|
||||
metadataAfter);
|
||||
// track message for group 2
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
introductionGroup2.getId());
|
||||
will(returnValue(metadataBefore));
|
||||
oneOf(clientHelper)
|
||||
.mergeGroupMetadata(txn, introductionGroup2.getId(),
|
||||
metadataAfter);
|
||||
oneOf(db).endTransaction(txn);
|
||||
}});
|
||||
|
||||
@@ -163,6 +179,13 @@ public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, sessionId);
|
||||
will(returnValue(state));
|
||||
oneOf(introduceeManager).acceptIntroduction(txn, state, time);
|
||||
// track message
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
introductionGroup1.getId());
|
||||
will(returnValue(metadataBefore));
|
||||
oneOf(clientHelper)
|
||||
.mergeGroupMetadata(txn, introductionGroup1.getId(),
|
||||
metadataAfter);
|
||||
oneOf(db).endTransaction(txn);
|
||||
}});
|
||||
|
||||
@@ -191,6 +214,13 @@ public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, sessionId);
|
||||
will(returnValue(state));
|
||||
oneOf(introduceeManager).declineIntroduction(txn, state, time);
|
||||
// track message
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
introductionGroup1.getId());
|
||||
will(returnValue(metadataBefore));
|
||||
oneOf(clientHelper)
|
||||
.mergeGroupMetadata(txn, introductionGroup1.getId(),
|
||||
metadataAfter);
|
||||
oneOf(db).endTransaction(txn);
|
||||
}});
|
||||
|
||||
@@ -241,12 +271,22 @@ public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
final BdfDictionary state = new BdfDictionary();
|
||||
txn = new Transaction(null, false);
|
||||
|
||||
metadataBefore.put(GROUP_KEY_UNREAD_COUNT, 1L);
|
||||
metadataAfter.put(GROUP_KEY_UNREAD_COUNT, 2L);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(introduceeManager)
|
||||
.initialize(txn, introductionGroup1.getId(), msg);
|
||||
will(returnValue(state));
|
||||
oneOf(introduceeManager)
|
||||
.incomingMessage(txn, state, msg);
|
||||
// track message
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
introductionGroup1.getId());
|
||||
will(returnValue(metadataBefore));
|
||||
oneOf(clientHelper)
|
||||
.mergeGroupMetadata(txn, introductionGroup1.getId(),
|
||||
metadataAfter);
|
||||
}});
|
||||
|
||||
introductionManager
|
||||
@@ -272,10 +312,20 @@ public class IntroductionManagerImplTest extends BriarTestCase {
|
||||
|
||||
txn = new Transaction(null, false);
|
||||
|
||||
metadataBefore.put(GROUP_KEY_UNREAD_COUNT, 41L);
|
||||
metadataAfter.put(GROUP_KEY_UNREAD_COUNT, 42L);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn, sessionId);
|
||||
will(returnValue(state));
|
||||
oneOf(introducerManager).incomingMessage(txn, state, msg);
|
||||
// track message
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
introductionGroup1.getId());
|
||||
will(returnValue(metadataBefore));
|
||||
oneOf(clientHelper)
|
||||
.mergeGroupMetadata(txn, introductionGroup1.getId(),
|
||||
metadataAfter);
|
||||
}});
|
||||
|
||||
introductionManager
|
||||
|
||||
Reference in New Issue
Block a user