1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.http;
12
13 import org.mule.DefaultMuleMessage;
14 import org.mule.api.MuleException;
15 import org.mule.api.config.MuleProperties;
16 import org.mule.api.endpoint.InboundEndpoint;
17 import org.mule.module.client.MuleClient;
18 import org.mule.tck.DynamicPortTestCase;
19
20 import java.io.ByteArrayInputStream;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
26 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
27
28 public class DispatchTestCase extends DynamicPortTestCase
29 {
30 public void testEchoService() throws Exception
31 {
32 final int THREADS = 10;
33 final CountDownLatch latch = new CountDownLatch(THREADS);
34
35 final MuleClient client = new MuleClient(muleContext);
36
37 final byte[] buf = new byte[8192];
38 Arrays.fill(buf, (byte) 'a');
39
40 client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inEchoService")).getAddress(),
41 new DefaultMuleMessage(new ByteArrayInputStream(buf), muleContext));
42
43 for (int i = 0; i < THREADS; i++)
44 {
45 new Thread(new Runnable()
46 {
47 public void run()
48 {
49 Map<String, Object> props = new HashMap<String, Object>();
50 props.put(MuleProperties.MULE_REPLY_TO_PROPERTY, "vm://queue");
51 try
52 {
53 for (int i = 0; i < 20; i++)
54 {
55 client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inEchoService")).getAddress(),
56 new DefaultMuleMessage(buf, muleContext), props);
57 }
58
59 }
60 catch (MuleException e)
61 {
62 e.printStackTrace();
63 }
64 finally
65 {
66 latch.countDown();
67 }
68 }
69
70 }).start();
71 }
72
73
74 latch.await(40, TimeUnit.SECONDS);
75
76 int count = 0;
77 while (client.request("vm://queue", RECEIVE_TIMEOUT) != null)
78 {
79 count++;
80 }
81
82 assertEquals(200, count);
83 }
84
85 @Override
86 protected String getConfigResources()
87 {
88 return "dispatch-conf.xml";
89 }
90
91 @Override
92 protected int getNumPortsToFind()
93 {
94 return 1;
95 }
96
97 }