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 24866d6bbb48f8b634b89ec8a145cbfa2032d5e3 Author: Rémi KOWALSKI <[email protected]> AuthorDate: Wed Mar 11 14:10:55 2020 +0100 JAMES-3082 define different wait times for event bus in tests --- .../mailbox/events/ErrorHandlingContract.java | 28 ++++++++-------- .../james/mailbox/events/EventBusContract.java | 38 ++++++++++++++++++++++ .../james/mailbox/events/EventBusTestFixture.java | 6 ---- .../apache/james/mailbox/events/GroupContract.java | 10 +++--- .../apache/james/mailbox/events/KeyContract.java | 7 ++-- .../james/mailbox/events/InVMEventBusTest.java | 5 +++ .../james/mailbox/events/RabbitMQEventBusTest.java | 17 +++++++--- 7 files changed, 77 insertions(+), 34 deletions(-) diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/ErrorHandlingContract.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/ErrorHandlingContract.java index 4c9612d..7979ed2 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/events/ErrorHandlingContract.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/ErrorHandlingContract.java @@ -27,8 +27,6 @@ import static org.apache.james.mailbox.events.EventBusTestFixture.GROUP_A; import static org.apache.james.mailbox.events.EventBusTestFixture.KEY_1; import static org.apache.james.mailbox.events.EventBusTestFixture.NO_KEYS; import static org.apache.james.mailbox.events.EventBusTestFixture.RETRY_BACKOFF_CONFIGURATION; -import static org.apache.james.mailbox.events.EventBusTestFixture.WAIT_CONDITION; -import static org.apache.james.mailbox.events.EventBusTestFixture.WAIT_CONDITION_LONG; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.spy; @@ -103,7 +101,7 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(eventCollector, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); - WAIT_CONDITION + getSpeedProfile().shortWaitCondition() .untilAsserted(() -> assertThat(eventCollector.getEvents()).hasSize(1)); } @@ -125,7 +123,7 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(eventCollector, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); - WAIT_CONDITION_LONG + getSpeedProfile().longWaitCondition() .untilAsserted(() -> assertThat(eventCollector.getEvents()).hasSize(1)); } @@ -161,10 +159,9 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(throwingListener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); - TimeUnit.MINUTES.sleep(1); + Thread.sleep(getSpeedProfile().getLongWaitTime().toMillis()); int numberOfCallsAfterExceedMaxRetries = throwingListener.timeElapsed.size(); - TimeUnit.MINUTES.sleep(1); - + Thread.sleep(getSpeedProfile().getLongWaitTime().toMillis()); assertThat(throwingListener.timeElapsed.size()) .isEqualTo(numberOfCallsAfterExceedMaxRetries); @@ -177,7 +174,7 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(throwingListener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); - TimeUnit.MINUTES.sleep(1); + Thread.sleep(getSpeedProfile().getLongWaitTime().toMillis()); SoftAssertions.assertSoftly(softly -> { List<Instant> timeElapsed = throwingListener.timeElapsed; softly.assertThat(timeElapsed).hasSize(RETRY_BACKOFF_CONFIGURATION.getMaxRetries() + 1); @@ -215,7 +212,7 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(listener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); - WAIT_CONDITION.until(successfulRetry::get); + getSpeedProfile().shortWaitCondition().until(successfulRetry::get); } @Test @@ -258,7 +255,7 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(eventCollector, new EventBusTestFixture.GroupA()); eventBus().dispatch(EVENT, NO_KEYS).block(); - WAIT_CONDITION + getSpeedProfile().shortWaitCondition() .untilAsserted(() -> assertThat(eventCollector.getEvents()).hasSize(1)); assertThat(deadLetter().groupsWithFailedEvents().toIterable()) @@ -284,7 +281,8 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(eventCollector, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); - WAIT_CONDITION_LONG.untilAsserted(() -> assertThat(deadLetter().failedIds(GROUP_A) + getSpeedProfile().longWaitCondition() + .untilAsserted(() -> assertThat(deadLetter().failedIds(GROUP_A) .flatMap(insertionId -> deadLetter().failedEvent(GROUP_A, insertionId)) .toIterable()) .containsOnly(EVENT)); @@ -312,7 +310,8 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(eventCollector, GROUP_A); eventBus().reDeliver(GROUP_A, EVENT).block(); - WAIT_CONDITION_LONG.untilAsserted(() -> + getSpeedProfile().longWaitCondition() + .untilAsserted(() -> assertThat( deadLetter() .failedIds(GROUP_A) @@ -343,7 +342,8 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(eventCollector, GROUP_A); eventBus().reDeliver(GROUP_A, EVENT).block(); - WAIT_CONDITION_LONG.untilAsserted(() -> assertThat(eventCollector.getEvents()).isNotEmpty()); + getSpeedProfile().longWaitCondition() + .untilAsserted(() -> assertThat(eventCollector.getEvents()).isNotEmpty()); } @Test @@ -355,7 +355,7 @@ interface ErrorHandlingContract extends EventBusContract { eventBus().register(eventCollector2, KEY_1); eventBus().reDeliver(GROUP_A, EVENT).block(); - WAIT_CONDITION + getSpeedProfile().longWaitCondition() .untilAsserted(() -> assertThat(eventCollector.getEvents()).hasSize(1)); assertThat(eventCollector2.getEvents()).isEmpty(); } diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusContract.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusContract.java index 4abbdee..055eea3 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusContract.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusContract.java @@ -19,8 +19,46 @@ package org.apache.james.mailbox.events; +import static org.awaitility.Awaitility.await; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +import org.awaitility.core.ConditionFactory; + public interface EventBusContract { + enum EnvironmentSpeedProfile { + SLOW(Duration.ofSeconds(5), Duration.ofSeconds(30)), + FAST(Duration.ofMillis(200), Duration.ofSeconds(5)); + + private final Duration shortWaitTime; + private final Duration longWaitTime; + + EnvironmentSpeedProfile(Duration shortWaitTime, Duration longWaitTime) { + this.shortWaitTime = shortWaitTime; + this.longWaitTime = longWaitTime; + } + + public Duration getShortWaitTime() { + return shortWaitTime; + } + + public Duration getLongWaitTime() { + return longWaitTime; + } + + public ConditionFactory shortWaitCondition() { + return await().timeout(new org.awaitility.Duration(this.getShortWaitTime().toMillis(), TimeUnit.MILLISECONDS)); + } + + public ConditionFactory longWaitCondition() { + return await().timeout(new org.awaitility.Duration(this.getLongWaitTime().toMillis(), TimeUnit.MILLISECONDS)); + } + } + + EnvironmentSpeedProfile getSpeedProfile(); + interface MultipleEventBusContract extends EventBusContract { EventBus eventBus2(); diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java index 1d637b0..440ba39 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/EventBusTestFixture.java @@ -21,7 +21,6 @@ package org.apache.james.mailbox.events; import static org.apache.james.mailbox.events.RetryBackoffConfiguration.DEFAULT_JITTER_FACTOR; import static org.apache.james.mailbox.events.RetryBackoffConfiguration.DEFAULT_MAX_RETRIES; -import static org.awaitility.Awaitility.await; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -35,8 +34,6 @@ import org.apache.james.mailbox.model.MailboxConstants; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.TestId; -import org.awaitility.Duration; -import org.awaitility.core.ConditionFactory; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -118,9 +115,6 @@ public interface EventBusTestFixture { GroupC GROUP_C = new GroupC(); List<Group> ALL_GROUPS = ImmutableList.of(GROUP_A, GROUP_B, GROUP_C); - ConditionFactory WAIT_CONDITION = await().timeout(Duration.FIVE_SECONDS); - ConditionFactory WAIT_CONDITION_LONG = await().timeout(Duration.ONE_MINUTE); - java.time.Duration DEFAULT_FIRST_BACKOFF = java.time.Duration.ofMillis(20); //Retry backoff configuration for testing with a shorter first backoff to accommodate the shorter retry interval in tests RetryBackoffConfiguration RETRY_BACKOFF_CONFIGURATION = RetryBackoffConfiguration.builder() diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupContract.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupContract.java index ebfd4ea..83ef2cf 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupContract.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/GroupContract.java @@ -29,7 +29,6 @@ import static org.apache.james.mailbox.events.EventBusTestFixture.GROUP_B; import static org.apache.james.mailbox.events.EventBusTestFixture.GROUP_C; import static org.apache.james.mailbox.events.EventBusTestFixture.NO_KEYS; import static org.apache.james.mailbox.events.EventBusTestFixture.ONE_SECOND; -import static org.apache.james.mailbox.events.EventBusTestFixture.WAIT_CONDITION; import static org.apache.james.mailbox.events.EventBusTestFixture.newListener; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; @@ -96,7 +95,8 @@ public interface GroupContract { IntStream.range(0, eventCount) .forEach(i -> eventBus().dispatch(EVENT, NO_KEYS).block()); - WAIT_CONDITION.atMost(org.awaitility.Duration.TEN_MINUTES).untilAsserted(() -> assertThat(finishedExecutions.get()).isEqualTo(eventCount)); + getSpeedProfile().shortWaitCondition().atMost(org.awaitility.Duration.TEN_MINUTES) + .untilAsserted(() -> assertThat(finishedExecutions.get()).isEqualTo(eventCount)); assertThat(rateExceeded).isFalse(); } @@ -145,7 +145,7 @@ public interface GroupContract { eventBus().dispatch(EVENT, NO_KEYS).subscribeOn(Schedulers.elastic()).subscribe(); - WAIT_CONDITION.atMost(org.awaitility.Duration.TEN_SECONDS) + getSpeedProfile().shortWaitCondition().atMost(org.awaitility.Duration.TEN_SECONDS) .untilAsserted(() -> assertThat(threads).hasSize(3)); assertThat(threads).doesNotHaveDuplicates(); } finally { @@ -166,7 +166,7 @@ public interface GroupContract { eventBus().register(listener, GROUP_A); eventBus().dispatch(EVENT, NO_KEYS).block(); - WAIT_CONDITION.until(successfulRetry::get); + getSpeedProfile().shortWaitCondition().until(successfulRetry::get); } @Test @@ -318,7 +318,7 @@ public interface GroupContract { eventBus().dispatch(EVENT, NO_KEYS).block(); eventBus().dispatch(EVENT_2, NO_KEYS).block(); - WAIT_CONDITION + getSpeedProfile().shortWaitCondition() .untilAsserted(() -> assertThat(listener.numberOfEventCalls()).isEqualTo(1)); } diff --git a/mailbox/api/src/test/java/org/apache/james/mailbox/events/KeyContract.java b/mailbox/api/src/test/java/org/apache/james/mailbox/events/KeyContract.java index 2c8e581..34dd756 100644 --- a/mailbox/api/src/test/java/org/apache/james/mailbox/events/KeyContract.java +++ b/mailbox/api/src/test/java/org/apache/james/mailbox/events/KeyContract.java @@ -27,7 +27,6 @@ import static org.apache.james.mailbox.events.EventBusTestFixture.KEY_1; import static org.apache.james.mailbox.events.EventBusTestFixture.KEY_2; import static org.apache.james.mailbox.events.EventBusTestFixture.NO_KEYS; import static org.apache.james.mailbox.events.EventBusTestFixture.ONE_SECOND; -import static org.apache.james.mailbox.events.EventBusTestFixture.WAIT_CONDITION; import static org.apache.james.mailbox.events.EventBusTestFixture.newListener; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; @@ -82,7 +81,7 @@ public interface KeyContract extends EventBusContract { IntStream.range(0, eventCount) .forEach(i -> eventBus().dispatch(EVENT, KEY_1).block()); - WAIT_CONDITION.atMost(org.awaitility.Duration.TEN_MINUTES) + getSpeedProfile().shortWaitCondition().atMost(org.awaitility.Duration.TEN_MINUTES) .untilAsserted(() -> assertThat(finishedExecutions.get()).isEqualTo(eventCount)); assertThat(rateExceeded).isFalse(); } @@ -108,7 +107,7 @@ public interface KeyContract extends EventBusContract { eventBus().dispatch(EVENT, KEY_1).subscribeOn(Schedulers.elastic()).subscribe(); - WAIT_CONDITION.atMost(org.awaitility.Duration.TEN_SECONDS) + getSpeedProfile().shortWaitCondition().atMost(org.awaitility.Duration.TEN_SECONDS) .untilAsserted(() -> assertThat(threads).hasSize(3)); assertThat(threads).doesNotHaveDuplicates(); } finally { @@ -362,7 +361,7 @@ public interface KeyContract extends EventBusContract { eventBus().dispatch(EVENT, KEY_1).block(); eventBus().dispatch(EVENT_2, KEY_1).block(); - WAIT_CONDITION + getSpeedProfile().shortWaitCondition() .untilAsserted(() -> assertThat(listener.numberOfEventCalls()).isEqualTo(1)); } diff --git a/mailbox/event/event-memory/src/test/java/org/apache/james/mailbox/events/InVMEventBusTest.java b/mailbox/event/event-memory/src/test/java/org/apache/james/mailbox/events/InVMEventBusTest.java index 3195baa..5a6a6ea 100644 --- a/mailbox/event/event-memory/src/test/java/org/apache/james/mailbox/events/InVMEventBusTest.java +++ b/mailbox/event/event-memory/src/test/java/org/apache/james/mailbox/events/InVMEventBusTest.java @@ -37,6 +37,11 @@ public class InVMEventBusTest implements KeyContract.SingleEventBusKeyContract, } @Override + public EnvironmentSpeedProfile getSpeedProfile() { + return EnvironmentSpeedProfile.FAST; + } + + @Override public EventBus eventBus() { return eventBus; } diff --git a/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java b/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java index 0425320..ee96d11 100644 --- a/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java +++ b/mailbox/event/event-rabbitmq/src/test/java/org/apache/james/mailbox/events/RabbitMQEventBusTest.java @@ -31,7 +31,6 @@ import static org.apache.james.mailbox.events.EventBusTestFixture.EVENT; import static org.apache.james.mailbox.events.EventBusTestFixture.GROUP_A; import static org.apache.james.mailbox.events.EventBusTestFixture.KEY_1; import static org.apache.james.mailbox.events.EventBusTestFixture.NO_KEYS; -import static org.apache.james.mailbox.events.EventBusTestFixture.WAIT_CONDITION; import static org.apache.james.mailbox.events.EventBusTestFixture.newAsyncListener; import static org.apache.james.mailbox.events.EventBusTestFixture.newListener; import static org.apache.james.mailbox.events.GroupRegistration.WorkQueueName.MAILBOX_EVENT_WORK_QUEUE_PREFIX; @@ -77,7 +76,6 @@ import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.stubbing.Answer; import com.google.common.collect.ImmutableSet; - import reactor.core.publisher.Mono; import reactor.rabbitmq.BindingSpecification; import reactor.rabbitmq.ExchangeSpecification; @@ -100,6 +98,11 @@ class RabbitMQEventBusTest implements GroupContract.SingleEventBusGroupContract, private RoutingKeyConverter routingKeyConverter; private MemoryEventDeadLetters memoryEventDeadLetters; + @Override + public EnvironmentSpeedProfile getSpeedProfile() { + return EnvironmentSpeedProfile.SLOW; + } + @BeforeEach void setUp() { memoryEventDeadLetters = new MemoryEventDeadLetters(); @@ -184,6 +187,10 @@ class RabbitMQEventBusTest implements GroupContract.SingleEventBusGroupContract, class ConcurrentTest implements EventBusConcurrentTestContract.MultiEventBusConcurrentContract, EventBusConcurrentTestContract.SingleEventBusConcurrentContract { + @Override + public EnvironmentSpeedProfile getSpeedProfile() { + return EnvironmentSpeedProfile.SLOW; + } @Test void rabbitMQEventBusShouldHandleBulksGracefully() throws Exception { EventBusTestFixture.MailboxListenerCountingSuccessfulExecution countingListener1 = newCountingListener(); @@ -244,15 +251,15 @@ class RabbitMQEventBusTest implements GroupContract.SingleEventBusGroupContract, eventBus3.register(eventBus3Listener, GROUP_A); eventBus.dispatch(EVENT, NO_KEYS).block(); - WAIT_CONDITION + getSpeedProfile().shortWaitCondition() .untilAsserted(() -> assertThat(eventBusListener.numberOfEventCalls()).isEqualTo(1)); eventBus.stop(); - WAIT_CONDITION + getSpeedProfile().shortWaitCondition() .untilAsserted(() -> assertThat(eventBus2Listener.numberOfEventCalls()).isEqualTo(1)); eventBus2.stop(); - WAIT_CONDITION + getSpeedProfile().shortWaitCondition() .untilAsserted(() -> assertThat(eventBus3Listener.numberOfEventCalls()).isEqualTo(1)); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
