View Javadoc

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