You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2021/07/02 04:52:55 UTC

[GitHub] [james-project] chibenwa opened a new pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

chibenwa opened a new pull request #519:
URL: https://github.com/apache/james-project/pull/519


   ## Why?
   
   As described in JAMES-3605, RabbitMQ consumers can end up being stuck.
   
   In such a scenario we are not able to detect the failure and end up relying on cunsumer reports to restart James.
   
   We would like to have a health-check, allowing automatic testing about mail reception. (Both periodical logs and also a HTTP endpoint for integration with a monitoring system)...
   
   So a healthcheck no longer for a technical component but for a feature.
   
   
   ## How?
   
   In `healthcheck.properties` on could configure a user to run reception checks:
   
   ```
   reception.checks.user=test@domain.tld
   ```
   
   If configured, the healthcheck would then send a mail to the user, await the email via the eventbus, then retrieve its content.
   
   To be placed in a new `server/container/feature-checks` maven project.
   
   ## Definition of done
   
    - Given a paused RabbitMQ the check fails for the distributed server
    - Given a working James the check passes.
   
   ## Disclaimer
   
   The idea had been first expressed by @mbaechler years ago...


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa commented on pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
chibenwa commented on pull request #519:
URL: https://github.com/apache/james-project/pull/519#issuecomment-874399258


   Just squashed


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] Arsnael commented on a change in pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
Arsnael commented on a change in pull request #519:
URL: https://github.com/apache/james-project/pull/519#discussion_r663631184



##########
File path: server/protocols/webadmin-integration-test/distributed-webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/rabbitmq/MailReceptionCheckIntegrationTest.java
##########
@@ -0,0 +1,136 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.webadmin.integration.rabbitmq;
+
+import static io.restassured.RestAssured.given;
+import static org.apache.james.jmap.JMAPTestingConstants.ALICE;
+import static org.apache.james.jmap.JMAPTestingConstants.ALICE_PASSWORD;
+import static org.apache.james.jmap.JMAPTestingConstants.DOMAIN;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+
+import java.time.Duration;
+import java.util.Optional;
+
+import org.apache.james.CassandraExtension;
+import org.apache.james.CassandraRabbitMQJamesConfiguration;
+import org.apache.james.CassandraRabbitMQJamesServerMain;
+import org.apache.james.DockerElasticSearchExtension;
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.JamesServerBuilder;
+import org.apache.james.JamesServerExtension;
+import org.apache.james.SearchConfiguration;
+import org.apache.james.core.healthcheck.ResultStatus;
+import org.apache.james.events.RetryBackoffConfiguration;
+import org.apache.james.healthcheck.MailReceptionCheck;
+import org.apache.james.junit.categories.BasicFeature;
+import org.apache.james.modules.AwsS3BlobStoreExtension;
+import org.apache.james.modules.RabbitMQExtension;
+import org.apache.james.modules.blobstore.BlobStoreConfiguration;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.WebAdminGuiceProbe;
+import org.apache.james.webadmin.WebAdminUtils;
+import org.apache.james.webadmin.integration.WebadminIntegrationTestModule;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import io.restassured.RestAssured;
+
+@Tag(BasicFeature.TAG)
+class MailReceptionCheckIntegrationTest {
+    private static final RabbitMQExtension RABBIT_MQ_EXTENSION = new RabbitMQExtension();
+    public static final CassandraExtension CASSANDRA_EXTENSION = new CassandraExtension();
+
+    @RegisterExtension
+    static JamesServerExtension testExtension = new JamesServerBuilder<CassandraRabbitMQJamesConfiguration>(tmpDir ->
+        CassandraRabbitMQJamesConfiguration.builder()
+            .workingDirectory(tmpDir)
+            .configurationFromClasspath()
+            .blobStore(BlobStoreConfiguration.builder()
+                    .s3()
+                    .disableCache()
+                    .deduplication()
+                    .noCryptoConfig())
+            .searchConfiguration(SearchConfiguration.elasticSearch())
+            .build())
+        .extension(new DockerElasticSearchExtension())
+        .extension(CASSANDRA_EXTENSION)
+        .extension(new AwsS3BlobStoreExtension())
+        .extension(RABBIT_MQ_EXTENSION)
+        .server(configuration -> CassandraRabbitMQJamesServerMain.createServer(configuration)
+            .overrideWith(new WebadminIntegrationTestModule())
+            .overrideWith(binder -> binder.bind(MailReceptionCheck.Configuration.class)
+                .toInstance(new MailReceptionCheck.Configuration(
+                    Optional.of(ALICE), Duration.ofSeconds(10))))
+            // Enforce a single eventBus retry. Required as Current Quotas are handled by the eventBus.
+            .overrideWith(binder -> binder.bind(RetryBackoffConfiguration.class)
+                .toInstance(RetryBackoffConfiguration.builder()
+                    .maxRetries(1)
+                    .firstBackoff(Duration.ofMillis(2))
+                    .jitterFactor(0.5)
+                    .build())))
+        .build();
+
+    @BeforeEach
+    void setUp(GuiceJamesServer guiceJamesServer) throws Exception {
+        guiceJamesServer.getProbe(DataProbeImpl.class)
+            .fluent()
+            .addDomain(DOMAIN)
+            .addUser(ALICE.asString(), ALICE_PASSWORD);
+
+        WebAdminGuiceProbe webAdminGuiceProbe = guiceJamesServer.getProbe(WebAdminGuiceProbe.class);
+        RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(webAdminGuiceProbe.getWebAdminPort())
+            .build();
+    }
+
+    @Test
+    void shouldBeHealthy() {
+        given()
+            .pathParam("componentName", "MailReceptionCheck")
+        .when()
+            .get("/healthcheck/checks/{componentName}").prettyPeek()
+        .then()
+            .body("componentName", equalTo("MailReceptionCheck"))
+            .body("escapedComponentName", equalTo("MailReceptionCheck"))
+            .body("status", equalTo(ResultStatus.HEALTHY.getValue()))
+            .body("cause", is(nullValue()));
+    }
+
+    @Test
+    void shouldBeUnhealthyWhenRabbitMQIsPaused() throws Exception {
+        RABBIT_MQ_EXTENSION.dockerRabbitMQ().pause();
+        Thread.sleep(1000);
+        try {
+            given()
+                .pathParam("componentName", "MailReceptionCheck")
+            .when()
+                .get("/healthcheck/checks/{componentName}").prettyPeek()

Review comment:
       debug spotted

##########
File path: server/protocols/webadmin-integration-test/distributed-webadmin-integration-test/src/test/java/org/apache/james/webadmin/integration/rabbitmq/MailReceptionCheckIntegrationTest.java
##########
@@ -0,0 +1,136 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.webadmin.integration.rabbitmq;
+
+import static io.restassured.RestAssured.given;
+import static org.apache.james.jmap.JMAPTestingConstants.ALICE;
+import static org.apache.james.jmap.JMAPTestingConstants.ALICE_PASSWORD;
+import static org.apache.james.jmap.JMAPTestingConstants.DOMAIN;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+
+import java.time.Duration;
+import java.util.Optional;
+
+import org.apache.james.CassandraExtension;
+import org.apache.james.CassandraRabbitMQJamesConfiguration;
+import org.apache.james.CassandraRabbitMQJamesServerMain;
+import org.apache.james.DockerElasticSearchExtension;
+import org.apache.james.GuiceJamesServer;
+import org.apache.james.JamesServerBuilder;
+import org.apache.james.JamesServerExtension;
+import org.apache.james.SearchConfiguration;
+import org.apache.james.core.healthcheck.ResultStatus;
+import org.apache.james.events.RetryBackoffConfiguration;
+import org.apache.james.healthcheck.MailReceptionCheck;
+import org.apache.james.junit.categories.BasicFeature;
+import org.apache.james.modules.AwsS3BlobStoreExtension;
+import org.apache.james.modules.RabbitMQExtension;
+import org.apache.james.modules.blobstore.BlobStoreConfiguration;
+import org.apache.james.utils.DataProbeImpl;
+import org.apache.james.utils.WebAdminGuiceProbe;
+import org.apache.james.webadmin.WebAdminUtils;
+import org.apache.james.webadmin.integration.WebadminIntegrationTestModule;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import io.restassured.RestAssured;
+
+@Tag(BasicFeature.TAG)
+class MailReceptionCheckIntegrationTest {
+    private static final RabbitMQExtension RABBIT_MQ_EXTENSION = new RabbitMQExtension();
+    public static final CassandraExtension CASSANDRA_EXTENSION = new CassandraExtension();
+
+    @RegisterExtension
+    static JamesServerExtension testExtension = new JamesServerBuilder<CassandraRabbitMQJamesConfiguration>(tmpDir ->
+        CassandraRabbitMQJamesConfiguration.builder()
+            .workingDirectory(tmpDir)
+            .configurationFromClasspath()
+            .blobStore(BlobStoreConfiguration.builder()
+                    .s3()
+                    .disableCache()
+                    .deduplication()
+                    .noCryptoConfig())
+            .searchConfiguration(SearchConfiguration.elasticSearch())
+            .build())
+        .extension(new DockerElasticSearchExtension())
+        .extension(CASSANDRA_EXTENSION)
+        .extension(new AwsS3BlobStoreExtension())
+        .extension(RABBIT_MQ_EXTENSION)
+        .server(configuration -> CassandraRabbitMQJamesServerMain.createServer(configuration)
+            .overrideWith(new WebadminIntegrationTestModule())
+            .overrideWith(binder -> binder.bind(MailReceptionCheck.Configuration.class)
+                .toInstance(new MailReceptionCheck.Configuration(
+                    Optional.of(ALICE), Duration.ofSeconds(10))))
+            // Enforce a single eventBus retry. Required as Current Quotas are handled by the eventBus.
+            .overrideWith(binder -> binder.bind(RetryBackoffConfiguration.class)
+                .toInstance(RetryBackoffConfiguration.builder()
+                    .maxRetries(1)
+                    .firstBackoff(Duration.ofMillis(2))
+                    .jitterFactor(0.5)
+                    .build())))
+        .build();
+
+    @BeforeEach
+    void setUp(GuiceJamesServer guiceJamesServer) throws Exception {
+        guiceJamesServer.getProbe(DataProbeImpl.class)
+            .fluent()
+            .addDomain(DOMAIN)
+            .addUser(ALICE.asString(), ALICE_PASSWORD);
+
+        WebAdminGuiceProbe webAdminGuiceProbe = guiceJamesServer.getProbe(WebAdminGuiceProbe.class);
+        RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecification(webAdminGuiceProbe.getWebAdminPort())
+            .build();
+    }
+
+    @Test
+    void shouldBeHealthy() {
+        given()
+            .pathParam("componentName", "MailReceptionCheck")
+        .when()
+            .get("/healthcheck/checks/{componentName}").prettyPeek()

Review comment:
       debug spotted




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa merged pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
chibenwa merged pull request #519:
URL: https://github.com/apache/james-project/pull/519


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa commented on a change in pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
chibenwa commented on a change in pull request #519:
URL: https://github.com/apache/james-project/pull/519#discussion_r663626037



##########
File path: server/container/feature-checks/src/main/java/org/apache/james/healthcheck/MailReceptionCheck.java
##########
@@ -0,0 +1,270 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.healthcheck;
+
+import static reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.core.Username;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.events.Event;
+import org.apache.james.events.EventBus;
+import org.apache.james.events.EventListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.events.MailboxEvents.Added;
+import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.FetchGroup;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.server.core.MailImpl;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.MailetContext;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+import reactor.core.scheduler.Schedulers;
+
+public class MailReceptionCheck implements HealthCheck {
+    public static class Configuration {
+        private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
+        public static final Configuration DEFAULT = new Configuration(Optional.empty(), DEFAULT_TIMEOUT);
+
+        public static Configuration from(org.apache.commons.configuration2.Configuration configuration) {
+            Optional<Username> username = Optional.ofNullable(configuration.getString("reception.check.user", null))
+                .map(Username::of);
+            Duration timeout = Optional.ofNullable(configuration.getString("reception.check.timeout", null))
+                .map(s -> DurationParser.parse(s, ChronoUnit.SECONDS))
+                .orElse(DEFAULT_TIMEOUT);
+
+            return new Configuration(username, timeout);
+        }
+
+        private final Optional<Username> testUser;
+        private final Duration timeout;
+
+        public Configuration(Optional<Username> testUser, Duration timeout) {
+            this.testUser = testUser;
+            this.timeout = timeout;
+        }
+
+        public Optional<Username> getTestUser() {
+            return testUser;
+        }
+
+        public Duration getTimeout() {
+            return timeout;
+        }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof Configuration) {
+                Configuration that = (Configuration) o;
+                return Objects.equals(testUser, that.testUser)
+                    && Objects.equals(timeout, that.timeout);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(testUser, timeout);
+        }
+
+        @Override
+        public String toString() {
+            return MoreObjects.toStringHelper(this)
+                .add("testUser", testUser)
+                .add("timeout", timeout)
+                .toString();
+        }
+    }
+
+    public static class Content {
+        public static Content generate() {
+            return new Content(UUID.randomUUID());
+        }
+
+        private final UUID uuid;
+
+        private Content(UUID uuid) {
+            this.uuid = uuid;
+        }
+
+        public String asString() {
+            return uuid.toString();
+        }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof Content) {
+                Content that = (Content) o;
+                return Objects.equals(uuid, that.uuid);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(uuid);
+        }
+
+        @Override
+        public String toString() {
+            return asString();
+        }
+    }
+
+    public static class AwaitReceptionListener implements EventListener.ReactiveEventListener {
+        private final Sinks.Many<Added> sink;
+
+        public AwaitReceptionListener() {
+            sink = Sinks.many().multicast().onBackpressureBuffer();
+        }
+
+        @Override
+        public Publisher<Void> reactiveEvent(Event event) {
+            if (event instanceof Added) {
+                return Mono.fromRunnable(() -> sink.emitNext((Added) event, FAIL_FAST))
+                    .subscribeOn(Schedulers.elastic())
+                    .then();
+            }
+            return Mono.empty();
+        }
+
+        public Flux<Added> addedEvents() {
+            return sink.asFlux();
+        }
+    }
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MailReceptionCheck.class);
+
+    private final MailetContext mailetContext;
+    private final MailboxManager mailboxManager;
+    private final EventBus eventBus;
+    private final UsersRepository usersRepository;
+    private final Configuration configuration;
+
+    @Inject
+    public MailReceptionCheck(MailetContext mailetContext, MailboxManager mailboxManager, EventBus eventBus, UsersRepository usersRepository, Configuration configuration) {
+        this.mailetContext = mailetContext;
+        this.mailboxManager = mailboxManager;
+        this.eventBus = eventBus;
+        this.usersRepository = usersRepository;
+        this.configuration = configuration;
+    }
+
+    @Override
+    public ComponentName componentName() {
+        return new ComponentName("MailReceptionCheck");
+    }
+
+    @Override
+    public Publisher<Result> check() {
+        return configuration.getTestUser()
+            .map(this::check)
+            .orElse(Mono.just(Result.healthy(componentName())));
+    }
+
+    private Mono<Result> check(Username username) {
+        MailboxSession session = mailboxManager.createSystemSession(username);
+        AwaitReceptionListener listener = new AwaitReceptionListener();
+
+        return retrieveInbox(username, session)
+            .flatMap(mailbox -> Mono.usingWhen(
+                Mono.from(eventBus.register(listener, new MailboxIdRegistrationKey(mailbox.getId()))),
+                registration -> sendMail(username)
+                    .flatMap(content -> checkReceived(session, listener, mailbox, content)),
+                registration -> Mono.fromRunnable(registration::unregister)))
+            .subscribeOn(Schedulers.elastic())
+            .timeout(configuration.getTimeout(), Mono.error(() -> new RuntimeException("HealthCheck email was not received after " + configuration.getTimeout().toMillis() + "ms")))
+            .onErrorResume(e -> {
+                LOGGER.error("Mail reception check failed", e);
+                return Mono.just(Result.unhealthy(componentName(), e.getMessage()));
+            });
+    }
+
+    private Mono<MessageManager> retrieveInbox(Username username, MailboxSession session) {
+        MailboxPath mailboxPath = MailboxPath.inbox(username);
+        return Mono.from(mailboxManager.getMailboxReactive(mailboxPath, session))
+            .onErrorResume(MailboxNotFoundException.class, e -> Mono.fromCallable(() -> mailboxManager.createMailbox(mailboxPath, session))
+                .then(Mono.from(mailboxManager.getMailboxReactive(mailboxPath, session))));
+    }
+
+    private Mono<Result> checkReceived(MailboxSession session, AwaitReceptionListener listener, MessageManager mailbox, Content content) {
+        return listener.addedEvents()
+            .flatMapIterable(Added::getUids)
+            .flatMap(uid -> Mono.fromCallable(() -> mailbox.getMessages(MessageRange.one(uid), FetchGroup.FULL_CONTENT, session)))
+            .flatMapIterable(ImmutableList::copyOf)
+            .filter(Throwing.predicate(messageResult -> IOUtils.toString(messageResult.getBody().getInputStream()).contains(content.asString())))
+            // Cleanup our testing mail
+            .doOnNext(messageResult -> {
+                try {
+                    mailbox.delete(ImmutableList.of(messageResult.getUid()), session);
+                } catch (MailboxException e) {
+                    LOGGER.warn("Faield to delete Health check testing email", e);
+                }
+            })
+            .map(any -> Result.healthy(componentName()))
+            .next();
+    }
+
+    private Mono<Content> sendMail(Username username) {
+        Content content = Content.generate();
+
+        return Mono.fromCallable(() -> usersRepository.getMailAddressFor(username))
+            .flatMap(address -> Mono.fromRunnable(Throwing.runnable(() ->
+                mailetContext.sendMail(MailImpl.builder()
+                    .name(content.asString())
+                    .sender(address)
+                    .addRecipient(address)
+                    .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+                        .addFrom(new InternetAddress(address.asString()))
+                        .addToRecipient(address.asString())
+                        .setSubject(content.asString())

Review comment:
       What do you mean?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] Arsnael commented on a change in pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
Arsnael commented on a change in pull request #519:
URL: https://github.com/apache/james-project/pull/519#discussion_r663628533



##########
File path: server/container/feature-checks/src/main/java/org/apache/james/healthcheck/MailReceptionCheck.java
##########
@@ -0,0 +1,270 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.healthcheck;
+
+import static reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.core.Username;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.events.Event;
+import org.apache.james.events.EventBus;
+import org.apache.james.events.EventListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.events.MailboxEvents.Added;
+import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.FetchGroup;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.server.core.MailImpl;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.MailetContext;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+import reactor.core.scheduler.Schedulers;
+
+public class MailReceptionCheck implements HealthCheck {
+    public static class Configuration {
+        private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
+        public static final Configuration DEFAULT = new Configuration(Optional.empty(), DEFAULT_TIMEOUT);
+
+        public static Configuration from(org.apache.commons.configuration2.Configuration configuration) {
+            Optional<Username> username = Optional.ofNullable(configuration.getString("reception.check.user", null))
+                .map(Username::of);
+            Duration timeout = Optional.ofNullable(configuration.getString("reception.check.timeout", null))
+                .map(s -> DurationParser.parse(s, ChronoUnit.SECONDS))
+                .orElse(DEFAULT_TIMEOUT);
+
+            return new Configuration(username, timeout);
+        }
+
+        private final Optional<Username> testUser;

Review comment:
       Well I have no issue with that terminology. We use a test user to check that our mailing process works
   
   I would guess `reception.check.user` is more related to the name of the class `MailReceptionCheck`? And we have an other param using this prefix as well. I really don't see anything wrong here :)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa commented on pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
chibenwa commented on pull request #519:
URL: https://github.com/apache/james-project/pull/519#issuecomment-874399258


   Just squashed


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa commented on a change in pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
chibenwa commented on a change in pull request #519:
URL: https://github.com/apache/james-project/pull/519#discussion_r663627967



##########
File path: server/container/feature-checks/src/main/java/org/apache/james/healthcheck/MailReceptionCheck.java
##########
@@ -0,0 +1,270 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.healthcheck;
+
+import static reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.core.Username;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.events.Event;
+import org.apache.james.events.EventBus;
+import org.apache.james.events.EventListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.events.MailboxEvents.Added;
+import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.FetchGroup;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.server.core.MailImpl;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.MailetContext;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+import reactor.core.scheduler.Schedulers;
+
+public class MailReceptionCheck implements HealthCheck {
+    public static class Configuration {
+        private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
+        public static final Configuration DEFAULT = new Configuration(Optional.empty(), DEFAULT_TIMEOUT);
+
+        public static Configuration from(org.apache.commons.configuration2.Configuration configuration) {
+            Optional<Username> username = Optional.ofNullable(configuration.getString("reception.check.user", null))
+                .map(Username::of);
+            Duration timeout = Optional.ofNullable(configuration.getString("reception.check.timeout", null))
+                .map(s -> DurationParser.parse(s, ChronoUnit.SECONDS))
+                .orElse(DEFAULT_TIMEOUT);
+
+            return new Configuration(username, timeout);
+        }
+
+        private final Optional<Username> testUser;

Review comment:
       Ok for check user if it please you then.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] chibenwa commented on a change in pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
chibenwa commented on a change in pull request #519:
URL: https://github.com/apache/james-project/pull/519#discussion_r663625832



##########
File path: server/container/feature-checks/src/main/java/org/apache/james/healthcheck/MailReceptionCheck.java
##########
@@ -0,0 +1,270 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.healthcheck;
+
+import static reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.core.Username;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.events.Event;
+import org.apache.james.events.EventBus;
+import org.apache.james.events.EventListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.events.MailboxEvents.Added;
+import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.FetchGroup;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.server.core.MailImpl;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.MailetContext;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+import reactor.core.scheduler.Schedulers;
+
+public class MailReceptionCheck implements HealthCheck {
+    public static class Configuration {
+        private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
+        public static final Configuration DEFAULT = new Configuration(Optional.empty(), DEFAULT_TIMEOUT);
+
+        public static Configuration from(org.apache.commons.configuration2.Configuration configuration) {
+            Optional<Username> username = Optional.ofNullable(configuration.getString("reception.check.user", null))
+                .map(Username::of);
+            Duration timeout = Optional.ofNullable(configuration.getString("reception.check.timeout", null))
+                .map(s -> DurationParser.parse(s, ChronoUnit.SECONDS))
+                .orElse(DEFAULT_TIMEOUT);
+
+            return new Configuration(username, timeout);
+        }
+
+        private final Optional<Username> testUser;

Review comment:
       Mmm maybe other opinions here cc @Arsnael ? 
   
   Check vs test... In this case I prefer the 'test' terminology...




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] vttranlina commented on a change in pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
vttranlina commented on a change in pull request #519:
URL: https://github.com/apache/james-project/pull/519#discussion_r663626799



##########
File path: server/container/feature-checks/src/main/java/org/apache/james/healthcheck/MailReceptionCheck.java
##########
@@ -0,0 +1,270 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.healthcheck;
+
+import static reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.core.Username;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.events.Event;
+import org.apache.james.events.EventBus;
+import org.apache.james.events.EventListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.events.MailboxEvents.Added;
+import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.FetchGroup;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.server.core.MailImpl;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.MailetContext;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+import reactor.core.scheduler.Schedulers;
+
+public class MailReceptionCheck implements HealthCheck {
+    public static class Configuration {
+        private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
+        public static final Configuration DEFAULT = new Configuration(Optional.empty(), DEFAULT_TIMEOUT);
+
+        public static Configuration from(org.apache.commons.configuration2.Configuration configuration) {
+            Optional<Username> username = Optional.ofNullable(configuration.getString("reception.check.user", null))
+                .map(Username::of);
+            Duration timeout = Optional.ofNullable(configuration.getString("reception.check.timeout", null))
+                .map(s -> DurationParser.parse(s, ChronoUnit.SECONDS))
+                .orElse(DEFAULT_TIMEOUT);
+
+            return new Configuration(username, timeout);
+        }
+
+        private final Optional<Username> testUser;

Review comment:
       If `test` is used, we should change `reception.check.user` -> `reception.test.user`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org


[GitHub] [james-project] vttranlina commented on a change in pull request #519: JAMES-3607 Functional healthcheck exercising mail reception

Posted by GitBox <gi...@apache.org>.
vttranlina commented on a change in pull request #519:
URL: https://github.com/apache/james-project/pull/519#discussion_r663614199



##########
File path: server/container/feature-checks/src/main/java/org/apache/james/healthcheck/MailReceptionCheck.java
##########
@@ -0,0 +1,270 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.healthcheck;
+
+import static reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.core.Username;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.events.Event;
+import org.apache.james.events.EventBus;
+import org.apache.james.events.EventListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.events.MailboxEvents.Added;
+import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.FetchGroup;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.server.core.MailImpl;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.MailetContext;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+import reactor.core.scheduler.Schedulers;
+
+public class MailReceptionCheck implements HealthCheck {
+    public static class Configuration {
+        private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
+        public static final Configuration DEFAULT = new Configuration(Optional.empty(), DEFAULT_TIMEOUT);
+
+        public static Configuration from(org.apache.commons.configuration2.Configuration configuration) {
+            Optional<Username> username = Optional.ofNullable(configuration.getString("reception.check.user", null))
+                .map(Username::of);
+            Duration timeout = Optional.ofNullable(configuration.getString("reception.check.timeout", null))
+                .map(s -> DurationParser.parse(s, ChronoUnit.SECONDS))
+                .orElse(DEFAULT_TIMEOUT);
+
+            return new Configuration(username, timeout);
+        }
+
+        private final Optional<Username> testUser;

Review comment:
       I think `checkUser` is better (same name with property in the config file)

##########
File path: server/container/feature-checks/src/main/java/org/apache/james/healthcheck/MailReceptionCheck.java
##########
@@ -0,0 +1,270 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.healthcheck;
+
+import static reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.core.Username;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.events.Event;
+import org.apache.james.events.EventBus;
+import org.apache.james.events.EventListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.events.MailboxEvents.Added;
+import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.FetchGroup;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.server.core.MailImpl;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.MailetContext;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+import reactor.core.scheduler.Schedulers;
+
+public class MailReceptionCheck implements HealthCheck {
+    public static class Configuration {
+        private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
+        public static final Configuration DEFAULT = new Configuration(Optional.empty(), DEFAULT_TIMEOUT);
+
+        public static Configuration from(org.apache.commons.configuration2.Configuration configuration) {
+            Optional<Username> username = Optional.ofNullable(configuration.getString("reception.check.user", null))
+                .map(Username::of);
+            Duration timeout = Optional.ofNullable(configuration.getString("reception.check.timeout", null))
+                .map(s -> DurationParser.parse(s, ChronoUnit.SECONDS))
+                .orElse(DEFAULT_TIMEOUT);
+
+            return new Configuration(username, timeout);
+        }
+
+        private final Optional<Username> testUser;
+        private final Duration timeout;
+
+        public Configuration(Optional<Username> testUser, Duration timeout) {
+            this.testUser = testUser;
+            this.timeout = timeout;
+        }
+
+        public Optional<Username> getTestUser() {
+            return testUser;
+        }
+
+        public Duration getTimeout() {
+            return timeout;
+        }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof Configuration) {
+                Configuration that = (Configuration) o;
+                return Objects.equals(testUser, that.testUser)
+                    && Objects.equals(timeout, that.timeout);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(testUser, timeout);
+        }
+
+        @Override
+        public String toString() {
+            return MoreObjects.toStringHelper(this)
+                .add("testUser", testUser)
+                .add("timeout", timeout)
+                .toString();
+        }
+    }
+
+    public static class Content {
+        public static Content generate() {
+            return new Content(UUID.randomUUID());
+        }
+
+        private final UUID uuid;
+
+        private Content(UUID uuid) {
+            this.uuid = uuid;
+        }
+
+        public String asString() {
+            return uuid.toString();
+        }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof Content) {
+                Content that = (Content) o;
+                return Objects.equals(uuid, that.uuid);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(uuid);
+        }
+
+        @Override
+        public String toString() {
+            return asString();
+        }
+    }
+
+    public static class AwaitReceptionListener implements EventListener.ReactiveEventListener {
+        private final Sinks.Many<Added> sink;
+
+        public AwaitReceptionListener() {
+            sink = Sinks.many().multicast().onBackpressureBuffer();
+        }
+
+        @Override
+        public Publisher<Void> reactiveEvent(Event event) {
+            if (event instanceof Added) {
+                return Mono.fromRunnable(() -> sink.emitNext((Added) event, FAIL_FAST))
+                    .subscribeOn(Schedulers.elastic())
+                    .then();
+            }
+            return Mono.empty();
+        }
+
+        public Flux<Added> addedEvents() {
+            return sink.asFlux();
+        }
+    }
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MailReceptionCheck.class);
+
+    private final MailetContext mailetContext;
+    private final MailboxManager mailboxManager;
+    private final EventBus eventBus;
+    private final UsersRepository usersRepository;
+    private final Configuration configuration;
+
+    @Inject
+    public MailReceptionCheck(MailetContext mailetContext, MailboxManager mailboxManager, EventBus eventBus, UsersRepository usersRepository, Configuration configuration) {
+        this.mailetContext = mailetContext;
+        this.mailboxManager = mailboxManager;
+        this.eventBus = eventBus;
+        this.usersRepository = usersRepository;
+        this.configuration = configuration;
+    }
+
+    @Override
+    public ComponentName componentName() {
+        return new ComponentName("MailReceptionCheck");
+    }
+
+    @Override
+    public Publisher<Result> check() {
+        return configuration.getTestUser()
+            .map(this::check)
+            .orElse(Mono.just(Result.healthy(componentName())));
+    }
+
+    private Mono<Result> check(Username username) {
+        MailboxSession session = mailboxManager.createSystemSession(username);
+        AwaitReceptionListener listener = new AwaitReceptionListener();
+
+        return retrieveInbox(username, session)
+            .flatMap(mailbox -> Mono.usingWhen(
+                Mono.from(eventBus.register(listener, new MailboxIdRegistrationKey(mailbox.getId()))),
+                registration -> sendMail(username)
+                    .flatMap(content -> checkReceived(session, listener, mailbox, content)),
+                registration -> Mono.fromRunnable(registration::unregister)))
+            .subscribeOn(Schedulers.elastic())
+            .timeout(configuration.getTimeout(), Mono.error(() -> new RuntimeException("HealthCheck email was not received after " + configuration.getTimeout().toMillis() + "ms")))
+            .onErrorResume(e -> {
+                LOGGER.error("Mail reception check failed", e);
+                return Mono.just(Result.unhealthy(componentName(), e.getMessage()));
+            });
+    }
+
+    private Mono<MessageManager> retrieveInbox(Username username, MailboxSession session) {
+        MailboxPath mailboxPath = MailboxPath.inbox(username);
+        return Mono.from(mailboxManager.getMailboxReactive(mailboxPath, session))
+            .onErrorResume(MailboxNotFoundException.class, e -> Mono.fromCallable(() -> mailboxManager.createMailbox(mailboxPath, session))
+                .then(Mono.from(mailboxManager.getMailboxReactive(mailboxPath, session))));
+    }
+
+    private Mono<Result> checkReceived(MailboxSession session, AwaitReceptionListener listener, MessageManager mailbox, Content content) {
+        return listener.addedEvents()
+            .flatMapIterable(Added::getUids)
+            .flatMap(uid -> Mono.fromCallable(() -> mailbox.getMessages(MessageRange.one(uid), FetchGroup.FULL_CONTENT, session)))
+            .flatMapIterable(ImmutableList::copyOf)
+            .filter(Throwing.predicate(messageResult -> IOUtils.toString(messageResult.getBody().getInputStream()).contains(content.asString())))
+            // Cleanup our testing mail
+            .doOnNext(messageResult -> {
+                try {
+                    mailbox.delete(ImmutableList.of(messageResult.getUid()), session);
+                } catch (MailboxException e) {
+                    LOGGER.warn("Faield to delete Health check testing email", e);

Review comment:
       `Failed`

##########
File path: server/container/feature-checks/src/main/java/org/apache/james/healthcheck/MailReceptionCheck.java
##########
@@ -0,0 +1,270 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.healthcheck;
+
+import static reactor.core.publisher.Sinks.EmitFailureHandler.FAIL_FAST;
+
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.core.Username;
+import org.apache.james.core.builder.MimeMessageBuilder;
+import org.apache.james.core.healthcheck.ComponentName;
+import org.apache.james.core.healthcheck.HealthCheck;
+import org.apache.james.core.healthcheck.Result;
+import org.apache.james.events.Event;
+import org.apache.james.events.EventBus;
+import org.apache.james.events.EventListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.events.MailboxEvents.Added;
+import org.apache.james.mailbox.events.MailboxIdRegistrationKey;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.exception.MailboxNotFoundException;
+import org.apache.james.mailbox.model.FetchGroup;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.server.core.MailImpl;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.util.DurationParser;
+import org.apache.mailet.MailetContext;
+import org.reactivestreams.Publisher;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.github.fge.lambdas.Throwing;
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.publisher.Sinks;
+import reactor.core.scheduler.Schedulers;
+
+public class MailReceptionCheck implements HealthCheck {
+    public static class Configuration {
+        private static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(1);
+        public static final Configuration DEFAULT = new Configuration(Optional.empty(), DEFAULT_TIMEOUT);
+
+        public static Configuration from(org.apache.commons.configuration2.Configuration configuration) {
+            Optional<Username> username = Optional.ofNullable(configuration.getString("reception.check.user", null))
+                .map(Username::of);
+            Duration timeout = Optional.ofNullable(configuration.getString("reception.check.timeout", null))
+                .map(s -> DurationParser.parse(s, ChronoUnit.SECONDS))
+                .orElse(DEFAULT_TIMEOUT);
+
+            return new Configuration(username, timeout);
+        }
+
+        private final Optional<Username> testUser;
+        private final Duration timeout;
+
+        public Configuration(Optional<Username> testUser, Duration timeout) {
+            this.testUser = testUser;
+            this.timeout = timeout;
+        }
+
+        public Optional<Username> getTestUser() {
+            return testUser;
+        }
+
+        public Duration getTimeout() {
+            return timeout;
+        }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof Configuration) {
+                Configuration that = (Configuration) o;
+                return Objects.equals(testUser, that.testUser)
+                    && Objects.equals(timeout, that.timeout);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(testUser, timeout);
+        }
+
+        @Override
+        public String toString() {
+            return MoreObjects.toStringHelper(this)
+                .add("testUser", testUser)
+                .add("timeout", timeout)
+                .toString();
+        }
+    }
+
+    public static class Content {
+        public static Content generate() {
+            return new Content(UUID.randomUUID());
+        }
+
+        private final UUID uuid;
+
+        private Content(UUID uuid) {
+            this.uuid = uuid;
+        }
+
+        public String asString() {
+            return uuid.toString();
+        }
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof Content) {
+                Content that = (Content) o;
+                return Objects.equals(uuid, that.uuid);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(uuid);
+        }
+
+        @Override
+        public String toString() {
+            return asString();
+        }
+    }
+
+    public static class AwaitReceptionListener implements EventListener.ReactiveEventListener {
+        private final Sinks.Many<Added> sink;
+
+        public AwaitReceptionListener() {
+            sink = Sinks.many().multicast().onBackpressureBuffer();
+        }
+
+        @Override
+        public Publisher<Void> reactiveEvent(Event event) {
+            if (event instanceof Added) {
+                return Mono.fromRunnable(() -> sink.emitNext((Added) event, FAIL_FAST))
+                    .subscribeOn(Schedulers.elastic())
+                    .then();
+            }
+            return Mono.empty();
+        }
+
+        public Flux<Added> addedEvents() {
+            return sink.asFlux();
+        }
+    }
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MailReceptionCheck.class);
+
+    private final MailetContext mailetContext;
+    private final MailboxManager mailboxManager;
+    private final EventBus eventBus;
+    private final UsersRepository usersRepository;
+    private final Configuration configuration;
+
+    @Inject
+    public MailReceptionCheck(MailetContext mailetContext, MailboxManager mailboxManager, EventBus eventBus, UsersRepository usersRepository, Configuration configuration) {
+        this.mailetContext = mailetContext;
+        this.mailboxManager = mailboxManager;
+        this.eventBus = eventBus;
+        this.usersRepository = usersRepository;
+        this.configuration = configuration;
+    }
+
+    @Override
+    public ComponentName componentName() {
+        return new ComponentName("MailReceptionCheck");
+    }
+
+    @Override
+    public Publisher<Result> check() {
+        return configuration.getTestUser()
+            .map(this::check)
+            .orElse(Mono.just(Result.healthy(componentName())));
+    }
+
+    private Mono<Result> check(Username username) {
+        MailboxSession session = mailboxManager.createSystemSession(username);
+        AwaitReceptionListener listener = new AwaitReceptionListener();
+
+        return retrieveInbox(username, session)
+            .flatMap(mailbox -> Mono.usingWhen(
+                Mono.from(eventBus.register(listener, new MailboxIdRegistrationKey(mailbox.getId()))),
+                registration -> sendMail(username)
+                    .flatMap(content -> checkReceived(session, listener, mailbox, content)),
+                registration -> Mono.fromRunnable(registration::unregister)))
+            .subscribeOn(Schedulers.elastic())
+            .timeout(configuration.getTimeout(), Mono.error(() -> new RuntimeException("HealthCheck email was not received after " + configuration.getTimeout().toMillis() + "ms")))
+            .onErrorResume(e -> {
+                LOGGER.error("Mail reception check failed", e);
+                return Mono.just(Result.unhealthy(componentName(), e.getMessage()));
+            });
+    }
+
+    private Mono<MessageManager> retrieveInbox(Username username, MailboxSession session) {
+        MailboxPath mailboxPath = MailboxPath.inbox(username);
+        return Mono.from(mailboxManager.getMailboxReactive(mailboxPath, session))
+            .onErrorResume(MailboxNotFoundException.class, e -> Mono.fromCallable(() -> mailboxManager.createMailbox(mailboxPath, session))
+                .then(Mono.from(mailboxManager.getMailboxReactive(mailboxPath, session))));
+    }
+
+    private Mono<Result> checkReceived(MailboxSession session, AwaitReceptionListener listener, MessageManager mailbox, Content content) {
+        return listener.addedEvents()
+            .flatMapIterable(Added::getUids)
+            .flatMap(uid -> Mono.fromCallable(() -> mailbox.getMessages(MessageRange.one(uid), FetchGroup.FULL_CONTENT, session)))
+            .flatMapIterable(ImmutableList::copyOf)
+            .filter(Throwing.predicate(messageResult -> IOUtils.toString(messageResult.getBody().getInputStream()).contains(content.asString())))
+            // Cleanup our testing mail
+            .doOnNext(messageResult -> {
+                try {
+                    mailbox.delete(ImmutableList.of(messageResult.getUid()), session);
+                } catch (MailboxException e) {
+                    LOGGER.warn("Faield to delete Health check testing email", e);
+                }
+            })
+            .map(any -> Result.healthy(componentName()))
+            .next();
+    }
+
+    private Mono<Content> sendMail(Username username) {
+        Content content = Content.generate();
+
+        return Mono.fromCallable(() -> usersRepository.getMailAddressFor(username))
+            .flatMap(address -> Mono.fromRunnable(Throwing.runnable(() ->
+                mailetContext.sendMail(MailImpl.builder()
+                    .name(content.asString())
+                    .sender(address)
+                    .addRecipient(address)
+                    .mimeMessage(MimeMessageBuilder.mimeMessageBuilder()
+                        .addFrom(new InternetAddress(address.asString()))
+                        .addToRecipient(address.asString())
+                        .setSubject(content.asString())

Review comment:
       TODO here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org