mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Use now + max latency as ETA, add more tests.
This commit is contained in:
@@ -1873,7 +1873,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
public Collection<MessageId> getMessagesToOffer(Connection txn,
|
public Collection<MessageId> getMessagesToOffer(Connection txn,
|
||||||
ContactId c, int maxMessages, int maxLatency) throws DbException {
|
ContactId c, int maxMessages, int maxLatency) throws DbException {
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
long eta = now + maxLatency * 2;
|
long eta = now + maxLatency;
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
@@ -1932,7 +1932,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
public Collection<MessageId> getMessagesToSend(Connection txn, ContactId c,
|
public Collection<MessageId> getMessagesToSend(Connection txn, ContactId c,
|
||||||
int maxLength, int maxLatency) throws DbException {
|
int maxLength, int maxLatency) throws DbException {
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
long eta = now + maxLatency * 2;
|
long eta = now + maxLatency;
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
@@ -2063,7 +2063,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
public Collection<MessageId> getRequestedMessagesToSend(Connection txn,
|
public Collection<MessageId> getRequestedMessagesToSend(Connection txn,
|
||||||
ContactId c, int maxLength, int maxLatency) throws DbException {
|
ContactId c, int maxLength, int maxLatency) throws DbException {
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
long eta = now + maxLatency * 2;
|
long eta = now + maxLatency;
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
@@ -2910,7 +2910,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
+ " WHERE messageId = ? AND contactId = ?";
|
+ " WHERE messageId = ? AND contactId = ?";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
long now = clock.currentTimeMillis();
|
long now = clock.currentTimeMillis();
|
||||||
long eta = now + maxLatency * 2;
|
long eta = now + maxLatency;
|
||||||
ps.setLong(1, calculateExpiry(now, maxLatency, txCount));
|
ps.setLong(1, calculateExpiry(now, maxLatency, txCount));
|
||||||
ps.setLong(2, eta);
|
ps.setLong(2, eta);
|
||||||
ps.setBytes(3, m.getBytes());
|
ps.setBytes(3, m.getBytes());
|
||||||
|
|||||||
@@ -432,7 +432,7 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
Collection<MessageId> ids = db.getMessagesToSend(txn, contactId,
|
Collection<MessageId> ids = db.getMessagesToSend(txn, contactId,
|
||||||
ONE_MEGABYTE, MAX_LATENCY);
|
ONE_MEGABYTE, MAX_LATENCY);
|
||||||
assertEquals(singletonList(messageId), ids);
|
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
|
// The message should no longer be sendable
|
||||||
ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY);
|
ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY);
|
||||||
@@ -1816,11 +1816,12 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFasterMessageRetransmission() throws Exception {
|
public void testMessageRetransmission() throws Exception {
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
long steps[] = {now, now, now + MAX_LATENCY, now + MAX_LATENCY,
|
long steps[] = {now, now, now + MAX_LATENCY * 2,
|
||||||
now + 1 + MAX_LATENCY * 2};
|
now + MAX_LATENCY * 2 + 1};
|
||||||
Database<Connection> db = open(false, new ArrayClock(steps));
|
Database<Connection> db =
|
||||||
|
open(false, new TestMessageFactory(), new ArrayClock(steps));
|
||||||
Connection txn = db.startTransaction();
|
Connection txn = db.startTransaction();
|
||||||
|
|
||||||
// Add a contact, a shared group and a shared message
|
// Add a contact, a shared group and a shared message
|
||||||
@@ -1832,27 +1833,25 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
db.addMessage(txn, message, DELIVERED, true, null);
|
db.addMessage(txn, message, DELIVERED, true, null);
|
||||||
|
|
||||||
// Time: now
|
// 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,
|
Collection<MessageId> ids = db.getMessagesToSend(txn, contactId,
|
||||||
ONE_MEGABYTE, MAX_LATENCY);
|
ONE_MEGABYTE, MAX_LATENCY);
|
||||||
assertEquals(singletonList(messageId), ids);
|
assertEquals(singletonList(messageId), ids);
|
||||||
|
|
||||||
// Time: now
|
// Time: now
|
||||||
|
// Mark the message as sent
|
||||||
db.updateExpiryTimeAndEta(txn, contactId, messageId, MAX_LATENCY);
|
db.updateExpiryTimeAndEta(txn, contactId, messageId, MAX_LATENCY);
|
||||||
|
|
||||||
// Time: now + MAX_LATENCY
|
// The message should expire after 2 * MAX_LATENCY
|
||||||
// The message should no longer be sendable via transports with
|
assertEquals(now + MAX_LATENCY * 2, db.getNextSendTime(txn, contactId));
|
||||||
// with an equal or higher ETA
|
|
||||||
|
// Time: now + MAX_LATENCY * 2
|
||||||
|
// The message should not yet be sendable
|
||||||
ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY);
|
ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY);
|
||||||
assertTrue(ids.isEmpty());
|
assertTrue(ids.isEmpty());
|
||||||
|
|
||||||
// Time: now + MAX_LATENCY
|
// Time: now + MAX_LATENCY * 2 + 1
|
||||||
// The message should be sendable via a transport with a faster ETA
|
// The message should have expired and should now be sendable
|
||||||
ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE,
|
|
||||||
MAX_LATENCY / 2 - 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);
|
ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY);
|
||||||
assertEquals(singletonList(messageId), ids);
|
assertEquals(singletonList(messageId), ids);
|
||||||
|
|
||||||
@@ -1861,6 +1860,58 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFasterMessageRetransmission() throws Exception {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
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
|
||||||
|
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
|
||||||
|
// The message should not be sendable via the same transport
|
||||||
|
ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE, MAX_LATENCY);
|
||||||
|
assertTrue(ids.isEmpty());
|
||||||
|
|
||||||
|
// Time: now
|
||||||
|
// The message should be sendable via a transport with a faster ETA
|
||||||
|
ids = db.getMessagesToSend(txn, contactId, ONE_MEGABYTE,
|
||||||
|
MAX_LATENCY - 1);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private Database<Connection> open(boolean resume) throws Exception {
|
private Database<Connection> open(boolean resume) throws Exception {
|
||||||
return open(resume, new TestMessageFactory(), new SystemClock());
|
return open(resume, new TestMessageFactory(), new SystemClock());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user