mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Moved transactions out of database component.
This commit is contained in:
@@ -14,6 +14,7 @@ import org.briarproject.api.db.NoSuchLocalAuthorException;
|
||||
import org.briarproject.api.db.NoSuchMessageException;
|
||||
import org.briarproject.api.db.NoSuchTransportException;
|
||||
import org.briarproject.api.db.StorageStatus;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.GroupAddedEvent;
|
||||
import org.briarproject.api.event.GroupRemovedEvent;
|
||||
@@ -50,11 +51,13 @@ import org.junit.Test;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.api.sync.SyncConstants.MAX_GROUP_DESCRIPTOR_LENGTH;
|
||||
import static org.briarproject.api.sync.ValidationManager.Validity.UNKNOWN;
|
||||
import static org.briarproject.api.sync.ValidationManager.Validity.VALID;
|
||||
import static org.briarproject.api.transport.TransportConstants.REORDERING_WINDOW_SIZE;
|
||||
import static org.briarproject.db.DatabaseConstants.MAX_OFFERED_MESSAGES;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -121,14 +124,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
|
||||
final EventBus eventBus = context.mock(EventBus.class);
|
||||
context.checking(new Expectations() {{
|
||||
exactly(9).of(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
exactly(9).of(database).commitTransaction(txn);
|
||||
// open()
|
||||
oneOf(database).open();
|
||||
will(returnValue(false));
|
||||
oneOf(shutdown).addShutdownHook(with(any(Runnable.class)));
|
||||
will(returnValue(shutdownHandle));
|
||||
// startTransaction()
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
// addLocalAuthor()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
will(returnValue(false));
|
||||
@@ -171,6 +174,8 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
will(returnValue(true));
|
||||
oneOf(database).removeLocalAuthor(txn, localAuthorId);
|
||||
// endTransaction()
|
||||
oneOf(database).commitTransaction(txn);
|
||||
// close()
|
||||
oneOf(shutdown).removeShutdownHook(shutdownHandle);
|
||||
oneOf(database).close();
|
||||
@@ -179,15 +184,21 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
shutdown);
|
||||
|
||||
assertFalse(db.open());
|
||||
db.addLocalAuthor(localAuthor);
|
||||
assertEquals(contactId, db.addContact(author, localAuthorId));
|
||||
assertEquals(Collections.singletonList(contact), db.getContacts());
|
||||
db.addGroup(group); // First time - listeners called
|
||||
db.addGroup(group); // Second time - not called
|
||||
assertEquals(Collections.singletonList(group), db.getGroups(clientId));
|
||||
db.removeGroup(group);
|
||||
db.removeContact(contactId);
|
||||
db.removeLocalAuthor(localAuthorId);
|
||||
Transaction transaction = db.startTransaction();
|
||||
db.addLocalAuthor(transaction, localAuthor);
|
||||
assertEquals(contactId,
|
||||
db.addContact(transaction, author, localAuthorId));
|
||||
assertEquals(Collections.singletonList(contact),
|
||||
db.getContacts(transaction));
|
||||
db.addGroup(transaction, group); // First time - listeners called
|
||||
db.addGroup(transaction, group); // Second time - not called
|
||||
assertEquals(Collections.singletonList(group),
|
||||
db.getGroups(transaction, clientId));
|
||||
db.removeGroup(transaction, group);
|
||||
db.removeContact(transaction, contactId);
|
||||
db.removeLocalAuthor(transaction, localAuthorId);
|
||||
transaction.setComplete();
|
||||
db.endTransaction(transaction);
|
||||
db.close();
|
||||
|
||||
context.assertIsSatisfied();
|
||||
@@ -211,11 +222,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.addLocalMessage(message, clientId, metadata, true);
|
||||
db.addLocalMessage(transaction, message, clientId, metadata, true);
|
||||
fail();
|
||||
} catch (NoSuchGroupException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
@@ -253,7 +267,13 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.addLocalMessage(message, clientId, metadata, true);
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.addLocalMessage(transaction, message, clientId, metadata, true);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -277,126 +297,178 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.addTransportKeys(contactId, createTransportKeys());
|
||||
db.addTransportKeys(transaction, contactId, createTransportKeys());
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.generateAck(contactId, 123);
|
||||
db.generateAck(transaction, contactId, 123);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.generateBatch(contactId, 123, 456);
|
||||
db.generateBatch(transaction, contactId, 123, 456);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.generateOffer(contactId, 123, 456);
|
||||
db.generateOffer(transaction, contactId, 123, 456);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.generateRequest(contactId, 123);
|
||||
db.generateRequest(transaction, contactId, 123);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getContact(contactId);
|
||||
db.getContact(transaction, contactId);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getMessageStatus(contactId, groupId);
|
||||
db.getMessageStatus(transaction, contactId, groupId);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getMessageStatus(contactId, messageId);
|
||||
db.getMessageStatus(transaction, contactId, messageId);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.incrementStreamCounter(contactId, transportId, 0);
|
||||
db.incrementStreamCounter(transaction, contactId, transportId, 0);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.isVisibleToContact(contactId, groupId);
|
||||
db.isVisibleToContact(transaction, contactId, groupId);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
Ack a = new Ack(Collections.singletonList(messageId));
|
||||
db.receiveAck(contactId, a);
|
||||
db.receiveAck(transaction, contactId, a);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.receiveMessage(contactId, message);
|
||||
db.receiveMessage(transaction, contactId, message);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
Offer o = new Offer(Collections.singletonList(messageId));
|
||||
db.receiveOffer(contactId, o);
|
||||
db.receiveOffer(transaction, contactId, o);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
Request r = new Request(Collections.singletonList(messageId));
|
||||
db.receiveRequest(contactId, r);
|
||||
db.receiveRequest(transaction, contactId, r);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.removeContact(contactId);
|
||||
db.removeContact(transaction, contactId);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.setReorderingWindow(contactId, transportId, 0, 0, new byte[4]);
|
||||
db.setReorderingWindow(transaction, contactId, transportId, 0, 0,
|
||||
new byte[REORDERING_WINDOW_SIZE / 8]);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.setVisibleToContact(contactId, groupId, true);
|
||||
db.setVisibleToContact(transaction, contactId, groupId, true);
|
||||
fail();
|
||||
} catch (NoSuchContactException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
@@ -421,25 +493,34 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.addContact(author, localAuthorId);
|
||||
db.addContact(transaction, author, localAuthorId);
|
||||
fail();
|
||||
} catch (NoSuchLocalAuthorException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getLocalAuthor(localAuthorId);
|
||||
db.getLocalAuthor(transaction, localAuthorId);
|
||||
fail();
|
||||
} catch (NoSuchLocalAuthorException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.removeLocalAuthor(localAuthorId);
|
||||
db.removeLocalAuthor(transaction, localAuthorId);
|
||||
fail();
|
||||
} catch (NoSuchLocalAuthorException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
@@ -468,53 +549,74 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.getGroup(groupId);
|
||||
db.getGroup(transaction, groupId);
|
||||
fail();
|
||||
} catch (NoSuchGroupException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getGroupMetadata(groupId);
|
||||
db.getGroupMetadata(transaction, groupId);
|
||||
fail();
|
||||
} catch (NoSuchGroupException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getMessageStatus(contactId, groupId);
|
||||
db.getMessageStatus(transaction, contactId, groupId);
|
||||
fail();
|
||||
} catch (NoSuchGroupException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.isVisibleToContact(contactId, groupId);
|
||||
db.isVisibleToContact(transaction, contactId, groupId);
|
||||
fail();
|
||||
} catch (NoSuchGroupException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.mergeGroupMetadata(groupId, metadata);
|
||||
db.mergeGroupMetadata(transaction, groupId, metadata);
|
||||
fail();
|
||||
} catch (NoSuchGroupException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.removeGroup(group);
|
||||
db.removeGroup(transaction, group);
|
||||
fail();
|
||||
} catch (NoSuchGroupException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.setVisibleToContact(contactId, groupId, true);
|
||||
db.setVisibleToContact(transaction, contactId, groupId, true);
|
||||
fail();
|
||||
} catch (NoSuchGroupException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
@@ -542,60 +644,84 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.deleteMessage(messageId);
|
||||
db.deleteMessage(transaction, messageId);
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.deleteMessageMetadata(messageId);
|
||||
db.deleteMessageMetadata(transaction, messageId);
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getRawMessage(messageId);
|
||||
db.getRawMessage(transaction, messageId);
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getMessageMetadata(messageId);
|
||||
db.getMessageMetadata(transaction, messageId);
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.getMessageStatus(contactId, messageId);
|
||||
db.getMessageStatus(transaction, contactId, messageId);
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.mergeMessageMetadata(messageId, metadata);
|
||||
db.mergeMessageMetadata(transaction, messageId, metadata);
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.setMessageShared(message, true);
|
||||
db.setMessageShared(transaction, message, true);
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.setMessageValid(message, clientId, true);
|
||||
db.setMessageValid(transaction, message, clientId, true);
|
||||
fail();
|
||||
} catch (NoSuchMessageException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
@@ -610,22 +736,21 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
|
||||
final EventBus eventBus = context.mock(EventBus.class);
|
||||
context.checking(new Expectations() {{
|
||||
// addLocalAuthor()
|
||||
// startTransaction()
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
// addLocalAuthor()
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
will(returnValue(false));
|
||||
oneOf(database).addLocalAuthor(txn, localAuthor);
|
||||
oneOf(database).commitTransaction(txn);
|
||||
// addContact()
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).containsLocalAuthor(txn, localAuthorId);
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsContact(txn, authorId, localAuthorId);
|
||||
will(returnValue(false));
|
||||
oneOf(database).addContact(txn, author, localAuthorId);
|
||||
will(returnValue(contactId));
|
||||
// endTransaction()
|
||||
oneOf(database).commitTransaction(txn);
|
||||
// Check whether the transport is in the DB (which it's not)
|
||||
exactly(4).of(database).startTransaction();
|
||||
@@ -639,35 +764,55 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.addLocalAuthor(localAuthor);
|
||||
assertEquals(contactId, db.addContact(author, localAuthorId));
|
||||
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.getTransportKeys(transportId);
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
db.addLocalAuthor(transaction, localAuthor);
|
||||
assertEquals(contactId,
|
||||
db.addContact(transaction, author, localAuthorId));
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.incrementStreamCounter(contactId, transportId, 0);
|
||||
db.getTransportKeys(transaction, transportId);
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.removeTransport(transportId);
|
||||
db.incrementStreamCounter(transaction, contactId, transportId, 0);
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.setReorderingWindow(contactId, transportId, 0, 0, new byte[4]);
|
||||
db.removeTransport(transaction, transportId);
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
transaction = db.startTransaction();
|
||||
try {
|
||||
db.setReorderingWindow(transaction, contactId, transportId, 0, 0,
|
||||
new byte[REORDERING_WINDOW_SIZE / 8]);
|
||||
fail();
|
||||
} catch (NoSuchTransportException expected) {
|
||||
// Expected
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
@@ -695,8 +840,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Ack a = db.generateAck(contactId, 123);
|
||||
assertEquals(messagesToAck, a.getMessageIds());
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
Ack a = db.generateAck(transaction, contactId, 123);
|
||||
assertEquals(messagesToAck, a.getMessageIds());
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -733,8 +884,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
assertEquals(messages, db.generateBatch(contactId, size * 2,
|
||||
maxLatency));
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
assertEquals(messages, db.generateBatch(transaction, contactId,
|
||||
size * 2, maxLatency));
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -764,8 +921,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Offer o = db.generateOffer(contactId, 123, maxLatency);
|
||||
assertEquals(ids, o.getMessageIds());
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
Offer o = db.generateOffer(transaction, contactId, 123, maxLatency);
|
||||
assertEquals(ids, o.getMessageIds());
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -792,8 +955,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Request r = db.generateRequest(contactId, 123);
|
||||
assertEquals(ids, r.getMessageIds());
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
Request r = db.generateRequest(transaction, contactId, 123);
|
||||
assertEquals(ids, r.getMessageIds());
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -831,8 +1000,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
assertEquals(messages, db.generateRequestedBatch(contactId, size * 2,
|
||||
maxLatency));
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
assertEquals(messages, db.generateRequestedBatch(transaction,
|
||||
contactId, size * 2, maxLatency));
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -858,7 +1033,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.receiveAck(contactId, new Ack(Collections.singletonList(messageId)));
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
Ack a = new Ack(Collections.singletonList(messageId));
|
||||
db.receiveAck(transaction, contactId, a);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -896,7 +1078,13 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.receiveMessage(contactId, message);
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.receiveMessage(transaction, contactId, message);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -926,7 +1114,13 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.receiveMessage(contactId, message);
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.receiveMessage(transaction, contactId, message);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -952,7 +1146,13 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.receiveMessage(contactId, message);
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.receiveMessage(transaction, contactId, message);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -998,9 +1198,16 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
Offer o = new Offer(Arrays.asList(messageId, messageId1, messageId2,
|
||||
messageId3));
|
||||
db.receiveOffer(contactId, o);
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
Offer o = new Offer(Arrays.asList(messageId, messageId1,
|
||||
messageId2, messageId3));
|
||||
db.receiveOffer(transaction, contactId, o);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@@ -1026,8 +1233,14 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.receiveRequest(contactId, new Request(Collections.singletonList(
|
||||
messageId)));
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
Request r = new Request(Collections.singletonList(messageId));
|
||||
db.receiveRequest(transaction, contactId, r);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -1056,7 +1269,13 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.setVisibleToContact(contactId, groupId, true);
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.setVisibleToContact(transaction, contactId, groupId, true);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -1083,45 +1302,56 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.setVisibleToContact(contactId, groupId, true);
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.setVisibleToContact(transaction, contactId, groupId, true);
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransportKeys() throws Exception {
|
||||
final TransportKeys keys = createTransportKeys();
|
||||
final TransportKeys transportKeys = createTransportKeys();
|
||||
final Map<ContactId, TransportKeys> keys = Collections.singletonMap(
|
||||
contactId, transportKeys);
|
||||
Mockery context = new Mockery();
|
||||
@SuppressWarnings("unchecked")
|
||||
final Database<Object> database = context.mock(Database.class);
|
||||
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
|
||||
final EventBus eventBus = context.mock(EventBus.class);
|
||||
context.checking(new Expectations() {{
|
||||
// updateTransportKeys()
|
||||
// startTransaction()
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
// updateTransportKeys()
|
||||
oneOf(database).containsContact(txn, contactId);
|
||||
will(returnValue(true));
|
||||
oneOf(database).containsTransport(txn, transportId);
|
||||
will(returnValue(true));
|
||||
oneOf(database).updateTransportKeys(txn,
|
||||
Collections.singletonMap(contactId, keys));
|
||||
oneOf(database).commitTransaction(txn);
|
||||
oneOf(database).updateTransportKeys(txn, keys);
|
||||
// getTransportKeys()
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).containsTransport(txn, transportId);
|
||||
will(returnValue(true));
|
||||
oneOf(database).getTransportKeys(txn, transportId);
|
||||
will(returnValue(Collections.singletonMap(contactId, keys)));
|
||||
will(returnValue(keys));
|
||||
// endTransaction()
|
||||
oneOf(database).commitTransaction(txn);
|
||||
}});
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
db.updateTransportKeys(Collections.singletonMap(contactId, keys));
|
||||
assertEquals(Collections.singletonMap(contactId, keys),
|
||||
db.getTransportKeys(transportId));
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
db.updateTransportKeys(transaction, keys);
|
||||
assertEquals(keys, db.getTransportKeys(transaction, transportId));
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
@@ -1162,29 +1392,34 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
|
||||
final EventBus eventBus = context.mock(EventBus.class);
|
||||
context.checking(new Expectations() {{
|
||||
// mergeSettings()
|
||||
// startTransaction()
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
// mergeSettings()
|
||||
oneOf(database).getSettings(txn, "namespace");
|
||||
will(returnValue(before));
|
||||
oneOf(database).mergeSettings(txn, update, "namespace");
|
||||
oneOf(database).commitTransaction(txn);
|
||||
oneOf(eventBus).broadcast(with(any(SettingsUpdatedEvent.class)));
|
||||
// mergeSettings() again
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).getSettings(txn, "namespace");
|
||||
will(returnValue(merged));
|
||||
// endTransaction()
|
||||
oneOf(database).commitTransaction(txn);
|
||||
}});
|
||||
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
// First merge should broadcast an event
|
||||
db.mergeSettings(update, "namespace");
|
||||
// Second merge should not broadcast an event
|
||||
db.mergeSettings(update, "namespace");
|
||||
Transaction transaction = db.startTransaction();
|
||||
try {
|
||||
// First merge should broadcast an event
|
||||
db.mergeSettings(transaction, update, "namespace");
|
||||
// Second merge should not broadcast an event
|
||||
db.mergeSettings(transaction, update, "namespace");
|
||||
transaction.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(transaction);
|
||||
}
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user