Search in sources :

Example 11 with Advised

use of cn.taketoday.aop.framework.Advised in project today-infrastructure by TAKETODAY.

the class RetryOperationsInterceptorTests method testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation.

@Test
public void testDefaultInterceptorWithRetryListenerInspectingTheMethodInvocation() throws Exception {
    final String label = "FOO";
    final String classTagName = "class";
    final String methodTagName = "method";
    final String labelTagName = "label";
    final Map<String, String> monitoringTags = new HashMap<String, String>();
    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(new SimpleRetryPolicy(2));
    template.registerListener(new MethodInvocationRetryListenerSupport() {

        @Override
        protected <T, E extends Throwable> void doClose(RetryContext context, MethodInvocationRetryCallback<T, E> callback, Throwable throwable) {
            monitoringTags.put(labelTagName, callback.getLabel());
            Method method = callback.getInvocation().getMethod();
            monitoringTags.put(classTagName, method.getDeclaringClass().getSimpleName());
            monitoringTags.put(methodTagName, method.getName());
        }
    });
    this.interceptor.setLabel(label);
    this.interceptor.setRetryOperations(template);
    ((Advised) this.service).addAdvice(this.interceptor);
    this.service.service();
    assertEquals(2, count);
    assertEquals(3, monitoringTags.entrySet().size());
    assertThat(monitoringTags.get(labelTagName), equalTo(label));
    assertThat(monitoringTags.get(classTagName), equalTo(Service.class.getSimpleName()));
    assertThat(monitoringTags.get(methodTagName), equalTo("service"));
}
Also used : MethodInvocationRetryListenerSupport(cn.taketoday.retry.listener.MethodInvocationRetryListenerSupport) HashMap(java.util.HashMap) RetryContext(cn.taketoday.retry.RetryContext) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) Method(java.lang.reflect.Method) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Advised(cn.taketoday.aop.framework.Advised) Test(org.junit.Test)

Example 12 with Advised

use of cn.taketoday.aop.framework.Advised in project today-infrastructure by TAKETODAY.

the class RetryOperationsInterceptorTests method testRetryExceptionAfterTooManyAttempts.

@Test
public void testRetryExceptionAfterTooManyAttempts() throws Exception {
    ((Advised) this.service).addAdvice(this.interceptor);
    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(new NeverRetryPolicy());
    this.interceptor.setRetryOperations(template);
    try {
        this.service.service();
        fail("Expected Exception.");
    } catch (Exception e) {
        assertTrue(e.getMessage().startsWith("Not enough calls"));
    }
    assertEquals(1, count);
}
Also used : RetryTemplate(cn.taketoday.retry.support.RetryTemplate) Advised(cn.taketoday.aop.framework.Advised) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Test(org.junit.Test)

Example 13 with Advised

use of cn.taketoday.aop.framework.Advised in project today-infrastructure by TAKETODAY.

the class StatefulRetryOperationsInterceptorTests method testRetryExceptionAfterTooManyAttemptsWithNoRecovery.

@Test
public void testRetryExceptionAfterTooManyAttemptsWithNoRecovery() throws Exception {
    ((Advised) service).addAdvice(interceptor);
    interceptor.setRetryOperations(retryTemplate);
    retryTemplate.setRetryPolicy(new NeverRetryPolicy());
    try {
        service.service("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
    try {
        service.service("foo");
        fail("Expected ExhaustedRetryException");
    } catch (ExhaustedRetryException e) {
        // expected
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Retry exhausted"));
    }
    assertEquals(1, count);
}
Also used : ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Advised(cn.taketoday.aop.framework.Advised) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 14 with Advised

use of cn.taketoday.aop.framework.Advised in project today-infrastructure by TAKETODAY.

the class StatefulRetryOperationsInterceptorTests method testTransformerRecoveryAfterTooManyAttempts.

@Test
public void testTransformerRecoveryAfterTooManyAttempts() throws Exception {
    ((Advised) transformer).addAdvice(interceptor);
    interceptor.setRetryOperations(retryTemplate);
    retryTemplate.setRetryPolicy(new NeverRetryPolicy());
    try {
        transformer.transform("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
    interceptor.setRecoverer((MethodInvocationRecoverer<Collection<String>>) (data, cause) -> {
        count++;
        return Collections.singleton((String) data[0]);
    });
    Collection<String> result = transformer.transform("foo");
    assertEquals(2, count);
    assertEquals(1, result.size());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) AlwaysRetryPolicy(cn.taketoday.retry.policy.AlwaysRetryPolicy) RetryListener(cn.taketoday.retry.RetryListener) Advised(cn.taketoday.aop.framework.Advised) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ArgumentCaptor(org.mockito.ArgumentCaptor) RetryTemplate(cn.taketoday.retry.support.RetryTemplate) RetryContext(cn.taketoday.retry.RetryContext) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) Assert.fail(org.junit.Assert.fail) SimpleRetryPolicy(cn.taketoday.retry.policy.SimpleRetryPolicy) RetryCallback(cn.taketoday.retry.RetryCallback) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Collection(java.util.Collection) RetryOperations(cn.taketoday.retry.RetryOperations) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.when(org.mockito.Mockito.when) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) RecoveryCallback(cn.taketoday.retry.RecoveryCallback) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) DefaultRetryState(cn.taketoday.retry.support.DefaultRetryState) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Advised(cn.taketoday.aop.framework.Advised) Collection(java.util.Collection) NeverRetryPolicy(cn.taketoday.retry.policy.NeverRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Example 15 with Advised

use of cn.taketoday.aop.framework.Advised in project today-infrastructure by TAKETODAY.

the class StatefulRetryOperationsInterceptorTests method testDefaultInterceptorAlwaysRetry.

@Test
public void testDefaultInterceptorAlwaysRetry() throws Exception {
    retryTemplate.setRetryPolicy(new AlwaysRetryPolicy());
    interceptor.setRetryOperations(retryTemplate);
    ((Advised) service).addAdvice(interceptor);
    try {
        service.service("foo");
        fail("Expected Exception.");
    } catch (Exception e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.startsWith("Not enough calls"));
    }
    assertEquals(1, count);
}
Also used : Advised(cn.taketoday.aop.framework.Advised) AlwaysRetryPolicy(cn.taketoday.retry.policy.AlwaysRetryPolicy) ExhaustedRetryException(cn.taketoday.retry.ExhaustedRetryException) Test(org.junit.jupiter.api.Test)

Aggregations

Advised (cn.taketoday.aop.framework.Advised)85 Test (org.junit.jupiter.api.Test)58 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)40 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)23 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)18 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)18 Advisor (cn.taketoday.aop.Advisor)13 ExhaustedRetryException (cn.taketoday.retry.ExhaustedRetryException)12 SimpleRetryPolicy (cn.taketoday.retry.policy.SimpleRetryPolicy)12 RetryTemplate (cn.taketoday.retry.support.RetryTemplate)12 NeverRetryPolicy (cn.taketoday.retry.policy.NeverRetryPolicy)10 Test (org.junit.Test)10 NopInterceptor (cn.taketoday.aop.NopInterceptor)9 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)9 DefaultPointcutAdvisor (cn.taketoday.aop.support.DefaultPointcutAdvisor)8 SerializableNopInterceptor (cn.taketoday.aop.SerializableNopInterceptor)7 MethodInvocation (org.aopalliance.intercept.MethodInvocation)7 RetryContext (cn.taketoday.retry.RetryContext)6 AlwaysRetryPolicy (cn.taketoday.retry.policy.AlwaysRetryPolicy)6 ArrayList (java.util.ArrayList)6