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.ResponseOutputStream;
10  import org.mule.api.client.MuleClient;
11  import org.mule.api.transport.DispatchException;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.junit4.rule.DynamicPort;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.io.OutputStream;
18  import java.net.Socket;
19  
20  import org.junit.Rule;
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.fail;
26  
27  public class TcpSocketsPoolTestCase extends FunctionalTestCase
28  {
29  
30      protected static String TEST_MESSAGE = "Test TCP Request";
31  
32      @Rule
33      public DynamicPort dynamicPort1 = new DynamicPort("port1");
34  
35  
36      @Override
37      protected String getConfigResources()
38      {
39          return "tcp-sockets-pool-test.xml";
40      }
41  
42      @Test
43      public void testExceptionInSendReleasesSocket() throws Exception
44      {
45          TcpConnector tcpConnector = (TcpConnector) muleContext.getRegistry().lookupConnector("connectorWithException");
46          assertNotNull(tcpConnector);
47          MuleClient client = muleContext.getClient();
48          try
49          {
50              client.send("clientWithExceptionEndpoint", TEST_MESSAGE, null);
51              fail("Dispatch exception was expected");
52          }
53          catch(DispatchException e)
54          {
55              // Expected exception
56          }
57          assertEquals(0, tcpConnector.getSocketsPoolNumActive());
58      }
59  
60      @Test
61      public void testSocketsPoolSettings() throws Exception
62      {
63          TcpConnector tcpConnector = (TcpConnector) muleContext.getRegistry().lookupConnector("connectorWithException");
64          assertEquals(8, tcpConnector.getSocketsPoolMaxActive());
65          assertEquals(1, tcpConnector.getSocketsPoolMaxIdle());
66          assertEquals(3000, tcpConnector.getSocketsPoolMaxWait());
67      }
68  
69      @Test
70      public void testSocketsPoolDefaultSettings() throws Exception
71      {
72          TcpConnector tcpConnector = (TcpConnector) muleContext.getRegistry().lookupConnector("tcpConnector");
73          int maxActive = tcpConnector.getDispatcherThreadingProfile().getMaxThreadsActive();
74          int maxIdle = tcpConnector.getDispatcherThreadingProfile().getMaxThreadsIdle();
75          assertEquals(maxActive, tcpConnector.getSocketsPoolMaxActive());
76          assertEquals(maxIdle, tcpConnector.getSocketsPoolMaxIdle());
77          assertEquals(TcpConnector.DEFAULT_WAIT_TIMEOUT, tcpConnector.getSocketMaxWait());
78      }
79  
80      public static class MockTcpProtocol implements TcpProtocol
81      {
82          public ResponseOutputStream createResponse(Socket socket) throws IOException
83          {
84              throw new UnsupportedOperationException("createResponse");
85          }
86  
87          public Object read(InputStream is) throws IOException
88          {
89              throw new UnsupportedOperationException("read");
90          }
91  
92          public void write(OutputStream os, Object data) throws IOException
93          {
94              throw new UnsupportedOperationException("write");
95          }
96      }
97  
98  }