mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Add integration test for eager retransmission.
This commit is contained in:
@@ -25,7 +25,7 @@ public class TestDuplexTransportConnection
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public TestDuplexTransportConnection(InputStream in, OutputStream out) {
|
||||
reader = new TestTransportConnectionReader(in);
|
||||
writer = new TestTransportConnectionWriter(out);
|
||||
writer = new TestTransportConnectionWriter(out, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,10 +15,13 @@ public class TestTransportConnectionWriter
|
||||
implements TransportConnectionWriter {
|
||||
|
||||
private final OutputStream out;
|
||||
private final boolean lossyAndCheap;
|
||||
private final CountDownLatch disposed = new CountDownLatch(1);
|
||||
|
||||
public TestTransportConnectionWriter(OutputStream out) {
|
||||
public TestTransportConnectionWriter(OutputStream out,
|
||||
boolean lossyAndCheap) {
|
||||
this.out = out;
|
||||
this.lossyAndCheap = lossyAndCheap;
|
||||
}
|
||||
|
||||
public CountDownLatch getDisposedLatch() {
|
||||
@@ -37,7 +40,7 @@ public class TestTransportConnectionWriter
|
||||
|
||||
@Override
|
||||
public boolean isLossyAndCheap() {
|
||||
return false;
|
||||
return lossyAndCheap;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,7 +11,9 @@ import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
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.MessagesSentEvent;
|
||||
import org.briarproject.bramble.test.TestDatabaseConfigModule;
|
||||
import org.briarproject.bramble.test.TestTransportConnectionReader;
|
||||
import org.briarproject.bramble.test.TestTransportConnectionWriter;
|
||||
@@ -71,7 +73,16 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
}
|
||||
|
||||
@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
|
||||
Identity aliceIdentity =
|
||||
alice.getIdentityManager().createIdentity("Alice");
|
||||
@@ -86,16 +97,20 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
bob.getEventBus().addListener(listener);
|
||||
// Alice sends a private message to Bob
|
||||
sendMessage(alice, bobId);
|
||||
// Sync Alice's client versions and transport properties
|
||||
read(bob, write(alice, bobId), 2);
|
||||
// Sync Bob's client versions and transport properties
|
||||
read(alice, write(bob, aliceId), 2);
|
||||
// Sync the private message and the attachment
|
||||
read(bob, write(alice, bobId), 2);
|
||||
// Sync Alice's client versions
|
||||
read(bob, write(alice, bobId, eager, 1), 1);
|
||||
// Sync Bob's client versions
|
||||
read(alice, write(bob, aliceId, eager, 1), 1);
|
||||
// Sync Alice's client versions, the private message and the attachment
|
||||
read(bob, write(alice, bobId, eager, 3), 3);
|
||||
// Bob should have received the private message
|
||||
assertTrue(listener.messageAdded);
|
||||
// Bob should have received the attachment
|
||||
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,
|
||||
@@ -149,15 +164,24 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
}
|
||||
|
||||
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
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
TestTransportConnectionWriter writer =
|
||||
new TestTransportConnectionWriter(out);
|
||||
new TestTransportConnectionWriter(out, eager);
|
||||
device.getConnectionManager().manageOutgoingConnection(contactId,
|
||||
SIMPLEX_TRANSPORT_ID, writer);
|
||||
// Wait for the writer to be disposed
|
||||
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 out.toByteArray();
|
||||
}
|
||||
@@ -178,6 +202,24 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
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
|
||||
private static class MessageDeliveryListener implements EventListener {
|
||||
|
||||
@@ -191,7 +233,9 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof MessageStateChangedEvent) {
|
||||
MessageStateChangedEvent m = (MessageStateChangedEvent) e;
|
||||
if (m.getState().equals(DELIVERED)) delivered.countDown();
|
||||
if (!m.isLocal() && m.getState().equals(DELIVERED)) {
|
||||
delivered.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -429,7 +429,7 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
// Write the messages to a transport stream
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
TestTransportConnectionWriter writer =
|
||||
new TestTransportConnectionWriter(out);
|
||||
new TestTransportConnectionWriter(out, false);
|
||||
fromComponent.getConnectionManager().manageOutgoingConnection(toId,
|
||||
SIMPLEX_TRANSPORT_ID, writer);
|
||||
writer.getDisposedLatch().await(TIMEOUT, MILLISECONDS);
|
||||
@@ -487,7 +487,7 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
// start outgoing connection
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
TestTransportConnectionWriter writer =
|
||||
new TestTransportConnectionWriter(out);
|
||||
new TestTransportConnectionWriter(out, false);
|
||||
fromComponent.getConnectionManager().manageOutgoingConnection(toId,
|
||||
SIMPLEX_TRANSPORT_ID, writer);
|
||||
writer.getDisposedLatch().await(TIMEOUT, MILLISECONDS);
|
||||
|
||||
Reference in New Issue
Block a user