Search in sources :

Example 1 with OnlyOnceTrigger

use of org.springframework.integration.test.util.OnlyOnceTrigger in project spring-integration by spring-projects.

the class PollerAdviceTests method testMixedAdvice.

@Test
public void testMixedAdvice() throws Exception {
    SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter();
    final List<String> callOrder = new ArrayList<>();
    final AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(4));
    MessageSource<Object> source = () -> {
        callOrder.add("c");
        latch.get().countDown();
        return null;
    };
    adapter.setSource(source);
    OnlyOnceTrigger trigger = new OnlyOnceTrigger();
    adapter.setTrigger(trigger);
    configure(adapter);
    List<Advice> adviceChain = new ArrayList<>();
    adviceChain.add((MethodInterceptor) invocation -> {
        callOrder.add("a");
        latch.get().countDown();
        return invocation.proceed();
    });
    final AtomicInteger count = new AtomicInteger();
    class TestSourceAdvice extends AbstractMessageSourceAdvice {

        @Override
        public boolean beforeReceive(MessageSource<?> target) {
            count.incrementAndGet();
            callOrder.add("b");
            latch.get().countDown();
            return true;
        }

        @Override
        public Message<?> afterReceive(Message<?> result, MessageSource<?> target) {
            callOrder.add("d");
            latch.get().countDown();
            return result;
        }
    }
    adviceChain.add(new TestSourceAdvice());
    adapter.setAdviceChain(adviceChain);
    adapter.afterPropertiesSet();
    adapter.start();
    assertTrue(latch.get().await(10, TimeUnit.SECONDS));
    // advice + advice + source + advice
    assertThat(callOrder, contains("a", "b", "c", "d"));
    adapter.stop();
    trigger.reset();
    latch.set(new CountDownLatch(4));
    adapter.start();
    assertTrue(latch.get().await(10, TimeUnit.SECONDS));
    adapter.stop();
    assertEquals(2, count.get());
    // Now test when the source is already a proxy.
    ProxyFactory pf = new ProxyFactory(source);
    pf.addAdvice((MethodInterceptor) Joinpoint::proceed);
    adapter.setSource((MessageSource<?>) pf.getProxy());
    trigger.reset();
    latch.set(new CountDownLatch(4));
    count.set(0);
    callOrder.clear();
    adapter.start();
    assertTrue(latch.get().await(10, TimeUnit.SECONDS));
    // advice + advice + source + advice
    assertThat(callOrder, contains("a", "b", "c", "d"));
    adapter.stop();
    trigger.reset();
    latch.set(new CountDownLatch(4));
    adapter.start();
    assertTrue(latch.get().await(10, TimeUnit.SECONDS));
    adapter.stop();
    assertEquals(2, count.get());
    Advisor[] advisors = ((Advised) adapter.getMessageSource()).getAdvisors();
    // make sure we didn't remove the original one
    assertEquals(2, advisors.length);
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Date(java.util.Date) SimpleActiveIdleMessageSourceAdvice(org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice) Autowired(org.springframework.beans.factory.annotation.Autowired) DynamicPeriodicTrigger(org.springframework.integration.util.DynamicPeriodicTrigger) Assert.assertThat(org.junit.Assert.assertThat) OnlyOnceTrigger(org.springframework.integration.test.util.OnlyOnceTrigger) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) NullChannel(org.springframework.integration.channel.NullChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Mockito.atLeast(org.mockito.Mockito.atLeast) TriggerContext(org.springframework.scheduling.TriggerContext) Trigger(org.springframework.scheduling.Trigger) CompoundTriggerAdvice(org.springframework.integration.aop.CompoundTriggerAdvice) EnableIntegration(org.springframework.integration.config.EnableIntegration) MessageChannel(org.springframework.messaging.MessageChannel) SimplePollSkipStrategy(org.springframework.integration.scheduling.SimplePollSkipStrategy) Configuration(org.springframework.context.annotation.Configuration) ServiceActivator(org.springframework.integration.annotation.ServiceActivator) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Log4j2LevelAdjuster(org.springframework.integration.test.rule.Log4j2LevelAdjuster) Matchers.contains(org.hamcrest.Matchers.contains) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) DirectChannel(org.springframework.integration.channel.DirectChannel) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractMessageSourceAdvice(org.springframework.integration.aop.AbstractMessageSourceAdvice) Advised(org.springframework.aop.framework.Advised) RunWith(org.junit.runner.RunWith) ExpressionControlBusFactoryBean(org.springframework.integration.config.ExpressionControlBusFactoryBean) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageSource(org.springframework.integration.core.MessageSource) ArrayList(java.util.ArrayList) Joinpoint(org.aopalliance.intercept.Joinpoint) Advice(org.aopalliance.aop.Advice) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Message(org.springframework.messaging.Message) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) LinkedList(java.util.LinkedList) Advisor(org.springframework.aop.Advisor) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) PollSkipAdvice(org.springframework.integration.scheduling.PollSkipAdvice) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Rule(org.junit.Rule) BeanFactory(org.springframework.beans.factory.BeanFactory) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Bean(org.springframework.context.annotation.Bean) CompoundTrigger(org.springframework.integration.util.CompoundTrigger) GenericMessage(org.springframework.messaging.support.GenericMessage) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractMessageSourceAdvice(org.springframework.integration.aop.AbstractMessageSourceAdvice) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ArrayList(java.util.ArrayList) MessageSource(org.springframework.integration.core.MessageSource) Advisor(org.springframework.aop.Advisor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) OnlyOnceTrigger(org.springframework.integration.test.util.OnlyOnceTrigger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Advised(org.springframework.aop.framework.Advised) SimpleActiveIdleMessageSourceAdvice(org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice) CompoundTriggerAdvice(org.springframework.integration.aop.CompoundTriggerAdvice) AbstractMessageSourceAdvice(org.springframework.integration.aop.AbstractMessageSourceAdvice) Advice(org.aopalliance.aop.Advice) PollSkipAdvice(org.springframework.integration.scheduling.PollSkipAdvice) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Advice (org.aopalliance.aop.Advice)1 Joinpoint (org.aopalliance.intercept.Joinpoint)1 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)1 Matchers.contains (org.hamcrest.Matchers.contains)1 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Rule (org.junit.Rule)1