View Javadoc

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