From 4f495bb4d3f6f54cbd75f5f1fc3f5ca21aafccef Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Tue, 18 Sep 2018 14:27:49 +0100 Subject: [PATCH] Use now + max latency as ETA, add more tests. --- .../briarproject/bramble/db/JdbcDatabase.java | 8 +- .../bramble/db/JdbcDatabaseTest.java | 79 +++++++++++++++---- 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java b/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java index eab7bffc5c..cf368e1ed9 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java @@ -1873,7 +1873,7 @@ abstract class JdbcDatabase implements Database<Connection> { public Collection<MessageId> getMessagesToOffer(Connection txn, ContactId c, int maxMessages, int maxLatency) throws DbException { long now = clock.currentTimeMillis(); - long eta = now + maxLatency * 2; + long eta = now + maxLatency; PreparedStatement ps = null; ResultSet rs = null; try { @@ -1932,7 +1932,7 @@ abstract class JdbcDatabase implements Database<Connection> { public Collection<MessageId> getMessagesToSend(Connection txn, ContactId c, int maxLength, int maxLatency) throws DbException { long now = clock.currentTimeMillis(); - long eta = now + maxLatency * 2; + long eta = now + maxLatency; PreparedStatement ps = null; ResultSet rs = null; try { @@ -2063,7 +2063,7 @@ abstract class JdbcDatabase implements Database<Connection> { public Collection<MessageId> getRequestedMessagesToSend(Connection txn, ContactId c, int maxLength, int maxLatency) throws DbException { long now = clock.currentTimeMillis(); - long eta = now + maxLatency * 2; + long eta = now + maxLatency; PreparedStatement ps = null; ResultSet rs = null; try { @@ -2910,7 +2910,7 @@ abstract class JdbcDatabase implements Database<Connection> { + " WHERE messageId = ? AND contactId = ?"; ps = txn.prepareStatement(sql); long now = clock.currentTimeMillis(); - long eta = now + maxLatency * 2; + long eta = now + maxLatency; ps.setLong(1, calculateExpiry(now, maxLatency, txCount)); ps.setLong(2, eta); ps.setBytes(3, m.getBytes()); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java b/bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java index 55b908b068..3f929e9cf0 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java @@ -432,7 +432,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase { Collection<MessageId> ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY); assertEquals(singletonList(messageId), ids); - db.updateExpiryTimeAndEta(txn, contactId, messageId, Integer.MAX_VALUE); + db.updateExpiryTimeAndEta(txn, contactId, messageId, MAX_LATENCY); // The message should no longer be sendable ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY); @@ -1815,12 +1815,57 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase { db.close(); } + @Test + public void testMessageRetransmission() throws Exception { + long now = System.currentTimeMillis(); + long steps[] = {now, now, now + MAX_LATENCY * 2, + now + MAX_LATENCY * 2 + 1}; + Database<Connection> db = + open(false, new TestMessageFactory(), new ArrayClock(steps)); + Connection txn = db.startTransaction(); + + // Add a contact, a shared group and a shared message + db.addLocalAuthor(txn, localAuthor); + assertEquals(contactId, db.addContact(txn, author, localAuthor.getId(), + true, true)); + db.addGroup(txn, group); + db.addGroupVisibility(txn, contactId, groupId, true); + db.addMessage(txn, message, DELIVERED, true, null); + + // Time: now + // Retrieve the message from the database + Collection<MessageId> ids = db.getMessagesToSend(txn, contactId, + ONE_MEGABYTE, MAX_LATENCY); + assertEquals(singletonList(messageId), ids); + + // Time: now + // Mark the message as sent + db.updateExpiryTimeAndEta(txn, contactId, messageId, MAX_LATENCY); + + // The message should expire after 2 * MAX_LATENCY + assertEquals(now + MAX_LATENCY * 2, db.getNextSendTime(txn, contactId)); + + // Time: now + MAX_LATENCY * 2 + // The message should not yet be sendable + ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY); + assertTrue(ids.isEmpty()); + + // Time: now + MAX_LATENCY * 2 + 1 + // The message should have expired and should now be sendable + ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY); + assertEquals(singletonList(messageId), ids); + + db.commitTransaction(txn); + db.close(); + } + + @Test public void testFasterMessageRetransmission() throws Exception { long now = System.currentTimeMillis(); - long steps[] = {now, now, now + MAX_LATENCY, now + MAX_LATENCY, - now + 1 + MAX_LATENCY * 2}; - Database<Connection> db = open(false, new ArrayClock(steps)); + long steps[] = {now, now, now, now, now + 1}; + Database<Connection> db = + open(false, new TestMessageFactory(), new ArrayClock(steps)); Connection txn = db.startTransaction(); // Add a contact, a shared group and a shared message @@ -1832,29 +1877,35 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase { db.addMessage(txn, message, DELIVERED, true, null); // Time: now - // Retrieve the message from the database and mark it as sent + // Retrieve the message from the database Collection<MessageId> ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY); assertEquals(singletonList(messageId), ids); + // Time: now + // Mark the message as sent db.updateExpiryTimeAndEta(txn, contactId, messageId, MAX_LATENCY); - // Time: now + MAX_LATENCY - // The message should no longer be sendable via transports with - // with an equal or higher ETA + // The message should expire after 2 * MAX_LATENCY + assertEquals(now + MAX_LATENCY * 2, db.getNextSendTime(txn, contactId)); + + // Time: now + // The message should not be sendable via the same transport ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY); assertTrue(ids.isEmpty()); - // Time: now + MAX_LATENCY + // Time: now // The message should be sendable via a transport with a faster ETA ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, - MAX_LATENCY / 2 - 1); + MAX_LATENCY - 1); assertEquals(singletonList(messageId), ids); - // Time: now + 1 + MAX_LATENCY * 2 - // The message expired and should be sendable by every transport. - ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY); - assertEquals(singletonList(messageId), ids); + // Time: now + 1 + // The message should no longer be sendable via the faster transport, + // as the ETA is now equal + ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, + MAX_LATENCY - 1); + assertTrue(ids.isEmpty()); db.commitTransaction(txn); db.close(); -- GitLab