View Javadoc

1   /*
2    * $Id: DispatcherPoolTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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;
12  
13  import org.mule.api.config.ThreadingProfile;
14  import org.mule.api.endpoint.OutboundEndpoint;
15  import org.mule.api.transport.MessageDispatcher;
16  import org.mule.config.ImmutableThreadingProfile;
17  import org.mule.tck.AbstractMuleTestCase;
18  import org.mule.tck.testmodels.mule.TestConnector;
19  
20  import org.apache.commons.pool.impl.GenericKeyedObjectPool;
21  
22  public class DispatcherPoolTestCase extends AbstractMuleTestCase
23  {
24  
25      public void testDefaultDispatcherPoolConfiguration() throws Exception
26      {
27          final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_RUN);
28  
29          // ThreadingProfile exhausted action default is RUN
30          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_RUN, connector.getDispatcherThreadingProfile()
31              .getPoolExhaustedAction());
32          assertEquals(2, connector.dispatchers.getMaxActive());
33          // This must equal maxActive dispatchers because low maxIdle would result in
34          // a lot of dispatcher churn
35          assertEquals(2, connector.dispatchers.getMaxIdle());
36          assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK,
37              connector.dispatchers.getWhenExhaustedAction());
38          assertEquals(-1, connector.dispatchers.getMaxWait());
39      }
40  
41      public void testDefaultDispatcherPoolConfigurationThreadingProfileWait() throws Exception
42      {
43          final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
44  
45          assertEquals(ThreadingProfile.WHEN_EXHAUSTED_WAIT, connector.getDispatcherThreadingProfile()
46              .getPoolExhaustedAction());
47          assertEquals(1, connector.dispatchers.getMaxActive());
48          assertEquals(1, connector.dispatchers.getMaxIdle());
49          assertEquals(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK,
50              connector.dispatchers.getWhenExhaustedAction());
51          assertEquals(-1, connector.dispatchers.getMaxWait());
52      }
53  
54      public void testDispatcherPoolDefaultBlockExhaustedAction() throws Exception
55      {
56          final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
57          connector.setDispatcherPoolMaxWait(100);
58  
59          assertEquals(1, connector.dispatchers.getMaxActive());
60          assertEquals(100, connector.dispatchers.getMaxWait());
61  
62          final OutboundEndpoint endpoint = getTestOutboundEndpoint("test", "test://test");
63  
64          new Thread(new Runnable()
65          {
66              public void run()
67              {
68                  try
69                  {
70                      MessageDispatcher messageDispatcher = (MessageDispatcher) connector.dispatchers.borrowObject(endpoint);
71                      Thread.sleep(50);
72                      connector.dispatchers.returnObject(endpoint, messageDispatcher);
73                  }
74                  catch (Exception e)
75                  {
76                      e.printStackTrace();
77                  }
78  
79              }
80          }).start();
81          Thread.sleep(10);
82          assertEquals(1, connector.dispatchers.getNumActive());
83          connector.dispatchers.borrowObject(endpoint);
84          assertEquals(1, connector.dispatchers.getNumActive());
85  
86      }
87  
88      public void testDispatcherPoolBlockTimeoutExhaustedAction() throws Exception
89      {
90          final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
91          connector.setDispatcherPoolMaxWait(10);
92  
93          assertEquals(1, connector.dispatchers.getMaxActive());
94          assertEquals(10, connector.dispatchers.getMaxWait());
95  
96          final OutboundEndpoint endpoint = getTestOutboundEndpoint("test", "test://test");
97  
98          new Thread(new Runnable()
99          {
100             public void run()
101             {
102                 try
103                 {
104                     MessageDispatcher messageDispatcher = (MessageDispatcher) connector.dispatchers.borrowObject(endpoint);
105                     Thread.sleep(200);
106                     connector.dispatchers.returnObject(endpoint, messageDispatcher);
107                 }
108                 catch (Exception e)
109                 {
110                     e.printStackTrace();
111                 }
112 
113             }
114         }).start();
115         Thread.sleep(10);
116         assertEquals(1, connector.dispatchers.getNumActive());
117         try
118         {
119             connector.dispatchers.borrowObject(endpoint);
120             fail("Exception expected");
121         }
122         catch (Exception e)
123         {
124             assertEquals(1, connector.dispatchers.getNumActive());
125         }
126     }
127 
128     public void testDispatcherPoolGrowExhaustedAction() throws Exception
129     {
130         final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
131         connector.setDispatcherPoolWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW);
132 
133         assertEquals(1, connector.dispatchers.getMaxActive());
134 
135         final OutboundEndpoint endpoint = getTestOutboundEndpoint("test", "test://test");
136 
137         connector.dispatchers.borrowObject(endpoint);
138         connector.dispatchers.borrowObject(endpoint);
139         assertEquals(2, connector.dispatchers.getNumActive());
140 
141     }
142 
143     public void testDispatcherPoolFailExhaustedAction() throws Exception
144     {
145         final TestConnector connector = createConnectorWithSingleObjectDispatcherPool(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
146         connector.setDispatcherPoolWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
147 
148         assertEquals(1, connector.dispatchers.getMaxActive());
149 
150         final OutboundEndpoint endpoint = getTestOutboundEndpoint("test", "test://test");
151 
152         connector.dispatchers.borrowObject(endpoint);
153         try
154         {
155             connector.dispatchers.borrowObject(endpoint);
156             fail("Exception expected");
157         }
158         catch (Exception e)
159         {
160             assertEquals(1, connector.dispatchers.getNumActive());
161         }
162     }
163 
164     private TestConnector createConnectorWithSingleObjectDispatcherPool(int exhaustedAction) throws Exception
165     {
166         TestConnector connector = new TestConnector(muleContext);
167         ThreadingProfile threadingProfile = new ImmutableThreadingProfile(1, 1, 1, 1, 1, exhaustedAction,
168             true, null, null);
169         connector.setDispatcherThreadingProfile(threadingProfile);
170         connector.createReceiver(getTestService(), getTestInboundEndpoint("test", "test://test"));
171         muleContext.getRegistry().registerConnector(connector);
172         return connector;
173     }
174 
175 }