View Javadoc

1   /*
2    * $Id: AbstractPoolTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.tck.model;
12  
13  import org.mule.config.PoolingProfile;
14  import org.mule.impl.MuleDescriptor;
15  import org.mule.impl.model.seda.SedaModel;
16  import org.mule.tck.AbstractMuleTestCase;
17  import org.mule.tck.testmodels.fruit.Orange;
18  import org.mule.umo.UMODescriptor;
19  import org.mule.umo.lifecycle.InitialisationException;
20  import org.mule.umo.model.UMOPoolFactory;
21  import org.mule.util.ExceptionUtils;
22  import org.mule.util.ObjectPool;
23  
24  public abstract class AbstractPoolTestCase extends AbstractMuleTestCase
25  {
26      public static final byte FAIL_WHEN_EXHAUSTED = 0;
27      public static final byte GROW_WHEN_EXHAUSTED = 1;
28      public static final byte BLOCK_WHEN_EXHAUSTED = 2;
29  
30      public static final byte DEFAULT_POOL_SIZE = 3;
31      public static final long DEFAULT_WAIT = 1500;
32  
33      public AbstractPoolTestCase()
34      {
35          super();
36  
37      }
38  
39      // @Override
40      protected void doSetUp() throws Exception
41      {
42          // Initialise the manager
43          getManager(true);
44      }
45  
46      public void testCreatePool() throws Exception
47      {
48  
49          MuleDescriptor d = getTestDescriptor("orange", Orange.class.getName());
50          ObjectPool pool = createPool(d, FAIL_WHEN_EXHAUSTED);
51  
52          assertNotNull(pool);
53          assertEquals(0, pool.getSize());
54  
55          Object borrowed = pool.borrowObject();
56          assertNotNull(borrowed);
57          assertEquals(1, pool.getSize());
58          pool.returnObject(borrowed);
59  
60          borrowed = pool.borrowObject();
61          assertNotNull(borrowed);
62          assertEquals(1, pool.getSize());
63          Object borrowed2 = pool.borrowObject();
64          assertNotNull(borrowed2);
65          assertEquals(2, pool.getSize());
66      }
67  
68      public void testFailOnExhaust() throws Exception
69      {
70          ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), FAIL_WHEN_EXHAUSTED);
71          Object borrowed = null;
72  
73          for (int i = 0; i < pool.getMaxSize(); i++)
74          {
75              borrowed = pool.borrowObject();
76              assertNotNull(borrowed);
77              assertEquals(pool.getSize(), i + 1);
78          }
79  
80          try
81          {
82              borrowed = pool.borrowObject();
83              fail("Should have thrown an Exception");
84          }
85          catch (Exception e)
86          {
87              // expected
88          }
89      }
90  
91      public void testBlockExpiryOnExhaust() throws Exception
92      {
93          ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()),
94              BLOCK_WHEN_EXHAUSTED);
95          Object borrowed = null;
96  
97          assertEquals(0, pool.getSize());
98          borrowed = pool.borrowObject();
99          assertNotNull(borrowed);
100         borrowed = pool.borrowObject();
101         assertNotNull(borrowed);
102         borrowed = pool.borrowObject();
103         assertNotNull(borrowed);
104         assertEquals(3, pool.getSize());
105 
106         try
107         {
108             borrowed = pool.borrowObject();
109             fail("Should have thrown an Exception");
110         }
111         catch (Exception e)
112         {
113             // expected
114         }
115     }
116 
117     public void testBlockOnExhaust() throws Exception
118     {
119         ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()),
120             BLOCK_WHEN_EXHAUSTED);
121         Object borrowed = null;
122 
123         assertEquals(0, pool.getSize());
124 
125         borrowed = pool.borrowObject();
126         borrowed = pool.borrowObject();
127         assertEquals(2, pool.getSize());
128 
129         long borrowerWait = 500;
130         Borrower borrower = new Borrower(pool, borrowerWait);
131         borrower.start();
132 
133         // Make sure the borrower borrows first
134         Thread.sleep(200);
135 
136         borrowed = pool.borrowObject();
137         assertNotNull(borrowed);
138     }
139 
140     public void testGrowOnExhaust() throws Exception
141     {
142         ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), GROW_WHEN_EXHAUSTED);
143 
144         Object borrowed = pool.borrowObject();
145         borrowed = pool.borrowObject();
146         borrowed = pool.borrowObject();
147         assertEquals(3, pool.getSize());
148         assertEquals(3, pool.getMaxSize());
149 
150         // Should now grow
151         borrowed = pool.borrowObject();
152         assertNotNull(borrowed);
153 
154         assertEquals(4, pool.getSize());
155     }
156 
157     public void testClearPool() throws Exception
158     {
159         ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), FAIL_WHEN_EXHAUSTED);
160 
161         Object borrowed = pool.borrowObject();
162         assertEquals(1, pool.getSize());
163         pool.returnObject(borrowed);
164 
165         pool.clearPool();
166         assertEquals(0, pool.getSize());
167 
168         borrowed = pool.borrowObject();
169         assertEquals(1, pool.getSize());
170     }
171 
172     public void testCreateFromFactory() throws Exception
173     {
174         UMODescriptor descriptor = getTestDescriptor("orange", Orange.class.getName());
175         UMOPoolFactory factory = getPoolFactory();
176         ObjectPool pool = factory.createPool(descriptor, new SedaModel(), new PoolingProfile());
177         assertNotNull(pool);
178     }
179 
180     public void testPoolLifecycle() throws Exception
181     {
182         MuleDescriptor d = getTestDescriptor("orange", Orange.class.getName());
183         ObjectPool pool = createPool(d, FAIL_WHEN_EXHAUSTED);
184 
185         assertNotNull(pool);
186         assertEquals(0, pool.getSize());
187     }
188 
189     private class Borrower extends Thread
190     {
191         private ObjectPool pool;
192         private long time;
193 
194         public Borrower(ObjectPool pool, long time)
195         {
196             super("Borrower");
197             if (pool == null)
198             {
199                 throw new IllegalArgumentException("Pool cannot be null");
200             }
201             this.pool = pool;
202             if (time < 500)
203             {
204                 time = 500;
205             }
206             this.time = time;
207         }
208 
209         /*
210          * (non-Javadoc)
211          * 
212          * @see java.lang.Runnable#run()
213          */
214         public void run()
215         {
216             try
217             {
218                 Object object = pool.borrowObject();
219                 sleep(time);
220                 pool.returnObject(object);
221             }
222             catch (Exception e)
223             {
224                 fail("Borrower thread failed:\n" + ExceptionUtils.getStackTrace(e));
225             }
226         }
227 
228     }
229 
230     public abstract ObjectPool createPool(MuleDescriptor descriptor, byte action)
231         throws InitialisationException;
232 
233     public abstract UMOPoolFactory getPoolFactory();
234 }