1 package org.xmlBlaster.test.classtest;
  2 
  3 import org.xmlBlaster.util.Timestamp;
  4 import org.xmlBlaster.util.pool.PoolManager;
  5 import org.xmlBlaster.util.pool.ResourceWrapper;
  6 import org.xmlBlaster.util.pool.I_PoolManager;
  7 import org.xmlBlaster.util.XmlBlasterException;
  8 import java.util.*;
  9 import junit.framework.*;
 10 
 11 /**
 12  * Test org.xmlBlaster.util.pool.PoolManager
 13  * <p />
 14  * All methods starting with 'test' and without arguments are invoked automatically
 15  * <p />
 16  * java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.TestPoolManager
 17  * <p />
 18  * TODO: http://xmlunit.sourceforge.net/
 19  * @see org.xmlBlaster.util.pool.PoolManager
 20  */
 21 public class TestPoolManager extends TestCase {
 22    private final String ME = "TestPoolManager";
 23 
 24    public TestPoolManager(String name) {
 25       super(name);
 26    }
 27 
 28    protected void setUp() {
 29    }
 30 
 31    /**
 32     * This class is usually a UserSession object or a JDBC connection object
 33     * or whatever resource you want to handle
 34     */
 35    class TestResource {
 36       String name;
 37       String instanceId;
 38       boolean isBusy;
 39       boolean isErased = false;
 40       public TestResource(String name, String instanceId, boolean isBusy) {
 41          this.name = name;
 42          this.instanceId = instanceId;
 43          this.isBusy = isBusy;
 44       }
 45       public String toString() {
 46          return name;
 47       }
 48    }
 49 
 50    /**
 51     * This class does the resource pooling for TestResource,
 52     * with the help of PoolManager
 53     */
 54    class TestPool implements I_PoolManager {
 55       private int counter = 0;
 56       PoolManager poolManager;
 57       TestPool(int maxInstances, long busyToIdle, long idleToErase) {
 58          poolManager = new PoolManager(ME, this, maxInstances, busyToIdle, idleToErase);
 59       }
 60 
 61       // These four methods are callbacks from PoolManager ...
 62       public void idleToBusy(Object resource) {
 63          TestResource rr = (TestResource) resource;
 64          System.out.println("Entering idleToBusy(" + rr.name + ") ...");
 65          rr.isBusy = true; // you could do some re-initialization here ...
 66       }
 67       public void busyToIdle(Object resource) {
 68          TestResource rr = (TestResource) resource;
 69          System.out.println("Entering busyToIdle(" + rr.name + ") ...");
 70          rr.isBusy = false; // you could do some coding here ...
 71       }
 72       public Object toCreate(String instanceId) throws XmlBlasterException {
 73          TestResource rr = new TestResource("TestResource-" + (counter++), instanceId, true);
 74          System.out.println("Entering toCreate(instanceId='" + instanceId + "', " + rr.name + ") ...");
 75          return rr;
 76       }
 77       public void toErased(Object resource) {
 78          TestResource rr = (TestResource) resource;
 79          System.out.println("Entering toErased(" + rr.name + ") ...");
 80          rr.isErased = true;
 81       }
 82 
 83       // These methods are used by your application to get a recource ...
 84       TestResource reserve() {
 85          return reserve(PoolManager.GENERATE_RANDOM);
 86       }
 87       TestResource reserve(String instanceId) {
 88          System.out.println("Entering reserve(" + instanceId + ") ...");
 89          try {
 90             synchronized (this) {
 91                ResourceWrapper rw = (ResourceWrapper) poolManager.reserve(instanceId);
 92                TestResource rr = (TestResource) rw.getResource();
 93                if (rr == null)
 94                   fail("*****ERROR:rr==null");
 95                rr.instanceId = rw.getInstanceId(); // remember the generated unique id
 96                return rr;
 97             }
 98          }
 99          catch (XmlBlasterException e) {
100             System.err.println("*****WARNING:Caught exception in reserve(): " + e.getMessage());
101             return null;
102          }
103       }
104       void release(TestResource rr) {
105          System.out.println("Entering release() ...");
106          try {
107             synchronized (this) {
108                poolManager.release(rr.instanceId);
109             }
110          }
111          catch (XmlBlasterException e) {
112             System.err.println("*****WARNING:Caught exception in release(): " + e.getMessage());
113          }
114       }
115    }
116 
117    public void testWithGeneratedInstanceIdAndBusyTimeout() {
118       System.out.println("\n\n=========================\nStarting TEST 1 ...");
119       TestPool testPool = new TestPool(3, 2000, 0);
120       TestResource r0 = testPool.reserve();
121       TestResource r1 = testPool.reserve(PoolManager.GENERATE_RANDOM); // is default
122       TestResource r2 = testPool.reserve();
123       testPool.reserve();
124       if (testPool.poolManager.getNumBusy() != 3 || testPool.poolManager.getNumIdle() != 0)
125          fail("TEST 1.1 FAILED: Wrong number of busy/idle resources");
126       testPool.release(r0);
127       if (testPool.poolManager.getNumBusy() != 2 || testPool.poolManager.getNumIdle() != 1)
128          fail("TEST 1.2 FAILED: Wrong number of busy/idle resources");
129       testPool.reserve();
130       testPool.release(r2);
131       System.out.println(testPool.poolManager.toXml());
132 
133       // The resources are swapped to idle in 2 seconds, lets wait 3 seconds ...
134       try {
135          Thread.sleep(3000);
136       }
137       catch (InterruptedException i) {
138       }
139       System.out.println(testPool.poolManager.toXml());
140       if (testPool.poolManager.getNumBusy() != 0 || testPool.poolManager.getNumIdle() != 3)
141          fail("TEST 1.3 FAILED: Wrong number of busy/idle resources");
142       testPool.reserve();
143       if (testPool.poolManager.getNumBusy() != 1 || testPool.poolManager.getNumIdle() != 2)
144          fail("TEST 1.4 FAILED: Wrong number of busy/idle resources");
145       try {
146          Thread.sleep(1000);
147       }
148       catch (InterruptedException i) {
149       }
150       System.out.println(testPool.poolManager.toXml());
151    }
152 
153    public void testWithSuppliedInstanceIdAndBusyTimeout() {
154       System.out.println("\n\n=========================\nStarting TEST 2 ...");
155       TestPool testPool = new TestPool(3, 2000, 0);
156       TestResource r0 = testPool.reserve("ID-0");
157       TestResource r1 = testPool.reserve("ID-1");
158       TestResource r2 = testPool.reserve("ID-2");
159       r2 = testPool.reserve("ID-2");
160       r2 = testPool.reserve("ID-2");
161       r2 = testPool.reserve("ID-2");
162       if (testPool.poolManager.getNumBusy() != 3 || testPool.poolManager.getNumIdle() != 0)
163          fail("TEST 2.1 FAILED: Wrong number of busy/idle resources");
164       testPool.reserve("ID-3");
165       testPool.release(r0);
166       if (testPool.poolManager.getNumBusy() != 2 || testPool.poolManager.getNumIdle() != 1)
167          fail("TEST 2.2 FAILED: Wrong number of busy/idle resources");
168       testPool.reserve("ID-4");
169       testPool.release(r2);
170       System.out.println(testPool.poolManager.toXml());
171 
172 
173       // The resources are swapped to idle in 2 seconds, lets wait 3 seconds ...
174       try {
175          Thread.sleep(3000);
176       }
177       catch (InterruptedException i) {
178       }
179       System.out.println(testPool.poolManager.toXml());
180       if (testPool.poolManager.getNumBusy() != 0 || testPool.poolManager.getNumIdle() != 3)
181          fail("TEST 2.3 FAILED: Wrong number of busy/idle resources");
182       testPool.reserve("ID-5");
183       if (testPool.poolManager.getNumBusy() != 1 || testPool.poolManager.getNumIdle() != 2)
184          fail("TEST 2.4 FAILED: Wrong number of busy/idle resources");
185       testPool.poolManager.destroy();
186       System.out.println(testPool.poolManager.toXml());
187       if (testPool.poolManager.getNumBusy() != 0 || testPool.poolManager.getNumIdle() != 0)
188          fail("TEST 2.5 FAILED: Wrong number of busy/idle resources");
189       testPool.reserve("ID-6");
190       System.out.println(testPool.poolManager.toXml());
191       if (testPool.poolManager.getNumBusy() != 1 || testPool.poolManager.getNumIdle() != 0)
192          fail("TEST 2.6 FAILED: Wrong number of busy/idle resources");
193       try {
194          Thread.sleep(1000);
195       }
196       catch (InterruptedException i) {
197       }
198       System.out.println(testPool.poolManager.toXml());
199    }
200 
201    public void testWithSuppliedInstanceIdBusyTimeoutAndEraseTimeout() {
202       System.out.println("\n\n=========================\nStarting TEST 3 ...");
203       TestPool testPool = new TestPool(3, 2000, 3000); // erase resource after 3 sec in idle state
204       TestResource r0 = testPool.reserve("ID-0");
205       TestResource r1 = testPool.reserve("ID-1");
206       TestResource r2 = testPool.reserve("ID-2");
207       testPool.poolManager.erase("ID-2"); // erase from busy list
208       System.out.println(testPool.poolManager.toXml());
209       if (testPool.poolManager.getNumBusy() != 2 || testPool.poolManager.getNumIdle() != 0)
210          fail("TEST 3.1 FAILED: Wrong number of busy/idle resources");
211 
212       // The resources are swapped to idle in 2 seconds, lets wait 3 seconds ...
213       try {
214          Thread.sleep(3000);
215       }
216       catch (InterruptedException i) {
217       }
218       testPool.poolManager.erase("ID-1"); // erase from idle list
219       System.out.println(testPool.poolManager.toXml());
220       if (testPool.poolManager.getNumBusy() != 0 || testPool.poolManager.getNumIdle() != 1)
221          fail("TEST 3.2 FAILED: Wrong number of busy/idle resources");
222 
223       // The resources are erased after 3 seconds in idle state, lets wait 4 seconds ...
224       try {
225          Thread.sleep(4000);
226       }
227       catch (InterruptedException i) {
228       }
229       System.out.println(testPool.poolManager.toXml());
230       if (testPool.poolManager.getNumBusy() != 0 || testPool.poolManager.getNumIdle() != 0)
231          fail("TEST 3.3 FAILED: Wrong number of busy/idle resources");
232       try {
233          Thread.sleep(1000);
234       }
235       catch (InterruptedException i) {
236       }
237       System.out.println(testPool.poolManager.toXml());
238    }
239 
240    public void testMultiThreaded() {
241       System.out.println("\n\n=========================\nStarting TEST 4 ...");
242       class Test extends Thread {
243          String ME = "TestThread";
244          TestPool testPool;
245          Test(String name, TestPool testPool) {
246             this.ME = name;
247             this.testPool = testPool;
248          }
249          public void run() {
250             try {
251                System.out.println("\n\nStarted " + ME + " ...");
252                TestResource r0 = testPool.reserve(PoolManager.USE_OBJECT_REF);
253                TestResource r1 = testPool.reserve(PoolManager.USE_OBJECT_REF);
254                TestResource r2 = testPool.reserve(PoolManager.USE_OBJECT_REF);
255                TestResource r3 = testPool.reserve(PoolManager.USE_OBJECT_REF);
256                TestResource r4 = testPool.reserve(PoolManager.USE_OBJECT_REF);
257                Timestamp.sleep(20L);
258 
259                // multi thread access
260                testPool.poolManager.isBusy("unknown");
261                testPool.poolManager.isBusy("TestResource-1");
262                testPool.poolManager.busyRefresh("TestResource-1");
263                testPool.poolManager.busyRefresh("TestResource-1");
264                testPool.poolManager.busyRefresh("TestResource-1");
265                testPool.poolManager.getNumBusy();
266                testPool.poolManager.getNumIdle();
267                testPool.poolManager.getState();
268                testPool.poolManager.toXml();
269                testPool.poolManager.erase("" + r2); // the instanceId
270                r2 = testPool.reserve();
271                testPool.release(r2);
272                r2 = testPool.reserve();
273                testPool.release(r2);
274                testPool.release(r3);
275                System.out.println("\n\nFinished " + ME + " ...");
276                //System.out.println(testPool.poolManager.toXml());
277             }
278             catch (Throwable e) {
279                e.printStackTrace();
280                fail("\n++++++++++++++++++++++++\nTEST 4.1 FAILED: " + e.toString());
281             }
282          }
283       }
284       int numThreads = 6;
285       long busyToIdle = 5000L;
286       long idleToErase = 4000L;
287       TestPool testPool = new TestPool(60, busyToIdle, idleToErase);
288       for (int jj = 0; jj < 2; jj++) {
289          System.out.println("\n run # " + jj);
290          for (int ii = 0; ii < numThreads; ii++) {
291             String name = "TestThread-" + ii;
292             Test p = new Test(name, testPool);
293             p.setDaemon(true);
294             p.start();
295          }
296          Timestamp.sleep(2000L);
297          if (testPool.poolManager.getNumBusy() != 3 * numThreads)
298             fail("TEST 4.2 FAILED: Wrong number " + testPool.poolManager.getNumBusy() + " of busy resources");
299          if (jj == 0)
300             Timestamp.sleep(4000L); // now all busy resources are idle
301       }
302       Timestamp.sleep(1000L);
303       testPool.poolManager.destroy();
304       System.out.println(testPool.poolManager.toXml());
305       if (testPool.poolManager.getNumBusy() != 0 || testPool.poolManager.getNumIdle() != 0)
306          fail("TEST 4.3 FAILED: Wrong number of busy/idle resources");
307    }
308 
309    public void testDestroy() {
310       System.out.println("\n\n=========================\nStarting TEST 5 ...");
311       TestPool testPool = new TestPool(10, 2000, 3000); // erase resource after 3 sec in idle state
312       TestResource r0 = testPool.reserve(PoolManager.USE_HASH_CODE);
313       TestResource r1 = testPool.reserve(PoolManager.GENERATE_RANDOM);
314       TestResource r2 = testPool.reserve(PoolManager.USE_HASH_CODE);
315       TestResource r3 = testPool.reserve(PoolManager.USE_OBJECT_REF);
316       TestResource r4 = testPool.reserve();
317       testPool.release(r2);
318       r2 = testPool.reserve();
319       testPool.release(r2);
320       testPool.release(r3);
321       System.out.println(testPool.poolManager.toXml());
322       testPool.poolManager.destroy();
323       System.out.println(testPool.poolManager.toXml());
324       if (testPool.poolManager.getNumBusy() != 0 || testPool.poolManager.getNumIdle() != 0)
325          fail("TEST 5.1 FAILED: Wrong number of busy/idle resources");
326    }
327 }


syntax highlighted by Code2HTML, v. 0.9.1