View Javadoc

1   /*
2    * $Id: DispatchTestCase.java 22450 2011-07-19 08:20:41Z 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.transport.http;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleException;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.junit4.FunctionalTestCase;
19  import org.mule.tck.junit4.rule.DynamicPort;
20  
21  import java.io.ByteArrayInputStream;
22  import java.util.Arrays;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.concurrent.CountDownLatch;
26  import java.util.concurrent.TimeUnit;
27  
28  import org.junit.Rule;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  public class DispatchTestCase extends FunctionalTestCase
34  {
35      @Rule
36      public DynamicPort dynamicPort = new DynamicPort("port1");
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "dispatch-conf.xml";
42      }
43  
44      @Test
45      public void testEchoService() throws Exception
46      {
47          final int THREADS = 10;
48          final CountDownLatch latch = new CountDownLatch(THREADS);
49  
50          final MuleClient client = new MuleClient(muleContext);
51  
52          final byte[] buf = new byte[8192];
53          Arrays.fill(buf, (byte) 'a');
54  
55          client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inEchoService")).getAddress(),
56              new DefaultMuleMessage(new ByteArrayInputStream(buf), muleContext));
57  
58          for (int i = 0; i < THREADS; i++)
59          {
60              new Thread(new Runnable()
61              {
62                  @Override
63                  public void run()
64                  {
65                      Map<String, Object> props = new HashMap<String, Object>();
66                      props.put(MuleProperties.MULE_REPLY_TO_PROPERTY, "vm://queue");
67                      try
68                      {
69                          for (int j = 0; j < 20; j++)
70                          {
71                              client.dispatch(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inEchoService")).getAddress(),
72                                  new DefaultMuleMessage(buf, muleContext), props);
73                          }
74  
75                      }
76                      catch (MuleException e)
77                      {
78                          e.printStackTrace();
79                      }
80                      finally
81                      {
82                          latch.countDown();
83                      }
84                  }
85  
86              }).start();
87          }
88  
89          //wait for somewhere close to 15 seconds before the test times out
90          latch.await(40, TimeUnit.SECONDS);
91  
92          int count = 0;
93          while (client.request("vm://queue", RECEIVE_TIMEOUT) != null)
94          {
95              count++;
96          }
97  
98          assertEquals(200, count);
99      }
100 }