This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 819bf96bb28604a0dfca559d2aa606cdc6f31e5f Author: Benoit Tellier <[email protected]> AuthorDate: Wed Jun 24 09:18:29 2020 +0700 JAMES-3177 Add tests to demonstrate SelectedMailboxImpl is not thread safe --- .../processor/base/SelectedMailboxImplTest.java | 60 ++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java b/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java index 1e0cd54..b62b654 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/processor/base/SelectedMailboxImplTest.java @@ -43,6 +43,7 @@ import org.apache.james.mailbox.MailboxSessionUtil; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.MessageUid; import org.apache.james.mailbox.ModSeq; +import org.apache.james.mailbox.events.Event; import org.apache.james.mailbox.events.EventBus; import org.apache.james.mailbox.events.MailboxIdRegistrationKey; import org.apache.james.mailbox.events.MailboxListener; @@ -52,11 +53,13 @@ import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.MessageMetaData; import org.apache.james.mailbox.model.SearchQuery; import org.apache.james.mailbox.model.TestId; +import org.apache.james.mailbox.model.UpdatedFlags; import org.apache.james.mailbox.store.event.EventFactory; import org.apache.james.mailbox.store.mail.model.DefaultMessageId; import org.apache.james.util.concurrent.NamedThreadFactory; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.mockito.stubbing.Answer; import org.slf4j.Logger; @@ -71,6 +74,7 @@ public class SelectedMailboxImplTest { private static final MessageUid EMITTED_EVENT_UID = MessageUid.of(5); private static final ModSeq MOD_SEQ = ModSeq.of(12); private static final int SIZE = 38; + private static final String CUSTOM_FLAG = "custom"; private ExecutorService executorService; private MailboxManager mailboxManager; @@ -129,6 +133,32 @@ public class SelectedMailboxImplTest { assertThat(selectedMailbox.getLastUid().get()).isEqualTo(EMITTED_EVENT_UID); } + @Ignore("JAMES-3177 SelectedMailboxImpl is not thread safe") + @Test + public void customFlagsEventShouldNotFailWhenConcurrentWithCreation() throws Exception { + AtomicInteger successCount = new AtomicInteger(0); + doAnswer(generateEmitCustomFlagEventAnswer(successCount)) + .when(eventBus) + .register(any(MailboxListener.class), eq(mailboxIdRegistrationKey)); + + new SelectedMailboxImpl(mailboxManager, eventBus, imapSession, messageManager); + + assertThat(successCount.get()).isEqualTo(1); + } + + @Ignore("JAMES-3177 SelectedMailboxImpl is not thread safe") + @Test + public void applicableFlagsShouldBeWellUpdatedWhenConcurrentWithCreation() throws Exception { + AtomicInteger successCount = new AtomicInteger(0); + doAnswer(generateEmitCustomFlagEventAnswer(successCount)) + .when(eventBus) + .register(any(MailboxListener.class), eq(mailboxIdRegistrationKey)); + + SelectedMailboxImpl selectedMailbox = new SelectedMailboxImpl(mailboxManager, eventBus, imapSession, messageManager); + + assertThat(selectedMailbox.getApplicableFlags().getUserFlags()).containsOnly(CUSTOM_FLAG); + } + @Test public void concurrentEventShouldBeProcessedSuccessfullyDuringInitialisation() throws Exception { AtomicInteger successCount = new AtomicInteger(0); @@ -155,12 +185,20 @@ public class SelectedMailboxImplTest { } private Answer<Mono<Registration>> generateEmitEventAnswer(AtomicInteger success) { + return generateEmitEventAnswer(event(), success); + } + + private Answer<Mono<Registration>> generateEmitCustomFlagEventAnswer(AtomicInteger success) { + return generateEmitEventAnswer(customFlagEvent(), success); + } + + private Answer<Mono<Registration>> generateEmitEventAnswer(Event event, AtomicInteger success) { return invocation -> { Object[] args = invocation.getArguments(); MailboxListener mailboxListener = (MailboxListener) args[0]; executorService.submit(() -> { try { - emitEvent(mailboxListener); + mailboxListener.event(event); success.incrementAndGet(); } catch (Exception e) { LOGGER.error("Error while processing event on a concurrent thread", e); @@ -170,12 +208,26 @@ public class SelectedMailboxImplTest { }; } - private void emitEvent(MailboxListener mailboxListener) throws Exception { - mailboxListener.event(EventFactory.added() + private Event event() { + return EventFactory.added() .randomEventId() .mailboxSession(MailboxSessionUtil.create(Username.of("user"))) .mailbox(mailbox) .addMetaData(new MessageMetaData(EMITTED_EVENT_UID, MOD_SEQ, new Flags(), SIZE, new Date(), new DefaultMessageId())) - .build()); + .build(); + } + + private Event customFlagEvent() { + return EventFactory.flagsUpdated() + .randomEventId() + .mailboxSession(MailboxSessionUtil.create(Username.of("user"))) + .mailbox(mailbox) + .updatedFlag(UpdatedFlags.builder() + .modSeq(ModSeq.of(36)) + .newFlags(new Flags(CUSTOM_FLAG)) + .oldFlags(new Flags()) + .uid(MessageUid.of(12)) + .build()) + .build(); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
