1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.udp.functional;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.module.client.MuleClient;
15 import org.mule.tck.FunctionalTestCase;
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
37
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
52 try
53 {
54 synchronized(this)
55 {
56 this.wait(BETWEEN_BATCH_PAUSE);
57 }
58 }
59 catch (InterruptedException e)
60 {
61
62 }
63 MuleClient client = new MuleClient();
64 int dropped = 0;
65 while (null != client.request("vm://foo", MAX_PAUSE_PERIOD))
66 {
67
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
87
88
89
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 MuleMessage message = client.request("vm://foo", pause);
110
111
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 }