Retry Advice Test Polishing

Clean up and add test with recovery.
This commit is contained in:
Gary Russell
2015-12-15 15:39:04 -05:00
committed by Artem Bilan
parent 52e690bc89
commit aca5646181
4 changed files with 62 additions and 24 deletions

View File

@@ -24,10 +24,14 @@
<beans:bean class="org.springframework.integration.config.xml.DefaultOutboundChannelAdapterParserTests$TestBean"/>
<poller task-executor="executor" max-messages-per-poll="5" fixed-delay="20" />
<request-handler-advice-chain>
<retry-advice/>
<retry-advice max-attempts="3" recovery-channel="recovery"/>
</request-handler-advice-chain>
</outbound-channel-adapter>
<channel id="recovery">
<queue/>
</channel>
<task:executor id="executor" pool-size="5" />
<outbound-channel-adapter id="adapterC" channel="channelC" order="99">

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,11 @@
package org.springframework.integration.config.xml;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.hamcrest.Matchers;
import org.junit.Test;
@@ -26,10 +30,17 @@ import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.TestConsumer;
import org.springframework.integration.handler.MethodInvokingMessageHandler;
import org.springframework.integration.handler.advice.RequestHandlerRetryAdvice;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -40,6 +51,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@DirtiesContext
public class DefaultOutboundChannelAdapterParserTests {
@Autowired
@@ -68,10 +80,18 @@ public class DefaultOutboundChannelAdapterParserTests {
public void checkConfigWithInnerBeanAndPoller() {
Object adapter = context.getBean("adapterB");
assertEquals(Boolean.FALSE, TestUtils.getPropertyValue(adapter, "autoStartup"));
Object handler = TestUtils.getPropertyValue(adapter, "handler");
MessageHandler handler = TestUtils.getPropertyValue(adapter, "handler", MessageHandler.class);
assertTrue(AopUtils.isAopProxy(handler));
assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"),
Matchers.instanceOf(RequestHandlerRetryAdvice.class));
handler.handleMessage(new GenericMessage<>("foo"));
QueueChannel recovery = context.getBean("recovery", QueueChannel.class);
Message<?> received = recovery.receive(10000);
assertNotNull(received);
assertThat(received, instanceOf(ErrorMessage.class));
assertThat(received.getPayload(), instanceOf(MessagingException.class));
assertEquals("foo", ((MessagingException) received.getPayload()).getFailedMessage().getPayload());
}
@Test
@@ -88,6 +108,7 @@ public class DefaultOutboundChannelAdapterParserTests {
static class TestBean {
public void out(Object o) {
throw new RuntimeException("ex");
}
}

View File

@@ -41,4 +41,10 @@
</int:request-handler-advice-chain>
</int:service-activator>
<int:service-activator id="saDefaultRetry" input-channel="foo" expression="'foo'">
<int:request-handler-advice-chain>
<int:retry-advice />
</int:request-handler-advice-chain>
</int:service-activator>
</beans>

View File

@@ -70,37 +70,44 @@ public class RetryAdviceParserTests {
@Autowired @Qualifier("sa2.handler")
private MessageHandler handler2;
@Autowired @Qualifier("saDefaultRetry.handler")
private MessageHandler defaultRetryHandler;
@Autowired
private MessageChannel foo;
@Test
public void testAll() {
assertEquals(Integer.valueOf(3), TestUtils.getPropertyValue(a1, "retryTemplate.retryPolicy.maxAttempts", Integer.class));
assertEquals(Integer.valueOf(4), TestUtils.getPropertyValue(a2, "retryTemplate.retryPolicy.maxAttempts", Integer.class));
assertEquals(Integer.valueOf(5), TestUtils.getPropertyValue(a3, "retryTemplate.retryPolicy.maxAttempts", Integer.class));
assertEquals(Integer.valueOf(6), TestUtils.getPropertyValue(a4, "retryTemplate.retryPolicy.maxAttempts", Integer.class));
assertEquals(Integer.valueOf(7), TestUtils.getPropertyValue(a5, "retryTemplate.retryPolicy.maxAttempts", Integer.class));
assertEquals(Integer.valueOf(8), TestUtils.getPropertyValue(a6, "retryTemplate.retryPolicy.maxAttempts", Integer.class));
assertEquals(Integer.valueOf(3), TestUtils.getPropertyValue(a7, "retryTemplate.retryPolicy.maxAttempts", Integer.class));
assertEquals(3, TestUtils.getPropertyValue(a1, "retryTemplate.retryPolicy.maxAttempts"));
assertEquals(4, TestUtils.getPropertyValue(a2, "retryTemplate.retryPolicy.maxAttempts"));
assertEquals(5, TestUtils.getPropertyValue(a3, "retryTemplate.retryPolicy.maxAttempts"));
assertEquals(6, TestUtils.getPropertyValue(a4, "retryTemplate.retryPolicy.maxAttempts"));
assertEquals(7, TestUtils.getPropertyValue(a5, "retryTemplate.retryPolicy.maxAttempts"));
assertEquals(8, TestUtils.getPropertyValue(a6, "retryTemplate.retryPolicy.maxAttempts"));
assertEquals(3, TestUtils.getPropertyValue(a7, "retryTemplate.retryPolicy.maxAttempts"));
assertEquals(Long.valueOf(1000), TestUtils.getPropertyValue(a3, "retryTemplate.backOffPolicy.backOffPeriod", Long.class));
assertEquals(Long.valueOf(1234), TestUtils.getPropertyValue(a4, "retryTemplate.backOffPolicy.backOffPeriod", Long.class));
assertEquals(1000L, TestUtils.getPropertyValue(a3, "retryTemplate.backOffPolicy.backOffPeriod"));
assertEquals(1234L, TestUtils.getPropertyValue(a4, "retryTemplate.backOffPolicy.backOffPeriod"));
assertEquals(Long.valueOf(100), TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.initialInterval", Long.class));
assertEquals(Double.valueOf(2.0), TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.multiplier", Double.class));
assertEquals(Long.valueOf(30000), TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.maxInterval", Long.class));
assertEquals(Long.valueOf(1000), TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.initialInterval", Long.class));
assertEquals(Double.valueOf(3.0), TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.multiplier", Double.class));
assertEquals(Long.valueOf(10000), TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.maxInterval", Long.class));
assertEquals(100L, TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.initialInterval"));
assertEquals(2.0, TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.multiplier"));
assertEquals(30000L, TestUtils.getPropertyValue(a5, "retryTemplate.backOffPolicy.maxInterval"));
assertEquals(1000L, TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.initialInterval"));
assertEquals(3.0, TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.multiplier"));
assertEquals(10000L, TestUtils.getPropertyValue(a6, "retryTemplate.backOffPolicy.maxInterval"));
assertNull(TestUtils.getPropertyValue(a1, "recoveryCallback"));
assertNotNull(TestUtils.getPropertyValue(a7, "recoveryCallback"));
assertSame(foo, TestUtils.getPropertyValue(a7, "recoveryCallback.messagingTemplate.defaultDestination"));
assertEquals(Long.valueOf(4567), TestUtils.getPropertyValue(a7, "recoveryCallback.messagingTemplate.sendTimeout"));
assertSame(this.foo, TestUtils.getPropertyValue(a7, "recoveryCallback.messagingTemplate.defaultDestination"));
assertEquals(4567L, TestUtils.getPropertyValue(a7, "recoveryCallback.messagingTemplate.sendTimeout"));
assertSame(a1, TestUtils.getPropertyValue(handler1, "adviceChain", List.class).get(0));
assertEquals(Integer.valueOf(9), TestUtils.getPropertyValue(TestUtils.getPropertyValue(handler2, "adviceChain", List.class).get(0),
"retryTemplate.retryPolicy.maxAttempts", Integer.class));
assertSame(this.a1, TestUtils.getPropertyValue(this.handler1, "adviceChain", List.class).get(0));
assertEquals(9, TestUtils.getPropertyValue(
TestUtils.getPropertyValue(this.handler2, "adviceChain", List.class).get(0),
"retryTemplate.retryPolicy.maxAttempts"));
assertEquals(3, TestUtils.getPropertyValue(
TestUtils.getPropertyValue(this.defaultRetryHandler, "adviceChain", List.class).get(0),
"retryTemplate.retryPolicy.maxAttempts"));
}
}