View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.http;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleException;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.api.endpoint.InboundEndpoint;
13  import org.mule.module.client.MuleClient;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  import org.mule.tck.junit4.rule.DynamicPort;
16  
17  import java.io.ByteArrayInputStream;
18  import java.util.Arrays;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
23  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24  import org.junit.Rule;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  
29  public class DispatchTestCase extends FunctionalTestCase
30  {
31  
32      @Rule
33      public DynamicPort dynamicPort = new DynamicPort("port1");
34  
35      @Override
36      protected String getConfigResources()
37      {
38          return "dispatch-conf.xml";
39      }
40  
41      @Test
42      public void testEchoService() throws Exception
43      {
44          final int THREADS = 10;
45          final CountDownLatch latch = new CountDownLatch(THREADS);
46  
47          final MuleClient client = new MuleClient(muleContext);
48  
49          final byte[] buf = new byte[8192];
50          Arrays.fill(buf, (byte) 'a');
51  
52          client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inEchoService")).getAddress(),
53              new DefaultMuleMessage(new ByteArrayInputStream(buf), muleContext));
54  
55          for (int i = 0; i < THREADS; i++)
56          {
57              new Thread(new Runnable()
58              {
59                  public void run()
60                  {
61                      Map<String, Object> props = new HashMap<String, Object>();
62                      props.put(MuleProperties.MULE_REPLY_TO_PROPERTY, "vm://queue");
63                      try
64                      {
65                          for (int i = 0; i < 20; i++)
66                          {
67                              client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inEchoService")).getAddress(),
68                                  new DefaultMuleMessage(buf, muleContext), props);
69                          }
70  
71                      }
72                      catch (MuleException e)
73                      {
74                          e.printStackTrace();
75                      }
76                      finally
77                      {
78                          latch.countDown();
79                      }
80                  }
81  
82              }).start();
83          }
84  
85          //wait for somewhere close to 15 seconds before the test times out
86          latch.await(40, TimeUnit.SECONDS);
87  
88          int count = 0;
89          while (client.request("vm://queue", RECEIVE_TIMEOUT) != null)
90          {
91              count++;
92          }
93  
94          assertEquals(200, count);
95      }
96  
97  }