diff --git a/briar-core/src/test/java/org/briarproject/briar/autodelete/AbstractAutoDeleteTest.java b/briar-core/src/test/java/org/briarproject/briar/autodelete/AbstractAutoDeleteTest.java index 74acfd83b..6f1081791 100644 --- a/briar-core/src/test/java/org/briarproject/briar/autodelete/AbstractAutoDeleteTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/autodelete/AbstractAutoDeleteTest.java @@ -4,9 +4,6 @@ import org.briarproject.bramble.api.contact.Contact; import org.briarproject.bramble.api.contact.ContactId; import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.db.DbException; -import org.briarproject.bramble.api.event.Event; -import org.briarproject.bramble.api.event.EventBus; -import org.briarproject.bramble.api.event.EventListener; import org.briarproject.bramble.api.sync.GroupId; import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.system.TimeTravelModule; @@ -24,12 +21,8 @@ import org.junit.Before; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.concurrent.CountDownLatch; - -import javax.annotation.Nonnull; import static java.util.Collections.sort; -import static java.util.concurrent.TimeUnit.MINUTES; import static org.briarproject.bramble.api.cleanup.CleanupManager.BATCH_DELAY_MS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -151,30 +144,4 @@ public abstract class AbstractAutoDeleteTest extends assertEquals("messageCount", messageCount, gc.getMsgCount()); assertEquals("unreadCount", unreadCount, gc.getUnreadCount()); } - - /** - * Broadcasts a marker event and waits for it to be delivered, which - * indicates that all previously broadcast events have been delivered. - */ - protected void waitForEvents(BriarIntegrationTestComponent component) - throws Exception { - CountDownLatch latch = new CountDownLatch(1); - MarkerEvent marker = new MarkerEvent(); - EventBus eventBus = component.getEventBus(); - eventBus.addListener(new EventListener() { - @Override - public void eventOccurred(@Nonnull Event e) { - if (e == marker) { - latch.countDown(); - eventBus.removeListener(this); - } - } - }); - eventBus.broadcast(marker); - if (!latch.await(1, MINUTES)) fail(); - } - - private static class MarkerEvent extends Event { - } - } diff --git a/briar-core/src/test/java/org/briarproject/briar/forum/ForumManagerTest.java b/briar-core/src/test/java/org/briarproject/briar/forum/ForumManagerTest.java index 05cc4544b..e5d6338ec 100644 --- a/briar-core/src/test/java/org/briarproject/briar/forum/ForumManagerTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/forum/ForumManagerTest.java @@ -166,25 +166,27 @@ public class ForumManagerTest @Test public void testForumPostDeliveredAfterParent() throws Exception { - // add one forum post without the parent + // Add a parent post and a child post long time = c0.getClock().currentTimeMillis(); ForumPost post1 = createForumPost(groupId0, null, "a", time); ForumPost post2 = createForumPost(groupId0, post1, "a", time); - forumManager0.addLocalPost(post2); - assertEquals(1, forumManager0.getPostHeaders(groupId0).size()); - assertEquals(0, forumManager1.getPostHeaders(groupId0).size()); - - // send post to 1 without waiting for message delivery - sync0To1(1, false); - assertEquals(0, forumManager1.getPostHeaders(groupId0).size()); - - // now add the parent post as well forumManager0.addLocalPost(post1); + forumManager0.addLocalPost(post2); assertEquals(2, forumManager0.getPostHeaders(groupId0).size()); assertEquals(0, forumManager1.getPostHeaders(groupId0).size()); - // and send it over to 1 and wait for a second message to be delivered - sync0To1(2, true); + // Unshare the parent so it won't be sent yet + setMessageNotShared(c0, post1.getMessage().getId()); + + // Send the child post to 1 - it should be validated but not delivered + // yet, as the parent is missing, and no headers should be returned yet + syncMessage(c0, c1, contactId1From0, 1, 0, 1, 0); + assertEquals(0, forumManager1.getPostHeaders(groupId0).size()); + + // Now send the parent post to 1 - it should be validated, both + // posts should be delivered, and both headers should be returned + setMessageShared(c0, post1.getMessage().getId()); + syncMessage(c0, c1, contactId1From0, 1, 0, 0, 2); assertEquals(2, forumManager1.getPostHeaders(groupId0).size()); } diff --git a/briar-core/src/test/java/org/briarproject/briar/messaging/AutoDeleteIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/messaging/AutoDeleteIntegrationTest.java index 911eedcf8..3ed03312e 100644 --- a/briar-core/src/test/java/org/briarproject/briar/messaging/AutoDeleteIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/messaging/AutoDeleteIntegrationTest.java @@ -729,20 +729,6 @@ public class AutoDeleteIntegrationTest extends AbstractAutoDeleteTest { component.getClock().currentTimeMillis(), "image/jpeg", in); } - private void setMessageNotShared(BriarIntegrationTestComponent component, - MessageId messageId) throws Exception { - DatabaseComponent db = component.getDatabaseComponent(); - - db.transaction(false, txn -> db.setMessageNotShared(txn, messageId)); - } - - private void setMessageShared(BriarIntegrationTestComponent component, - MessageId messageId) throws Exception { - DatabaseComponent db = component.getDatabaseComponent(); - - db.transaction(false, txn -> db.setMessageShared(txn, messageId)); - } - private boolean messageIsDeleted(BriarIntegrationTestComponent component, MessageId messageId) throws DbException { DatabaseComponent db = component.getDatabaseComponent(); @@ -754,5 +740,4 @@ public class AutoDeleteIntegrationTest extends AbstractAutoDeleteTest { return true; } } - } diff --git a/briar-core/src/test/java/org/briarproject/briar/privategroup/PrivateGroupIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/privategroup/PrivateGroupIntegrationTest.java index 8d9ebbeb0..4ea76ff72 100644 --- a/briar-core/src/test/java/org/briarproject/briar/privategroup/PrivateGroupIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/privategroup/PrivateGroupIntegrationTest.java @@ -175,8 +175,10 @@ public class PrivateGroupIntegrationTest // 1 reveals the contact relationship to 2 assertNotNull(contactId2From1); groupInvitationManager1.revealRelationship(contactId2From1, groupId0); - sync1To2(1, true); - sync2To1(1, true); + sync1To2(1, true); // 1 sends an invitation protocol join message + // 2 sends an invitation protocol join message and three private group + // protocol join messages, which 1 has already seen + syncMessage(c2, c1, contactId1From2, 1, 3, 0, 1); // their relationship is now revealed assertEquals(REVEALED_BY_US, diff --git a/briar-core/src/test/java/org/briarproject/briar/sharing/ForumSharingIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/sharing/ForumSharingIntegrationTest.java index 72c176284..a93049018 100644 --- a/briar-core/src/test/java/org/briarproject/briar/sharing/ForumSharingIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/sharing/ForumSharingIntegrationTest.java @@ -783,8 +783,8 @@ public class ForumSharingIntegrationTest // invitee accepts respondToRequest(contactId0From1, true); - // sync response back - sync1To0(1, true); + // sync response and both posts back (0 has already seen the posts) + syncMessage(c1, c0, contactId0From1, 1, 2, 0, 1); eventWaiter.await(TIMEOUT, 1); assertResponseReceived(listener0, contactId1From0, true); diff --git a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java index e7ea44960..47eff68cd 100644 --- a/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java +++ b/briar-core/src/test/java/org/briarproject/briar/test/BriarIntegrationTest.java @@ -15,6 +15,7 @@ import org.briarproject.bramble.api.data.BdfStringUtils; import org.briarproject.bramble.api.db.DatabaseComponent; import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.event.Event; +import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventListener; import org.briarproject.bramble.api.identity.Identity; import org.briarproject.bramble.api.identity.IdentityManager; @@ -26,6 +27,7 @@ import org.briarproject.bramble.api.sync.MessageFactory; import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.bramble.api.sync.event.MessageStateChangedEvent; import org.briarproject.bramble.api.sync.event.MessagesAckedEvent; +import org.briarproject.bramble.api.sync.event.MessagesSentEvent; import org.briarproject.bramble.test.TestTransportConnectionReader; import org.briarproject.bramble.test.TestTransportConnectionWriter; import org.briarproject.bramble.test.TestUtils; @@ -44,16 +46,21 @@ import org.junit.Before; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Logger; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; import static java.util.concurrent.Executors.newSingleThreadExecutor; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.logging.Level.WARNING; import static java.util.logging.Logger.getLogger; import static junit.framework.Assert.assertNotNull; @@ -398,14 +405,28 @@ public abstract class BriarIntegrationTest 0) { + validationWaiter.await(TIMEOUT, numPendingOrInvalid); } + assertEquals("Messages validated", numPendingOrInvalid, + validationCounter.getAndSet(0)); + + if (numDelivered > 0) { + deliveryWaiter.await(TIMEOUT, numDelivered); + } + assertEquals("Messages delivered", numDelivered, + deliveryCounter.getAndSet(0)); + try { - messageSemaphore.tryAcquire(num, TIMEOUT, MILLISECONDS); + messageSemaphore.tryAcquire(numNew, TIMEOUT, MILLISECONDS); } catch (InterruptedException e) { LOG.info("Interrupted while waiting for messages"); Thread.currentThread().interrupt(); @@ -448,6 +479,11 @@ public abstract class BriarIntegrationTest autoDeleteManager.getAutoDeleteTimer(txn, contactId, timestamp)); } + + protected void setMessageNotShared(BriarIntegrationTestComponent component, + MessageId messageId) throws Exception { + DatabaseComponent db = component.getDatabaseComponent(); + + db.transaction(false, txn -> db.setMessageNotShared(txn, messageId)); + } + + protected void setMessageShared(BriarIntegrationTestComponent component, + MessageId messageId) throws Exception { + DatabaseComponent db = component.getDatabaseComponent(); + + db.transaction(false, txn -> db.setMessageShared(txn, messageId)); + } + + /** + * Broadcasts a marker event and waits for it to be delivered, which + * indicates that all previously broadcast events have been delivered. + */ + protected void waitForEvents(BriarIntegrationTestComponent component) + throws Exception { + CountDownLatch latch = new CountDownLatch(1); + MarkerEvent marker = new MarkerEvent(); + EventBus eventBus = component.getEventBus(); + eventBus.addListener(new EventListener() { + @Override + public void eventOccurred(@Nonnull Event e) { + if (e == marker) { + latch.countDown(); + eventBus.removeListener(this); + } + } + }); + eventBus.broadcast(marker); + if (!latch.await(1, MINUTES)) fail(); + } + + private static class MarkerEvent extends Event { + } + + private static class SendListener implements EventListener { + + private final Set sent = new HashSet<>(); + + @Override + public void eventOccurred(Event e) { + if (e instanceof MessagesSentEvent) { + sent.addAll(((MessagesSentEvent) e).getMessageIds()); + } + } + } }