JAMES-1877 Provide tests for DNS error handling
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/9898e18e Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/9898e18e Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/9898e18e Branch: refs/heads/master Commit: 9898e18e475c7a672ba8e88c8fa4c1504d3c2531 Parents: 70ad233 Author: Benoit Tellier <btell...@linagora.com> Authored: Wed Dec 7 10:53:01 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Tue Jan 10 18:14:27 2017 +0700 ---------------------------------------------------------------------- .../mailets/remoteDelivery/MailDelivrer.java | 33 +++--- .../remoteDelivery/MailDelivrerTest.java | 109 ++++++++++++++++++- 2 files changed, 119 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/9898e18e/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java index e6f1ec3..27f66aa 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrer.java @@ -49,11 +49,16 @@ public class MailDelivrer { private final Logger logger; public MailDelivrer(RemoteDeliveryConfiguration configuration, MailDelivrerToHost mailDelivrerToHost, DNSService dnsServer, Bouncer bouncer, Logger logger) { + this(configuration, mailDelivrerToHost, new DnsHelper(dnsServer, configuration, logger), bouncer, logger); + } + + @VisibleForTesting + MailDelivrer(RemoteDeliveryConfiguration configuration, MailDelivrerToHost mailDelivrerToHost, DnsHelper dnsHelper, Bouncer bouncer, Logger logger) { this.configuration = configuration; this.mailDelivrerToHost = mailDelivrerToHost; - this.bouncer = bouncer; - this.dnsHelper = new DnsHelper(dnsServer, configuration, logger); + this.dnsHelper = dnsHelper; this.messageComposer = new MessageComposer(configuration); + this.bouncer = bouncer; this.logger = logger; } @@ -116,7 +121,8 @@ public class MailDelivrer { } return doDeliver(mail, mail.getMessage(), InternetAddressConverter.convert(mail.getRecipients()), targetServers); } catch (TemporaryResolutionException e) { - return handleTemporaryResolutionException(mail, host); + return logAndReturn(mail, ExecutionResult.temporaryFailure(new MessagingException("Temporary problem looking " + + "up mail server for host: " + host + ". I cannot determine where to send this message."))); } } @@ -252,28 +258,15 @@ public class MailDelivrer { } } - private ExecutionResult handleTemporaryResolutionException(Mail mail, String host) { - ExecutionResult executionResult = ExecutionResult.temporaryFailure(new MessagingException("Temporary problem looking " + - "up mail server for host: " + host + ". I cannot determine where to send this message.")); - logger.debug(messageComposer.composeFailLogMessage(mail, executionResult)); - return executionResult; - } - private ExecutionResult handleNoTargetServer(Mail mail, String host) { logger.info("No mail server found for: " + host); - String exceptionBuffer = "There are no DNS entries for the hostname " + host + ". I cannot determine where to send this message."; - - MessagingException messagingException = new MessagingException(exceptionBuffer); + MessagingException messagingException = new MessagingException("There are no DNS entries for the hostname " + host + ". I cannot determine where to send this message."); int retry = DeliveryRetriesHelper.retrieveRetries(mail); + System.out.println("retyry " + retry); if (retry == 0 || retry > configuration.getDnsProblemRetry()) { - // The domain has no dns entry.. Return a permanent error - ExecutionResult executionResult = ExecutionResult.permanentFailure(messagingException); - logger.debug(messageComposer.composeFailLogMessage(mail, executionResult)); - return executionResult; + return logAndReturn(mail, ExecutionResult.permanentFailure(messagingException)); } else { - ExecutionResult executionResult = ExecutionResult.temporaryFailure(messagingException); - logger.debug(messageComposer.composeFailLogMessage(mail, executionResult)); - return executionResult; + return logAndReturn(mail, ExecutionResult.temporaryFailure(messagingException)); } } http://git-wip-us.apache.org/repos/asf/james-project/blob/9898e18e/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrerTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrerTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrerTest.java index 477d945..27de817 100644 --- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrerTest.java +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/MailDelivrerTest.java @@ -19,24 +19,31 @@ package org.apache.james.transport.mailets.remoteDelivery; -import static org.mockito.Mockito.mock; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; import javax.mail.Address; import javax.mail.SendFailedException; import javax.mail.internet.InternetAddress; -import org.apache.james.dnsservice.api.DNSService; +import org.apache.james.dnsservice.api.TemporaryResolutionException; +import org.apache.james.domainlist.api.DomainList; +import org.apache.mailet.HostAddress; import org.apache.mailet.Mail; import org.apache.mailet.base.MailAddressFixture; import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailetConfig; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.UnmodifiableIterator; import com.sun.mail.smtp.SMTPSenderFailedException; public class MailDelivrerTest { @@ -44,11 +51,13 @@ public class MailDelivrerTest { private MailDelivrer testee; private Bouncer bouncer; + private DnsHelper dnsHelper; @Before public void setUp() { bouncer = mock(Bouncer.class); - testee = new MailDelivrer(mock(RemoteDeliveryConfiguration.class), mock(MailDelivrerToHost.class), mock(DNSService.class), bouncer, LOGGER); + dnsHelper = mock(DnsHelper.class); + testee = new MailDelivrer(mock(RemoteDeliveryConfiguration.class), mock(MailDelivrerToHost.class), dnsHelper, bouncer, LOGGER); } @Test @@ -200,4 +209,98 @@ public class MailDelivrerTest { verify(bouncer).bounce(mail, sfe); verifyNoMoreInteractions(bouncer); } + + @Test + public void deliverShouldReturnTemporaryFailureOnTemporaryResolutionException() throws Exception { + Mail mail = FakeMail.builder().recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES).build(); + + when(dnsHelper.retrieveHostAddressIterator(MailAddressFixture.JAMES_APACHE_ORG)).thenThrow(new TemporaryResolutionException()); + + ExecutionResult executionResult = testee.deliver(mail); + + assertThat(executionResult.getExecutionState()).isEqualTo(ExecutionResult.ExecutionState.TEMPORARY_FAILURE); + } + + @SuppressWarnings("deprecation") + @Test + @Ignore("Fails if first delivery attempt") + public void deliverShouldReturnTemporaryErrorWhenFirstDNSProblem() throws Exception { + Mail mail = FakeMail.builder().recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES).build(); + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") + .setProperty(RemoteDeliveryConfiguration.MAX_DNS_PROBLEM_RETRIES, "3") + .build(); + testee = new MailDelivrer(new RemoteDeliveryConfiguration(mailetConfig, mock(DomainList.class)), mock(MailDelivrerToHost.class), dnsHelper, bouncer, LOGGER); + + UnmodifiableIterator<HostAddress> empty = ImmutableList.<HostAddress>of().iterator(); + when(dnsHelper.retrieveHostAddressIterator(MailAddressFixture.JAMES_APACHE_ORG)).thenReturn(empty); + + ExecutionResult executionResult = testee.deliver(mail); + + assertThat(executionResult.getExecutionState()).isEqualTo(ExecutionResult.ExecutionState.TEMPORARY_FAILURE); + } + + @SuppressWarnings("deprecation") + @Test + public void deliverShouldReturnTemporaryErrorWhenToleratedDNSProblem() throws Exception { + Mail mail = FakeMail.builder().recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES).build(); + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") + .setProperty(RemoteDeliveryConfiguration.MAX_DNS_PROBLEM_RETRIES, "3") + .build(); + DeliveryRetriesHelper.incrementRetries(mail); + testee = new MailDelivrer(new RemoteDeliveryConfiguration(mailetConfig, mock(DomainList.class)), mock(MailDelivrerToHost.class), dnsHelper, bouncer, LOGGER); + + UnmodifiableIterator<HostAddress> empty = ImmutableList.<HostAddress>of().iterator(); + when(dnsHelper.retrieveHostAddressIterator(MailAddressFixture.JAMES_APACHE_ORG)).thenReturn(empty); + + ExecutionResult executionResult = testee.deliver(mail); + + assertThat(executionResult.getExecutionState()).isEqualTo(ExecutionResult.ExecutionState.TEMPORARY_FAILURE); + } + + @SuppressWarnings("deprecation") + @Test + @Ignore("One more failure is tolerated than specified by the configuration") + public void deliverShouldReturnPermanentErrorWhenLimitDNSProblemReached() throws Exception { + Mail mail = FakeMail.builder().recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES).build(); + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") + .setProperty(RemoteDeliveryConfiguration.MAX_DNS_PROBLEM_RETRIES, "3") + .build(); + DeliveryRetriesHelper.incrementRetries(mail); + DeliveryRetriesHelper.incrementRetries(mail); + DeliveryRetriesHelper.incrementRetries(mail); + testee = new MailDelivrer(new RemoteDeliveryConfiguration(mailetConfig, mock(DomainList.class)), mock(MailDelivrerToHost.class), dnsHelper, bouncer, LOGGER); + + UnmodifiableIterator<HostAddress> empty = ImmutableList.<HostAddress>of().iterator(); + when(dnsHelper.retrieveHostAddressIterator(MailAddressFixture.JAMES_APACHE_ORG)).thenReturn(empty); + + ExecutionResult executionResult = testee.deliver(mail); + + assertThat(executionResult.getExecutionState()).isEqualTo(ExecutionResult.ExecutionState.PERMANENT_FAILURE); + } + + @SuppressWarnings("deprecation") + @Test + public void deliverShouldReturnPermanentErrorWhenLimitDNSProblemExceeded() throws Exception { + Mail mail = FakeMail.builder().recipients(MailAddressFixture.ANY_AT_JAMES, MailAddressFixture.OTHER_AT_JAMES).build(); + FakeMailetConfig mailetConfig = FakeMailetConfig.builder() + .setProperty(RemoteDeliveryConfiguration.DELIVERY_THREADS, "1") + .setProperty(RemoteDeliveryConfiguration.MAX_DNS_PROBLEM_RETRIES, "3") + .build(); + DeliveryRetriesHelper.incrementRetries(mail); + DeliveryRetriesHelper.incrementRetries(mail); + DeliveryRetriesHelper.incrementRetries(mail); + DeliveryRetriesHelper.incrementRetries(mail); + testee = new MailDelivrer(new RemoteDeliveryConfiguration(mailetConfig, mock(DomainList.class)), mock(MailDelivrerToHost.class), dnsHelper, bouncer, LOGGER); + + UnmodifiableIterator<HostAddress> empty = ImmutableList.<HostAddress>of().iterator(); + when(dnsHelper.retrieveHostAddressIterator(MailAddressFixture.JAMES_APACHE_ORG)).thenReturn(empty); + + ExecutionResult executionResult = testee.deliver(mail); + + assertThat(executionResult.getExecutionState()).isEqualTo(ExecutionResult.ExecutionState.PERMANENT_FAILURE); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org