1
2
3
4
5
6
7
8
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
51
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
67 try
68 {
69 synchronized(this)
70 {
71 this.wait(BETWEEN_BATCH_PAUSE);
72 }
73 }
74 catch (InterruptedException e)
75 {
76
77 }
78 MuleClient client = new MuleClient(muleContext);
79 int dropped = 0;
80 while (null != client.request("vm://foo", MAX_PAUSE_PERIOD))
81 {
82
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
102
103
104
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
126
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 }