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.tcp;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  import org.mule.tck.junit4.rule.DynamicPort;
13  
14  import org.junit.Rule;
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertNotNull;
19  
20  public class TcpFunctionalTestCase extends FunctionalTestCase 
21  {
22  
23      protected static String TEST_MESSAGE = "Test TCP Request";
24  
25      @Rule
26      public DynamicPort dynamicPort1 = new DynamicPort("port1");
27  
28      @Rule
29      public DynamicPort dynamicPort2 = new DynamicPort("port2");
30  
31      @Override
32      protected String getConfigResources()
33      {
34          return "tcp-functional-test.xml";
35      }
36  
37      @Test
38      public void testSend() throws Exception
39      {
40          MuleClient client = new MuleClient(muleContext);
41          MuleMessage result = client.send("clientEndpoint", TEST_MESSAGE, null);
42          assertEquals(TEST_MESSAGE + " Received", result.getPayloadAsString());
43      }
44  
45      @Test
46      public void testDispatchAndReply() throws Exception
47      {
48          MuleClient client = new MuleClient(muleContext);
49          client.dispatch("asyncClientEndpoint", TEST_MESSAGE, null);
50          // MULE-2754
51          Thread.sleep(100);
52          MuleMessage result =  client.request("asyncClientEndpoint", 10000);
53          assertNotNull(result);
54          assertEquals(TEST_MESSAGE + " Received Async", result.getPayloadAsString());
55      }
56  
57      public void timeMultipleSend() throws Exception
58      {
59          MuleClient client = new MuleClient(muleContext);
60          long now = System.currentTimeMillis();
61          int count = 1000;
62          for (int i = 0; i < count; i++)
63          {
64              MuleMessage result = client.send("clientEndpoint", TEST_MESSAGE, null);
65              assertEquals(TEST_MESSAGE + " Received", result.getPayloadAsString());
66          }
67          long later = System.currentTimeMillis();
68          double speed = count * 1000.0 / (later - now);
69          logger.error(speed + " messages per second");
70      }
71  
72  }