Add integration test for eager retransmission.

This commit is contained in:
akwizgran
2021-06-16 12:26:54 +01:00
parent 32e9bf01ec
commit d5853e8403
4 changed files with 62 additions and 15 deletions

View File

@@ -25,7 +25,7 @@ public class TestDuplexTransportConnection
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public TestDuplexTransportConnection(InputStream in, OutputStream out) { public TestDuplexTransportConnection(InputStream in, OutputStream out) {
reader = new TestTransportConnectionReader(in); reader = new TestTransportConnectionReader(in);
writer = new TestTransportConnectionWriter(out); writer = new TestTransportConnectionWriter(out, false);
} }
@Override @Override

View File

@@ -15,10 +15,13 @@ public class TestTransportConnectionWriter
implements TransportConnectionWriter { implements TransportConnectionWriter {
private final OutputStream out; private final OutputStream out;
private final boolean lossyAndCheap;
private final CountDownLatch disposed = new CountDownLatch(1); private final CountDownLatch disposed = new CountDownLatch(1);
public TestTransportConnectionWriter(OutputStream out) { public TestTransportConnectionWriter(OutputStream out,
boolean lossyAndCheap) {
this.out = out; this.out = out;
this.lossyAndCheap = lossyAndCheap;
} }
public CountDownLatch getDisposedLatch() { public CountDownLatch getDisposedLatch() {
@@ -37,7 +40,7 @@ public class TestTransportConnectionWriter
@Override @Override
public boolean isLossyAndCheap() { public boolean isLossyAndCheap() {
return false; return lossyAndCheap;
} }
@Override @Override

View File

@@ -11,7 +11,9 @@ import org.briarproject.bramble.api.identity.IdentityManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager; import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.GroupId;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.bramble.api.sync.event.MessageStateChangedEvent; import org.briarproject.bramble.api.sync.event.MessageStateChangedEvent;
import org.briarproject.bramble.api.sync.event.MessagesSentEvent;
import org.briarproject.bramble.test.TestDatabaseConfigModule; import org.briarproject.bramble.test.TestDatabaseConfigModule;
import org.briarproject.bramble.test.TestTransportConnectionReader; import org.briarproject.bramble.test.TestTransportConnectionReader;
import org.briarproject.bramble.test.TestTransportConnectionWriter; import org.briarproject.bramble.test.TestTransportConnectionWriter;
@@ -71,7 +73,16 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
} }
@Test @Test
public void testWriteAndRead() throws Exception { public void testWriteAndReadWithLazyRetransmission() throws Exception {
testWriteAndRead(false);
}
@Test
public void testWriteAndReadWithEagerRetransmission() throws Exception {
testWriteAndRead(true);
}
private void testWriteAndRead(boolean eager) throws Exception {
// Create the identities // Create the identities
Identity aliceIdentity = Identity aliceIdentity =
alice.getIdentityManager().createIdentity("Alice"); alice.getIdentityManager().createIdentity("Alice");
@@ -86,16 +97,20 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
bob.getEventBus().addListener(listener); bob.getEventBus().addListener(listener);
// Alice sends a private message to Bob // Alice sends a private message to Bob
sendMessage(alice, bobId); sendMessage(alice, bobId);
// Sync Alice's client versions and transport properties // Sync Alice's client versions
read(bob, write(alice, bobId), 2); read(bob, write(alice, bobId, eager, 1), 1);
// Sync Bob's client versions and transport properties // Sync Bob's client versions
read(alice, write(bob, aliceId), 2); read(alice, write(bob, aliceId, eager, 1), 1);
// Sync the private message and the attachment // Sync Alice's client versions, the private message and the attachment
read(bob, write(alice, bobId), 2); read(bob, write(alice, bobId, eager, 3), 3);
// Bob should have received the private message // Bob should have received the private message
assertTrue(listener.messageAdded); assertTrue(listener.messageAdded);
// Bob should have received the attachment // Bob should have received the attachment
assertTrue(listener.attachmentAdded); assertTrue(listener.attachmentAdded);
// Sync messages from Alice to Bob again. If using eager
// retransmission, the three unacked messages should be sent again.
// They're all duplicates, so no further deliveries should occur
read(bob, write(alice, bobId, eager, eager ? 3 : 0), 0);
} }
private ContactId setUp(SimplexMessagingIntegrationTestComponent device, private ContactId setUp(SimplexMessagingIntegrationTestComponent device,
@@ -149,15 +164,24 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
} }
private byte[] write(SimplexMessagingIntegrationTestComponent device, private byte[] write(SimplexMessagingIntegrationTestComponent device,
ContactId contactId) throws Exception { ContactId contactId, boolean eager, int transmissions)
throws Exception {
// Listen for message transmissions
MessageTransmissionListener listener =
new MessageTransmissionListener(transmissions);
device.getEventBus().addListener(listener);
// Write the outgoing stream // Write the outgoing stream
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
TestTransportConnectionWriter writer = TestTransportConnectionWriter writer =
new TestTransportConnectionWriter(out); new TestTransportConnectionWriter(out, eager);
device.getConnectionManager().manageOutgoingConnection(contactId, device.getConnectionManager().manageOutgoingConnection(contactId,
SIMPLEX_TRANSPORT_ID, writer); SIMPLEX_TRANSPORT_ID, writer);
// Wait for the writer to be disposed // Wait for the writer to be disposed
writer.getDisposedLatch().await(TIMEOUT_MS, MILLISECONDS); writer.getDisposedLatch().await(TIMEOUT_MS, MILLISECONDS);
// Check that the expected number of messages were sent
assertTrue(listener.sent.await(TIMEOUT_MS, MILLISECONDS));
// Clean up the listener
device.getEventBus().removeListener(listener);
// Return the contents of the stream // Return the contents of the stream
return out.toByteArray(); return out.toByteArray();
} }
@@ -178,6 +202,24 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
deleteTestDirectory(testDir); deleteTestDirectory(testDir);
} }
@NotNullByDefault
private static class MessageTransmissionListener implements EventListener {
private final CountDownLatch sent;
private MessageTransmissionListener(int transmissions) {
sent = new CountDownLatch(transmissions);
}
@Override
public void eventOccurred(Event e) {
if (e instanceof MessagesSentEvent) {
MessagesSentEvent m = (MessagesSentEvent) e;
for (MessageId ignored : m.getMessageIds()) sent.countDown();
}
}
}
@NotNullByDefault @NotNullByDefault
private static class MessageDeliveryListener implements EventListener { private static class MessageDeliveryListener implements EventListener {
@@ -191,7 +233,9 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
public void eventOccurred(Event e) { public void eventOccurred(Event e) {
if (e instanceof MessageStateChangedEvent) { if (e instanceof MessageStateChangedEvent) {
MessageStateChangedEvent m = (MessageStateChangedEvent) e; MessageStateChangedEvent m = (MessageStateChangedEvent) e;
if (m.getState().equals(DELIVERED)) delivered.countDown(); if (!m.isLocal() && m.getState().equals(DELIVERED)) {
delivered.countDown();
}
} }
} }
} }

View File

@@ -429,7 +429,7 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
// Write the messages to a transport stream // Write the messages to a transport stream
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
TestTransportConnectionWriter writer = TestTransportConnectionWriter writer =
new TestTransportConnectionWriter(out); new TestTransportConnectionWriter(out, false);
fromComponent.getConnectionManager().manageOutgoingConnection(toId, fromComponent.getConnectionManager().manageOutgoingConnection(toId,
SIMPLEX_TRANSPORT_ID, writer); SIMPLEX_TRANSPORT_ID, writer);
writer.getDisposedLatch().await(TIMEOUT, MILLISECONDS); writer.getDisposedLatch().await(TIMEOUT, MILLISECONDS);
@@ -487,7 +487,7 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
// start outgoing connection // start outgoing connection
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
TestTransportConnectionWriter writer = TestTransportConnectionWriter writer =
new TestTransportConnectionWriter(out); new TestTransportConnectionWriter(out, false);
fromComponent.getConnectionManager().manageOutgoingConnection(toId, fromComponent.getConnectionManager().manageOutgoingConnection(toId,
SIMPLEX_TRANSPORT_ID, writer); SIMPLEX_TRANSPORT_ID, writer);
writer.getDisposedLatch().await(TIMEOUT, MILLISECONDS); writer.getDisposedLatch().await(TIMEOUT, MILLISECONDS);