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