View Javadoc

1   /*
2    * $Id: CommonsPoolObjectPoolTestCase.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.util.pool;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.object.ObjectFactory;
15  import org.mule.config.PoolingProfile;
16  import org.mule.tck.testmodels.fruit.BananaFactory;
17  
18  import java.util.NoSuchElementException;
19  
20  public class CommonsPoolObjectPoolTestCase extends AbstractPoolingTestCase
21  {
22      public void testPoolExhaustedFail() throws Exception
23      {
24          ObjectPool pool = createPoolWithExhaustedAction(PoolingProfile.WHEN_EXHAUSTED_FAIL);
25          assertEquals(0, pool.getNumActive());
26                  
27          borrowObjectsUntilPoolIsFull(pool);
28          
29          // borrow one more, this must fail
30          try
31          {
32              pool.borrowObject();
33              fail("borrowing an object from a pool with policy WHEN_EXHAUSTED_FAIL must fail");
34          }
35          catch (NoSuchElementException nse)
36          {
37              // this one was expected
38          }
39      }
40      
41      public void testPoolExhaustedGrow() throws Exception
42      {
43          ObjectPool pool = createPoolWithExhaustedAction(PoolingProfile.WHEN_EXHAUSTED_GROW);
44          assertEquals(0, pool.getNumActive());
45          
46          borrowObjectsUntilPoolIsFull(pool);
47          
48          // borrow one more, this must make the pool grow
49          pool.borrowObject();
50          assertEquals(MAX_ACTIVE + 1, pool.getNumActive());
51      }
52      
53      public void testPoolExhaustedWait() throws Exception
54      {
55          ObjectPool pool = createPoolWithExhaustedAction(PoolingProfile.WHEN_EXHAUSTED_WAIT);
56          assertEquals(0, pool.getNumActive());
57          
58          borrowObjectsUntilPoolIsFull(pool);
59          
60          // borrow one more, this must make the pool grow
61          long before = System.currentTimeMillis();
62          try
63          {
64              pool.borrowObject();
65              fail("WHEN_EXHAUSTED_WAIT was specified but the pool returned an object");
66          }
67          catch (NoSuchElementException nse)
68          {
69              long delta = System.currentTimeMillis() - before;
70              assertTrue(delta >= MAX_WAIT);
71          }
72      }
73      
74      public void testInitPoolNone() throws Exception
75      {
76          ObjectPool pool = createPoolWithInitialisationPolicy(PoolingProfile.INITIALISE_NONE);
77          CountingObjectFactory objectFactory = (CountingObjectFactory) pool.getObjectFactory();
78          assertEquals(0, objectFactory.getInstanceCount());
79      }
80      
81      public void testInitPoolOne() throws Exception
82      {
83          ObjectPool pool = createPoolWithInitialisationPolicy(PoolingProfile.INITIALISE_ONE);
84          CountingObjectFactory objectFactory = (CountingObjectFactory) pool.getObjectFactory();
85          assertEquals(1, objectFactory.getInstanceCount());
86      }
87      
88      public void testInitPoolAll() throws Exception
89      {   
90          ObjectPool pool = createPoolWithInitialisationPolicy(PoolingProfile.INITIALISE_ALL);
91          CountingObjectFactory objectFactory = (CountingObjectFactory) pool.getObjectFactory();
92          assertEquals(MAX_ACTIVE, objectFactory.getInstanceCount());
93      }
94  
95      private ObjectPool createPoolWithExhaustedAction(int exhaustedAction) throws Exception
96      {
97          PoolingProfile poolingProfile = createDefaultPoolingProfile();
98          poolingProfile.setExhaustedAction(exhaustedAction);
99  
100         ObjectFactory objectFactory = new BananaFactory();
101         return createPool(poolingProfile, objectFactory);
102     }
103 
104     private ObjectPool createPoolWithInitialisationPolicy(int initPolicy) throws Exception
105     {
106         PoolingProfile poolingProfile = createDefaultPoolingProfile();
107         poolingProfile.setInitialisationPolicy(initPolicy);
108         
109         ObjectFactory objectFactory = new CountingObjectFactory();
110         return createPool(poolingProfile, objectFactory);
111     }
112     
113     private ObjectPool createPool(PoolingProfile poolingProfile, ObjectFactory objectFactory) throws Exception
114     {        
115         CommonsPoolObjectPool pool = new CommonsPoolObjectPool(objectFactory, poolingProfile, muleContext);
116         pool.initialise();
117         return pool;
118     }
119 
120     private void borrowObjectsUntilPoolIsFull(ObjectPool pool) throws Exception
121     {
122         for (int i = 1; i <= MAX_ACTIVE; i++)
123         {
124             pool.borrowObject();
125             assertEquals(i, pool.getNumActive());
126         }
127     }
128     
129     private static class CountingObjectFactory extends BananaFactory
130     {
131         private int instanceCount = 0;
132         
133         @Override
134         public Object getInstance(MuleContext muleContext) throws Exception
135         {
136             instanceCount++;
137             return super.getInstance(muleContext);
138         }
139 
140         public int getInstanceCount()
141         {
142             return instanceCount;
143         }
144     }
145 }