View Javadoc

1   /*
2    * $Id: CollectionAggregatorRouterTimeoutTestCase.java 22421 2011-07-15 05:05:06Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.test.routing;
12  
13  import org.mule.api.context.notification.RoutingNotificationListener;
14  import org.mule.context.notification.RoutingNotification;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.AbstractServiceAndFlowTestCase;
17  import org.mule.tck.functional.FunctionalTestComponent;
18  
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.List;
22  import java.util.concurrent.atomic.AtomicInteger;
23  
24  import org.junit.Test;
25  import org.junit.runners.Parameterized.Parameters;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNotNull;
29  
30  public class CollectionAggregatorRouterTimeoutTestCase extends AbstractServiceAndFlowTestCase
31  {
32      @Parameters
33      public static Collection<Object[]> parameters()
34      {
35          return Arrays.asList(new Object[][]{
36              {ConfigVariant.SERVICE, "collection-aggregator-router-timeout-test-service.xml"},
37              {ConfigVariant.FLOW, "collection-aggregator-router-timeout-test-flow.xml"}});
38      }
39  
40      public CollectionAggregatorRouterTimeoutTestCase(ConfigVariant variant, String configResources)
41      {
42          super(variant, configResources);
43      }
44  
45      @Test
46      public void testNoFailOnTimeout() throws Exception
47      {
48          // correlation timeouts should not fire in this scenario, check it
49          final AtomicInteger correlationTimeoutCount = new AtomicInteger(0);
50          muleContext.registerListener(new RoutingNotificationListener<RoutingNotification>()
51          {
52              @Override
53              public void onNotification(RoutingNotification notification)
54              {
55                  if (notification.getAction() == RoutingNotification.CORRELATION_TIMEOUT)
56                  {
57                      correlationTimeoutCount.incrementAndGet();
58                  }
59              }
60          });
61  
62          FunctionalTestComponent vortex = (FunctionalTestComponent) getComponent("vortex");
63          FunctionalTestComponent aggregator = (FunctionalTestComponent) getComponent("aggregator");
64  
65          MuleClient client = new MuleClient(muleContext);
66          List<String> list = Arrays.asList("first", "second");
67          client.dispatch("vm://splitter", list, null);
68  
69          Thread.sleep(5000);
70  
71          // no correlation timeout should ever fire
72          assertEquals("Correlation timeout should not have happened.", 0, correlationTimeoutCount.intValue());
73  
74          // should receive only the second message
75          assertEquals("Vortex received wrong number of messages.", 1, vortex.getReceivedMessagesCount());
76          assertEquals("Wrong message received", "second", vortex.getLastReceivedMessage());
77  
78          // should receive only the first part
79          assertEquals("Aggregator received wrong number of messages.", 1,
80              aggregator.getReceivedMessagesCount());
81          assertEquals("Wrong message received", Arrays.asList("first"), aggregator.getLastReceivedMessage());
82  
83          // wait for the vortex timeout (6000ms for vortext + 2000ms for aggregator
84          // timeout + some extra for a test)
85          Thread.sleep(9000);
86  
87          // now get the messages which were lagging behind
88          // it will receive only one (first) as second will be discarded by the worker
89          // because it has already dispatched one with the same group id
90          assertEquals("Other messages never received by aggregator.", 1, aggregator.getReceivedMessagesCount());
91          assertNotNull(client.request("vm://out?connector=queue", 10000));
92      }
93  }