From 0a84a01235e2109e27f42d868bf8d01dc78ece13 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Wed, 7 Sep 2011 11:15:34 +0100 Subject: [PATCH] Allow identical batches to be sent to multiple contacts. --- components/net/sf/briar/db/JdbcDatabase.java | 2 +- test/net/sf/briar/db/H2DatabaseTest.java | 49 +++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/components/net/sf/briar/db/JdbcDatabase.java b/components/net/sf/briar/db/JdbcDatabase.java index dc6f9252e..4f1d3bfd6 100644 --- a/components/net/sf/briar/db/JdbcDatabase.java +++ b/components/net/sf/briar/db/JdbcDatabase.java @@ -127,7 +127,7 @@ abstract class JdbcDatabase implements Database { + " (batchId HASH NOT NULL," + " contactId INT NOT NULL," + " messageId HASH NOT NULL," - + " PRIMARY KEY (batchId, messageId)," + + " PRIMARY KEY (batchId, contactId, messageId)," + " FOREIGN KEY (batchId, contactId)" + " REFERENCES outstandingBatches (batchId, contactId)" + " ON DELETE CASCADE," diff --git a/test/net/sf/briar/db/H2DatabaseTest.java b/test/net/sf/briar/db/H2DatabaseTest.java index d1e5f076c..a73f72868 100644 --- a/test/net/sf/briar/db/H2DatabaseTest.java +++ b/test/net/sf/briar/db/H2DatabaseTest.java @@ -461,7 +461,6 @@ public class H2DatabaseTest extends TestCase { assertEquals(contactId, db.addContact(txn, transports, secret)); db.addBatchToAck(txn, contactId, batchId); db.addBatchToAck(txn, contactId, batchId); - db.commitTransaction(txn); // The batch ID should only be returned once Collection acks = db.getBatchesToAck(txn, contactId); @@ -479,6 +478,54 @@ public class H2DatabaseTest extends TestCase { db.close(); } + @Test + public void testSameBatchCannotBeSentTwice() throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add a contact, subscribe to a group and store a message + assertEquals(contactId, db.addContact(txn, transports, secret)); + db.addSubscription(txn, group); + db.addMessage(txn, message); + + // Add an outstanding batch + db.addOutstandingBatch(txn, contactId, batchId, + Collections.singleton(messageId)); + + // It should not be possible to add the same outstanding batch again + try { + db.addOutstandingBatch(txn, contactId, batchId, + Collections.singleton(messageId)); + fail(); + } catch(DbException expected) {} + + db.abortTransaction(txn); + db.close(); + } + + @Test + public void testSameBatchCanBeSentToDifferentContacts() throws DbException { + Database db = open(false); + Connection txn = db.startTransaction(); + + // Add two contacts, subscribe to a group and store a message + assertEquals(contactId, db.addContact(txn, transports, secret)); + ContactId contactId1 = db.addContact(txn, transports, secret); + db.addSubscription(txn, group); + db.addMessage(txn, message); + + // Add an outstanding batch for the first contact + db.addOutstandingBatch(txn, contactId, batchId, + Collections.singleton(messageId)); + + // Add the same outstanding batch for the second contact + db.addOutstandingBatch(txn, contactId1, batchId, + Collections.singleton(messageId)); + + db.commitTransaction(txn); + db.close(); + } + @Test public void testRemoveAckedBatch() throws DbException { Database db = open(false);