View Javadoc

1   /*
2    * $Id$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  public class RoundRobinTestCase extends FunctionalTestCase
22  {
23      private static final int NUMBER_OF_MESSAGES = 10;
24      private static final int NUMBER_OF_WRITERS = 10;
25      private static final int NUMBER_OF_ENDPOINTS = 5;
26      private MuleClient client;
27  
28      @Override
29      protected String getConfigResources()
30      {
31          return "round-robin-test.xml";
32      }
33  
34      public void testRoundRobin() throws Exception
35      {
36          client = new MuleClient(muleContext);
37          List<Thread> writers = new ArrayList<Thread>();
38          for (int i = 0; i < NUMBER_OF_WRITERS; i++)
39          {
40              writers.add(new Thread(new MessageWriter(i)));
41          }
42          for (Thread writer : writers)
43          {
44              writer.start();
45          }
46          for (Thread writer : writers)
47          {
48              writer.join();
49          }
50  
51          for (int i = 0, j = 0; i < NUMBER_OF_WRITERS * NUMBER_OF_MESSAGES; i++)
52          {
53              // Message should be disrtibuted uniformly among endpoints
54              String path = "vm://output" + j;
55              MuleMessage msg = client.request(path, 0);
56              assertNotNull(msg);
57              logger.debug(path + ": " + msg.getPayloadAsString());
58              j = (j + 1) % NUMBER_OF_ENDPOINTS;
59          }
60      }
61  
62      class MessageWriter implements Runnable
63      {
64          private int id;
65  
66          MessageWriter(int id)
67          {
68              this.id = id;
69          }
70  
71          public void run()
72          {
73              for (int i = 0; i < NUMBER_OF_MESSAGES; i++)
74              {
75                  try
76                  {
77                      client.send("vm://input", "Writer " + id + " Message " + i, null);
78                  }
79                  catch (MuleException ex)
80                  {
81                      logger.info("Unexpected exception dispatching message", ex);
82                  }
83              }
84          }
85      }
86  }