mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 22:29:53 +01:00
Unit tests and a bugfix. THE SYSTEM WORKS!
This commit is contained in:
@@ -357,7 +357,7 @@ class ReadWriteLockDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
int bytesSent = 0;
|
int bytesSent = 0;
|
||||||
for(MessageId m : requested) {
|
for(MessageId m : requested) {
|
||||||
byte[] message = db.getMessageIfSendable(txn, c, m);
|
byte[] message = db.getMessageIfSendable(txn, c, m);
|
||||||
if(b == null) continue; // Expired or not sendable
|
if(message == null) continue;
|
||||||
if(!b.writeMessage(message)) break;
|
if(!b.writeMessage(message)) break;
|
||||||
bytesSent += message.length;
|
bytesSent += message.length;
|
||||||
sent.add(m);
|
sent.add(m);
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ class SynchronizedDatabaseComponent<Txn> extends DatabaseComponentImpl<Txn> {
|
|||||||
int bytesSent = 0;
|
int bytesSent = 0;
|
||||||
for(MessageId m : requested) {
|
for(MessageId m : requested) {
|
||||||
byte[] message = db.getMessageIfSendable(txn, c, m);
|
byte[] message = db.getMessageIfSendable(txn, c, m);
|
||||||
if(b == null) continue; // Expired or not sendable
|
if(message == null) continue;
|
||||||
if(!b.writeMessage(message)) break;
|
if(!b.writeMessage(message)) break;
|
||||||
bytesSent += message.length;
|
bytesSent += message.length;
|
||||||
sent.add(m);
|
sent.add(m);
|
||||||
|
|||||||
@@ -41,9 +41,6 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
private final int size;
|
private final int size;
|
||||||
private final byte[] raw;
|
private final byte[] raw;
|
||||||
private final Message message;
|
private final Message message;
|
||||||
private final Collection<ContactId> contacts;
|
|
||||||
private final Collection<BatchId> acks;
|
|
||||||
private final Collection<GroupId> subs;
|
|
||||||
private final Map<String, String> transports;
|
private final Map<String, String> transports;
|
||||||
private final Collection<MessageId> messages;
|
private final Collection<MessageId> messages;
|
||||||
|
|
||||||
@@ -60,9 +57,6 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
raw = new byte[size];
|
raw = new byte[size];
|
||||||
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
|
message = new TestMessage(messageId, MessageId.NONE, groupId, authorId,
|
||||||
timestamp, raw);
|
timestamp, raw);
|
||||||
contacts = Collections.singletonList(contactId);
|
|
||||||
acks = Collections.singletonList(batchId);
|
|
||||||
subs = Collections.singletonList(groupId);
|
|
||||||
transports = Collections.singletonMap("foo", "bar");
|
transports = Collections.singletonMap("foo", "bar");
|
||||||
messages = Collections.singletonList(messageId);
|
messages = Collections.singletonList(messageId);
|
||||||
}
|
}
|
||||||
@@ -92,7 +86,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
will(returnValue(contactId));
|
will(returnValue(contactId));
|
||||||
// getContacts()
|
// getContacts()
|
||||||
oneOf(database).getContacts(txn);
|
oneOf(database).getContacts(txn);
|
||||||
will(returnValue(contacts));
|
will(returnValue(Collections.singletonList(contactId)));
|
||||||
// getTransports(contactId)
|
// getTransports(contactId)
|
||||||
oneOf(database).containsContact(txn, contactId);
|
oneOf(database).containsContact(txn, contactId);
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
@@ -102,7 +96,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(database).addSubscription(txn, group);
|
oneOf(database).addSubscription(txn, group);
|
||||||
// getSubscriptions()
|
// getSubscriptions()
|
||||||
oneOf(database).getSubscriptions(txn);
|
oneOf(database).getSubscriptions(txn);
|
||||||
will(returnValue(subs));
|
will(returnValue(Collections.singletonList(groupId)));
|
||||||
// unsubscribe(groupId)
|
// unsubscribe(groupId)
|
||||||
oneOf(database).removeSubscription(txn, groupId);
|
oneOf(database).removeSubscription(txn, groupId);
|
||||||
// removeContact(contactId)
|
// removeContact(contactId)
|
||||||
@@ -116,10 +110,10 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
db.open(false);
|
db.open(false);
|
||||||
assertEquals(Rating.UNRATED, db.getRating(authorId));
|
assertEquals(Rating.UNRATED, db.getRating(authorId));
|
||||||
assertEquals(contactId, db.addContact(transports));
|
assertEquals(contactId, db.addContact(transports));
|
||||||
assertEquals(contacts, db.getContacts());
|
assertEquals(Collections.singletonList(contactId), db.getContacts());
|
||||||
assertEquals(transports, db.getTransports(contactId));
|
assertEquals(transports, db.getTransports(contactId));
|
||||||
db.subscribe(group);
|
db.subscribe(group);
|
||||||
assertEquals(subs, db.getSubscriptions());
|
assertEquals(Collections.singletonList(groupId), db.getSubscriptions());
|
||||||
db.unsubscribe(groupId);
|
db.unsubscribe(groupId);
|
||||||
db.removeContact(contactId);
|
db.removeContact(contactId);
|
||||||
db.close();
|
db.close();
|
||||||
@@ -367,7 +361,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(database).addMessage(txn, message);
|
oneOf(database).addMessage(txn, message);
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).getContacts(txn);
|
oneOf(database).getContacts(txn);
|
||||||
will(returnValue(contacts));
|
will(returnValue(Collections.singletonList(contactId)));
|
||||||
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
|
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
|
||||||
// The author is unrated and there are no sendable children
|
// The author is unrated and there are no sendable children
|
||||||
oneOf(database).getRating(txn, authorId);
|
oneOf(database).getRating(txn, authorId);
|
||||||
@@ -400,7 +394,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(database).addMessage(txn, message);
|
oneOf(database).addMessage(txn, message);
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
oneOf(database).getContacts(txn);
|
oneOf(database).getContacts(txn);
|
||||||
will(returnValue(contacts));
|
will(returnValue(Collections.singletonList(contactId)));
|
||||||
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
|
oneOf(database).setStatus(txn, contactId, messageId, Status.NEW);
|
||||||
// The author is rated GOOD and there are two sendable children
|
// The author is rated GOOD and there are two sendable children
|
||||||
oneOf(database).getRating(txn, authorId);
|
oneOf(database).getRating(txn, authorId);
|
||||||
@@ -461,9 +455,9 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testGenerateAck() throws Exception {
|
public void testGenerateAck() throws Exception {
|
||||||
final BatchId batchId1 = new BatchId(TestUtils.getRandomId());
|
final BatchId batchId1 = new BatchId(TestUtils.getRandomId());
|
||||||
final Collection<BatchId> twoAcks = new ArrayList<BatchId>();
|
final Collection<BatchId> batchesToAck = new ArrayList<BatchId>();
|
||||||
twoAcks.add(batchId);
|
batchesToAck.add(batchId);
|
||||||
twoAcks.add(batchId1);
|
batchesToAck.add(batchId1);
|
||||||
Mockery context = new Mockery();
|
Mockery context = new Mockery();
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
final Database<Object> database = context.mock(Database.class);
|
final Database<Object> database = context.mock(Database.class);
|
||||||
@@ -477,7 +471,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
// Get the batches to ack
|
// Get the batches to ack
|
||||||
oneOf(database).getBatchesToAck(txn, contactId);
|
oneOf(database).getBatchesToAck(txn, contactId);
|
||||||
will(returnValue(twoAcks));
|
will(returnValue(batchesToAck));
|
||||||
// Try to add both batches to the writer - only manage to add one
|
// Try to add both batches to the writer - only manage to add one
|
||||||
oneOf(ackWriter).writeBatchId(batchId);
|
oneOf(ackWriter).writeBatchId(batchId);
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
@@ -485,7 +479,8 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
will(returnValue(false));
|
will(returnValue(false));
|
||||||
oneOf(ackWriter).finish();
|
oneOf(ackWriter).finish();
|
||||||
// Record the batch that was acked
|
// Record the batch that was acked
|
||||||
oneOf(database).removeBatchesToAck(txn, contactId, acks);
|
oneOf(database).removeBatchesToAck(txn, contactId,
|
||||||
|
Collections.singletonList(batchId));
|
||||||
}});
|
}});
|
||||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|
||||||
@@ -498,9 +493,9 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
public void testGenerateBatch() throws Exception {
|
public void testGenerateBatch() throws Exception {
|
||||||
final MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
final MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
||||||
final byte[] raw1 = new byte[size];
|
final byte[] raw1 = new byte[size];
|
||||||
final Collection<MessageId> twoMessages = new ArrayList<MessageId>();
|
final Collection<MessageId> sendable = new ArrayList<MessageId>();
|
||||||
twoMessages.add(messageId);
|
sendable.add(messageId);
|
||||||
twoMessages.add(messageId1);
|
sendable.add(messageId1);
|
||||||
Mockery context = new Mockery();
|
Mockery context = new Mockery();
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
final Database<Object> database = context.mock(Database.class);
|
final Database<Object> database = context.mock(Database.class);
|
||||||
@@ -516,7 +511,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
oneOf(batchWriter).getCapacity();
|
oneOf(batchWriter).getCapacity();
|
||||||
will(returnValue(ONE_MEGABYTE));
|
will(returnValue(ONE_MEGABYTE));
|
||||||
oneOf(database).getSendableMessages(txn, contactId, ONE_MEGABYTE);
|
oneOf(database).getSendableMessages(txn, contactId, ONE_MEGABYTE);
|
||||||
will(returnValue(twoMessages));
|
will(returnValue(sendable));
|
||||||
// Try to add both messages to the writer - only manage to add one
|
// Try to add both messages to the writer - only manage to add one
|
||||||
oneOf(database).getMessage(txn, messageId);
|
oneOf(database).getMessage(txn, messageId);
|
||||||
will(returnValue(raw));
|
will(returnValue(raw));
|
||||||
@@ -539,6 +534,50 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGenerateBatchFromRequest() throws Exception {
|
||||||
|
final MessageId messageId1 = new MessageId(TestUtils.getRandomId());
|
||||||
|
final MessageId messageId2 = new MessageId(TestUtils.getRandomId());
|
||||||
|
final byte[] raw1 = new byte[size];
|
||||||
|
final Collection<MessageId> requested = new ArrayList<MessageId>();
|
||||||
|
requested.add(messageId);
|
||||||
|
requested.add(messageId1);
|
||||||
|
requested.add(messageId2);
|
||||||
|
Mockery context = new Mockery();
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
final Database<Object> database = context.mock(Database.class);
|
||||||
|
final DatabaseCleaner cleaner = context.mock(DatabaseCleaner.class);
|
||||||
|
final BatchWriter batchWriter = context.mock(BatchWriter.class);
|
||||||
|
context.checking(new Expectations() {{
|
||||||
|
allowing(database).startTransaction();
|
||||||
|
will(returnValue(txn));
|
||||||
|
allowing(database).commitTransaction(txn);
|
||||||
|
allowing(database).containsContact(txn, contactId);
|
||||||
|
will(returnValue(true));
|
||||||
|
// Try to get the requested messages and add them to the writer
|
||||||
|
oneOf(database).getMessageIfSendable(txn, contactId, messageId);
|
||||||
|
will(returnValue(raw)); // Message is sendable
|
||||||
|
oneOf(batchWriter).writeMessage(raw);
|
||||||
|
will(returnValue(true)); // Message added to batch
|
||||||
|
oneOf(database).getMessageIfSendable(txn, contactId, messageId1);
|
||||||
|
will(returnValue(null)); // Message is not sendable
|
||||||
|
oneOf(database).getMessageIfSendable(txn, contactId, messageId2);
|
||||||
|
will(returnValue(raw1)); // Message is sendable
|
||||||
|
oneOf(batchWriter).writeMessage(raw1);
|
||||||
|
will(returnValue(false)); // Message not added to batch
|
||||||
|
oneOf(batchWriter).finish();
|
||||||
|
will(returnValue(batchId));
|
||||||
|
// Record the message that was sent
|
||||||
|
oneOf(database).addOutstandingBatch(txn, contactId, batchId,
|
||||||
|
Collections.singletonList(messageId));
|
||||||
|
}});
|
||||||
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|
||||||
|
db.generateBatch(contactId, batchWriter, requested);
|
||||||
|
|
||||||
|
context.assertIsSatisfied();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReceiveAck() throws Exception {
|
public void testReceiveAck() throws Exception {
|
||||||
Mockery context = new Mockery();
|
Mockery context = new Mockery();
|
||||||
@@ -554,7 +593,7 @@ public abstract class DatabaseComponentTest extends TestCase {
|
|||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
// Get the acked batches
|
// Get the acked batches
|
||||||
oneOf(ack).getBatches();
|
oneOf(ack).getBatches();
|
||||||
will(returnValue(acks));
|
will(returnValue(Collections.singletonList(batchId)));
|
||||||
oneOf(database).removeAckedBatch(txn, contactId, batchId);
|
oneOf(database).removeAckedBatch(txn, contactId, batchId);
|
||||||
}});
|
}});
|
||||||
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
DatabaseComponent db = createDatabaseComponent(database, cleaner);
|
||||||
|
|||||||
Reference in New Issue
Block a user