1
2
3
4
5
6
7 package org.mule.test.integration.resolvers;
8
9 import org.mule.api.MuleException;
10 import org.mule.api.MuleMessage;
11 import org.mule.module.client.MuleClient;
12 import org.mule.tck.junit4.FunctionalTestCase;
13 import org.mule.util.Base64;
14
15 import java.util.List;
16 import java.util.Random;
17
18 import org.junit.Test;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 public class DefaultEntryPointResolverSetMultithreadingTestCase extends FunctionalTestCase
27 {
28
29 @Override
30 protected String getConfigResources()
31 {
32 return "org/mule/test/integration/resolvers/default-entry-point-resolver-multithreading-test-config.xml";
33 }
34
35 @Override
36 public int getTestTimeoutSecs()
37 {
38 return 120;
39 }
40
41 @Test
42 public void testMultithreaded() throws Exception
43 {
44 final int numberOfThreads = 50;
45 final int requestCount = 100;
46 ClientRequest[] clients = new ClientRequest[numberOfThreads];
47 for (int i = 0; i < numberOfThreads; i++)
48 {
49 clients[i] = new ClientRequest(requestCount);
50 }
51
52 for (ClientRequest clientRequest : clients)
53 {
54 clientRequest.start();
55 try
56 {
57 Thread.sleep(5);
58 }
59 catch (InterruptedException ie)
60 {
61
62 }
63 }
64
65 for (int i = 0; i < numberOfThreads; i++)
66 {
67 try
68 {
69 clients[i].join();
70 }
71 catch (InterruptedException ie)
72 {
73
74 }
75 }
76 }
77
78 private static class ClientRequest extends Thread
79 {
80 final MuleClient client;
81 int requestCount;
82
83 private ClientRequest(final int requestCount) throws MuleException
84 {
85 client = new MuleClient(muleContext);
86 this.requestCount = requestCount;
87 }
88
89 @Override
90 public void run()
91 {
92 final byte[] payload = createPayload();
93
94 while (--requestCount >= 0)
95 {
96 try
97 {
98 final MuleMessage outbound = client.send("vm://test.inbound.sync", payload, null);
99 assertNull(outbound.getExceptionPayload());
100 assertNotNull(outbound.getPayload());
101 byte[] bytes = null;
102 if (outbound.getPayload() instanceof byte[])
103 {
104 bytes = (byte[]) outbound.getPayload();
105 }
106 else if (outbound.getPayload() instanceof List)
107 {
108 final List<?> list = (List<?>) outbound.getPayload();
109 assertEquals(1, list.size());
110 assertTrue(list.get(0) instanceof byte[]);
111 bytes = (byte[]) list.get(0);
112 }
113 else
114 {
115 fail("unexpected payload type");
116 }
117 assertEquals(Base64.encodeBytes(payload), Base64.encodeBytes(bytes));
118 }
119 catch (Exception e)
120 {
121 fail("failed with exception: " + e);
122 }
123 }
124 }
125
126 private byte[] createPayload()
127 {
128 Random random = new Random();
129 final int size = 55;
130 byte[] payload = new byte[size];
131 random.nextBytes(payload);
132 return payload;
133 }
134 }
135
136 public static class EchoBytes
137 {
138 public byte[] echo(byte[] input)
139 {
140 return input;
141 }
142 }
143 }