View Javadoc

1   /*
2    * $Id: DefaultEntryPointResolverSetMultithreadingTestCase.java 20690 2010-12-14 01:22:41Z dzapata $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.test.integration.resolvers;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.FunctionalTestCase;
17  import org.mule.util.Base64;
18  
19  import java.util.List;
20  import java.util.Random;
21  
22  public class DefaultEntryPointResolverSetMultithreadingTestCase extends FunctionalTestCase
23  {
24      public DefaultEntryPointResolverSetMultithreadingTestCase()
25      {
26          super();    
27          testTimeoutSecs = testTimeoutSecs * 2;
28      }
29      
30      @Override
31      protected String getConfigResources()
32      {
33          return "org/mule/test/integration/resolvers/default-entry-point-resolver-multithreading-test-config.xml";
34      }
35      
36      @Override
37      public int getTestTimeoutSecs()
38      {
39          return 120;
40      }
41  
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                  // ignore
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                  // ignore
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 }