View Javadoc

1   /*
2    * $Id: CollectionAggregatorRouterTimeoutTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.FunctionalTestCase;
17  import org.mule.tck.functional.FunctionalTestComponent;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
23  
24  public class CollectionAggregatorRouterTimeoutTestCase extends FunctionalTestCase
25  {
26  
27      protected String getConfigResources()
28      {
29          return "collection-aggregator-router-timeout-test.xml";
30      }
31  
32      public void testNoFailOnTimeout() throws Exception
33      {
34          // correlation timeouts should not fire in this scenario, check it
35          final AtomicInteger correlationTimeoutCount = new AtomicInteger(0);
36          muleContext.registerListener(new RoutingNotificationListener<RoutingNotification>()
37          {
38              public void onNotification(RoutingNotification notification)
39              {
40                  if (notification.getAction() == RoutingNotification.CORRELATION_TIMEOUT)
41                  {
42                      correlationTimeoutCount.incrementAndGet();
43                  }
44              }
45          });
46  
47          FunctionalTestComponent vortex = (FunctionalTestComponent) getComponent("vortex");
48          FunctionalTestComponent aggregator = (FunctionalTestComponent) getComponent("aggregator");
49  
50          MuleClient client = new MuleClient(muleContext);
51          List list = Arrays.asList("first", "second");
52          client.dispatch("vm://splitter", list, null);
53  
54          Thread.sleep(5000);
55  
56          // no correlation timeout should ever fire
57          assertEquals("Correlation timeout should not have happened.", 0, correlationTimeoutCount.intValue());
58  
59          // should receive only the second message
60          assertEquals("Vortex received wrong number of messages.", 1, vortex.getReceivedMessagesCount());
61          assertEquals("Wrong message received", "second", vortex.getLastReceivedMessage());
62  
63          // should receive only the first part
64          assertEquals("Aggregator received wrong number of messages.", 1, aggregator.getReceivedMessagesCount());
65          assertEquals("Wrong message received", Arrays.asList("first"), aggregator.getLastReceivedMessage());
66  
67          // wait for the vortex timeout (6000ms for vortext + 2000ms for aggregator timeout + some extra for a test)
68          Thread.sleep(9000);
69  
70          // now get the messages which were lagging behind
71          // 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
72          assertEquals("Other messages never received by aggregator.", 1, aggregator.getReceivedMessagesCount());
73          assertNotNull(client.request("vm://out?connector=queue", 10000));
74      }
75  }