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