View Javadoc

1   /*
2    * $Id: TcpFunctionalTestCase.java 22455 2011-07-19 16:16:29Z justin.calleja $
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.tcp;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import java.util.Arrays;
17  import java.util.Collection;
18  
19  import org.junit.Rule;
20  import org.junit.Test;
21  import org.junit.runners.Parameterized.Parameters;
22  import org.mule.api.MuleMessage;
23  import org.mule.module.client.MuleClient;
24  import org.mule.tck.AbstractServiceAndFlowTestCase;
25  import org.mule.tck.junit4.rule.DynamicPort;
26  
27  public class TcpFunctionalTestCase extends AbstractServiceAndFlowTestCase 
28  {
29  
30      protected static String TEST_MESSAGE = "Test TCP Request";
31  
32      @Rule
33      public DynamicPort dynamicPort1 = new DynamicPort("port1");
34  
35      @Rule
36      public DynamicPort dynamicPort2 = new DynamicPort("port2");
37  
38      public TcpFunctionalTestCase(ConfigVariant variant, String configResources)
39      {
40          super(variant, configResources);
41      }
42          
43      @Parameters
44      public static Collection<Object[]> parameters()
45      {
46          return Arrays.asList(new Object[][]{
47              {ConfigVariant.SERVICE, "tcp-functional-test-service.xml"},
48              {ConfigVariant.FLOW, "tcp-functional-test-flow.xml"}
49          });
50      }
51  
52      @Test
53      public void testSend() throws Exception
54      {
55          MuleClient client = new MuleClient(muleContext);
56          MuleMessage result = client.send("clientEndpoint", TEST_MESSAGE, null);
57          assertEquals(TEST_MESSAGE + " Received", result.getPayloadAsString());
58      }
59  
60      @Test
61      public void testDispatchAndReply() throws Exception
62      {
63          MuleClient client = new MuleClient(muleContext);
64          client.dispatch("asyncClientEndpoint", TEST_MESSAGE, null);
65          // MULE-2754
66          Thread.sleep(100);
67          MuleMessage result =  client.request("asyncClientEndpoint", 10000);
68          assertNotNull(result);
69          assertEquals(TEST_MESSAGE + " Received Async", result.getPayloadAsString());
70      }
71  
72      public void timeMultipleSend() throws Exception
73      {
74          MuleClient client = new MuleClient(muleContext);
75          long now = System.currentTimeMillis();
76          int count = 1000;
77          for (int i = 0; i < count; i++)
78          {
79              MuleMessage result = client.send("clientEndpoint", TEST_MESSAGE, null);
80              assertEquals(TEST_MESSAGE + " Received", result.getPayloadAsString());
81          }
82          long later = System.currentTimeMillis();
83          double speed = count * 1000.0 / (later - now);
84          logger.error(speed + " messages per second");
85      }
86  
87  }