View Javadoc

1   /*
2    * $Id: UdpConnectorFunctionalTestCase.java 22449 2011-07-19 07:40:43Z justin.calleja $
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.transport.udp.functional;
12  
13  import static org.junit.Assert.fail;
14  
15  import java.util.Arrays;
16  import java.util.Collection;
17  import java.util.HashSet;
18  import java.util.Set;
19  
20  import org.junit.Test;
21  import org.junit.runners.Parameterized.Parameters;
22  import org.mule.api.MuleMessage;
23  import org.mule.module.client.MuleClient;
24  import org.mule.tck.AbstractServiceAndFlowTestCase;
25  
26  public class UdpConnectorFunctionalTestCase extends AbstractServiceAndFlowTestCase
27  {
28      public static final String MESSAGE = "hello";
29      public static final int TOTAL_MESSAGE_COUNT = 1000;
30      public static final int MAX_NUMBER_OF_BATCHES = 128;
31      public static final long MAX_PAUSE_PERIOD = 2000;
32      public static final long MIN_PAUSE_PERIOD = 10;
33      public static final long BETWEEN_BATCH_PAUSE = 5000;
34      
35      public UdpConnectorFunctionalTestCase(ConfigVariant variant, String configResources)
36      {
37          super(variant, configResources);
38      }
39      
40      @Parameters
41      public static Collection<Object[]> parameters()
42      {
43          return Arrays.asList(new Object[][]{
44              {ConfigVariant.SERVICE, "udp-functional-test-service.xml"},
45              {ConfigVariant.FLOW, "udp-functional-test-flow.xml"}
46          });
47      }
48  
49      /**
50       * We try progressively smaller batches to see if there are issues with internal
51       * buffers.  If we don't get 100% success eventually, we fail.
52       */
53      @Test
54      public void testMany() throws Exception
55      {
56          int numberOfBatches = 0;
57          boolean ok = false;
58          while (!ok && numberOfBatches < MAX_NUMBER_OF_BATCHES)
59          {
60              numberOfBatches = 0 == numberOfBatches ? 1 : numberOfBatches * 2;
61              ok = doTestSome(TOTAL_MESSAGE_COUNT, TOTAL_MESSAGE_COUNT / numberOfBatches);
62              if (!ok)
63              {
64                  logger.warn("UDP failed to send " + TOTAL_MESSAGE_COUNT + " messages in " + numberOfBatches + " batches");
65  
66                  // clean out the system
67                  try
68                  {
69                      synchronized(this)
70                      {
71                          this.wait(BETWEEN_BATCH_PAUSE);
72                      }
73                  }
74                  catch (InterruptedException e)
75                  {
76                      // ignore
77                  }
78                  MuleClient client = new MuleClient(muleContext);
79                  int dropped = 0;
80                  while (null != client.request("vm://foo", MAX_PAUSE_PERIOD))
81                  {
82                      // discard old messages
83                      dropped++;
84                  }
85                  logger.info("Cleaned out " + dropped + " messages");
86              }
87          }
88  
89          if (!ok)
90          {
91              fail("Couldn't get UDP to 100% with a batch size of " + TOTAL_MESSAGE_COUNT / numberOfBatches);
92          }
93          else
94          {
95              logger.info("Required " + numberOfBatches + " batches before UDP 100% OK ("
96                      + TOTAL_MESSAGE_COUNT + " messages)");
97          }
98      }
99  
100     /**
101      * @param numberOfMessages Total number of tests
102      * @param burst Number of mesages to send between wait periods
103      * @return true if all messages received
104      * @throws Exception
105      */
106     protected boolean doTestSome(int numberOfMessages, int burst) throws Exception
107     {
108         logger.info("Trying " + numberOfMessages + " messages in batches of " + burst);
109         MuleClient client = new MuleClient(muleContext);
110 
111         int burstCount = 0;
112         Set receivedMessages = new HashSet(numberOfMessages);
113         for (int sentPackets = 0; sentPackets < numberOfMessages; sentPackets++)
114         {
115             burstCount++;
116             String msg = MESSAGE + sentPackets;
117             client.dispatch("serverEndpoint", msg, null);
118 
119             if (burst == burstCount || sentPackets == numberOfMessages-1)
120             {
121                 long pause = MAX_PAUSE_PERIOD;
122                 for (int i = 0; i < burstCount; i++)
123                 {
124                     MuleMessage message = client.request("vm://foo", pause);
125                     // reduce waiting time once we have a bunch of messages coming in
126                     // (without this, we can end up waiting for very long times....)
127                     pause = Math.max(MIN_PAUSE_PERIOD, pause / 2);
128                     if (null != message)
129                     {
130                         receivedMessages.add(message.getPayloadAsString());
131                     }
132                 }
133                 burstCount = 0;
134             }
135         }
136 
137         boolean ok = receivedMessages.size() == numberOfMessages;
138         if (!ok)
139         {
140             logger.info("Received " + receivedMessages.size() + " messages");
141         }
142         return ok;
143     }
144 
145 }