View Javadoc

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