View Javadoc

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