View Javadoc

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