You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/01/25 02:28:36 UTC

[james-project] branch master updated: JAMES-3700 Define basic expectations for Pulsar MailQueue dead letter policy (#836)

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


The following commit(s) were added to refs/heads/master by this push:
     new 9ab60f9  JAMES-3700 Define basic expectations for Pulsar MailQueue dead letter policy (#836)
9ab60f9 is described below

commit 9ab60f951534c7f01c1c9b80e75f0033543e1df6
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Tue Jan 25 09:28:26 2022 +0700

    JAMES-3700 Define basic expectations for Pulsar MailQueue dead letter policy (#836)
---
 .../james/queue/pulsar/PulsarMailQueueTest.java    | 45 ++++++++++++++++++++--
 .../org/apache/james/queue/pulsar/JavaClient.scala | 43 +++++++++++++++++++++
 2 files changed, 85 insertions(+), 3 deletions(-)

diff --git a/server/queue/queue-pulsar/src/test/java/org/apache/james/queue/pulsar/PulsarMailQueueTest.java b/server/queue/queue-pulsar/src/test/java/org/apache/james/queue/pulsar/PulsarMailQueueTest.java
index c8ed4e1..8c00d1c 100644
--- a/server/queue/queue-pulsar/src/test/java/org/apache/james/queue/pulsar/PulsarMailQueueTest.java
+++ b/server/queue/queue-pulsar/src/test/java/org/apache/james/queue/pulsar/PulsarMailQueueTest.java
@@ -22,10 +22,9 @@ package org.apache.james.queue.pulsar;
 import static org.apache.james.queue.api.Mails.defaultMail;
 import static org.assertj.core.api.Assertions.assertThat;
 
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
-import java.util.concurrent.TimeUnit;
+import java.util.Optional;
 import java.util.stream.IntStream;
 
 import javax.mail.MessagingException;
@@ -64,10 +63,12 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.github.fge.lambdas.Throwing;
+import com.sksamuel.pulsar4s.ConsumerMessage;
 
 import akka.actor.ActorSystem;
 import reactor.core.publisher.Flux;
-import reactor.core.scheduler.Schedulers;
+import reactor.core.publisher.Mono;
+import scala.jdk.javaapi.OptionConverters;
 
 @ExtendWith(DockerPulsarExtension.class)
 public class PulsarMailQueueTest implements MailQueueContract, MailQueueMetricContract, ManageableMailQueueContract, DelayedMailQueueContract, DelayedManageableMailQueueContract {
@@ -138,6 +139,44 @@ public class PulsarMailQueueTest implements MailQueueContract, MailQueueMetricCo
                 system);
     }
 
+    @Disabled("JAMES-3700 We need to define a deadletter policy for the Pulsar MailQueue")
+    @Test
+    void badMessagesShouldNotAlterDelivery(DockerPulsarExtension.DockerPulsar pulsar) throws Exception {
+        new JavaClient(pulsar.getConfiguration().brokerUri(),
+            String.format("persistent://%s/James-%s", pulsar.getConfiguration().namespace().asString(), mailQueueName.asString()))
+            .send("BAD").get();
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name")
+            .build());
+
+        MailQueue.MailQueueItem mail = Flux.from(getMailQueue().deQueue()).onErrorResume(e -> Mono.empty()).take(1).single().block();
+        assertThat(mail.getMail().getName()).isEqualTo("name");
+    }
+
+    @Disabled("JAMES-3700 We need to define a deadletter policy for the Pulsar MailQueue")
+    @Test
+    void badMessagesShouldBeMovedToADeadLetterTopic(DockerPulsarExtension.DockerPulsar pulsar) throws Exception {
+        new JavaClient(pulsar.getConfiguration().brokerUri(),
+            String.format("persistent://%s/James-%s", pulsar.getConfiguration().namespace().asString(), mailQueueName.asString()))
+            .send("BAD").get();
+
+        getMailQueue().enQueue(defaultMail()
+            .name("name")
+            .build());
+
+        try {
+            Flux.from(getMailQueue().deQueue()).take(1).single().block();
+        } catch (Exception e) {
+            // Expected to fail
+        }
+        Optional<String> deadletterMessage = OptionConverters.toJava(new JavaClient(pulsar.getConfiguration().brokerUri(),
+            String.format("persistent://%s/James-%s/dead-letter", pulsar.getConfiguration().namespace().asString(), mailQueueName.asString()))
+            .consumeOne())
+            .map(ConsumerMessage::value);
+        assertThat(deadletterMessage).contains("BAD");
+    }
+
     @Test
     void ensureThatDeletionDoNotDeleteFutureEmailsWithTwoInstancesOfMailQueue(DockerPulsarExtension.DockerPulsar pulsar) throws MessagingException, InterruptedException {
         PulsarMailQueue secondQueue = newInstance(pulsar);
diff --git a/server/queue/queue-pulsar/src/test/scala/org/apache/james/queue/pulsar/JavaClient.scala b/server/queue/queue-pulsar/src/test/scala/org/apache/james/queue/pulsar/JavaClient.scala
new file mode 100644
index 0000000..962e9ae
--- /dev/null
+++ b/server/queue/queue-pulsar/src/test/scala/org/apache/james/queue/pulsar/JavaClient.scala
@@ -0,0 +1,43 @@
+/** **************************************************************
+ * 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.queue.pulsar
+
+import com.sksamuel.pulsar4s.{Consumer, ConsumerConfig, ConsumerMessage, MessageId, Producer, ProducerConfig, PulsarAsyncClient, PulsarClient, Subscription, Topic}
+import org.apache.pulsar.client.api.{Schema, SubscriptionInitialPosition, SubscriptionType}
+
+import scala.concurrent.duration.{FiniteDuration, SECONDS}
+import scala.util.Try
+
+case class JavaClient(brokerUri: String, topic: String) {
+  private val client: PulsarAsyncClient = PulsarClient(brokerUri)
+  private val producerConfig: ProducerConfig = ProducerConfig(Topic(topic), enableBatching = Some(false))
+  private val producer: Producer[String] = client.producer(producerConfig)(Schema.STRING)
+
+  def send(payload: String): Try[MessageId] = producer.send(payload)
+
+  private val consumer: Consumer[String] =  client.consumer[String](
+    ConsumerConfig(
+      subscriptionName = Subscription("subscription"),
+      topics = Seq(Topic(topic)),
+      subscriptionType = Some(SubscriptionType.Shared),
+      subscriptionInitialPosition = Some(SubscriptionInitialPosition.Earliest)))(Schema.STRING)
+
+  def consumeOne: Option[ConsumerMessage[String]] = consumer.receive(FiniteDuration(1, SECONDS)).toOption.flatten
+}
\ No newline at end of file

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