KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > pool > test > ResourceLimitingPoolMultithreadMaxStrictBlockTestCase


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.pool.test;
9
10 import com.clarkware.junitperf.ConstantTimer;
11 import com.clarkware.junitperf.LoadTest;
12 import com.clarkware.junitperf.TimedTest;
13 import com.clarkware.junitperf.Timer;
14
15 import junit.extensions.TestSetup;
16 import junit.framework.Test;
17 import junit.framework.TestCase;
18 import junit.framework.TestSuite;
19
20 import org.apache.avalon.excalibur.logger.BufferedLogger;
21 import org.apache.avalon.excalibur.pool.Poolable;
22 import org.apache.avalon.excalibur.pool.ResourceLimitingPool;
23
24 /**
25  * @author <a HREF="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
26  * @version CVS $Revision: 1.2 $ $Date: 2002/01/25 01:24:27 $
27  * @since 4.1
28  */

29 public final class ResourceLimitingPoolMultithreadMaxStrictBlockTestCase extends TestCase
30 {
31     private static BufferedLogger m_logger;
32     private static ClassInstanceObjectFactory m_factory;
33     private static ResourceLimitingPool m_pool;
34     
35     /*---------------------------------------------------------------
36      * Constructors
37      *-------------------------------------------------------------*/

38     public ResourceLimitingPoolMultithreadMaxStrictBlockTestCase()
39     {
40         this( "ResourceLimitingPool Multithreaded Max Size Strict Blocking Test Case" );
41     }
42
43     public ResourceLimitingPoolMultithreadMaxStrictBlockTestCase( final String JavaDoc name )
44     {
45         super( name );
46     }
47     
48     /*---------------------------------------------------------------
49      * Suite
50      *-------------------------------------------------------------*/

51     public static Test suite()
52     {
53         TestSuite suite = new TestSuite();
54         
55         Timer timer = new ConstantTimer( 100 );
56         int maxUsers = 10;
57         int iterations = 10;
58         long maxElapsedTime = 20000;
59         
60         Test testCase = new ResourceLimitingPoolMultithreadMaxStrictBlockTestCase( "testGetPut" );
61         Test loadTest = new LoadTest( testCase, maxUsers, iterations, timer );
62         Test timedTest = new TimedTest( loadTest, maxElapsedTime );
63         suite.addTest( timedTest );
64         
65         TestSetup wrapper= new TestSetup(suite)
66         {
67             public void setUp()
68             {
69                 oneTimeSetUp();
70             }
71             public void tearDown() throws Exception JavaDoc
72             {
73                 oneTimeTearDown();
74             }
75         };
76         
77         return wrapper;
78     }
79     
80     public static void oneTimeSetUp()
81     {
82         m_logger = new BufferedLogger();
83         m_factory = new ClassInstanceObjectFactory( PoolableTestObject.class, m_logger );
84         m_pool = new ResourceLimitingPool( m_factory, 3, true, true, 0, 0 );
85         
86         m_pool.enableLogging( m_logger );
87     }
88     
89     public static void oneTimeTearDown() throws Exception JavaDoc
90     {
91         // The timing of this test makes it so the pool should grow to 4 elements
92
assertEquals( "1) Pool Ready Size", 3, m_pool.getReadySize() );
93         assertEquals( "1) Pool Size", 3, m_pool.getSize() );
94         
95         // Make sure that each of the objects are uniqe by checking them all back out.
96
Poolable p1 = m_pool.get();
97         Poolable p2 = m_pool.get();
98         Poolable p3 = m_pool.get();
99         
100         assertEquals( "2) Pool Ready Size", 0, m_pool.getReadySize() );
101         assertEquals( "2) Pool Size", 3, m_pool.getSize() );
102         
103         assertTrue( "p1 != p2", p1 != p2 );
104         assertTrue( "p1 != p3", p1 != p3 );
105         assertTrue( "p2 != p3", p2 != p3 );
106         
107         m_pool.put( p1 );
108         m_pool.put( p2 );
109         m_pool.put( p3 );
110         
111         assertEquals( "3) Pool Ready Size", 3, m_pool.getReadySize() );
112         assertEquals( "3) Pool Size", 3, m_pool.getSize() );
113         
114         m_pool.dispose();
115         
116         assertEquals( "4) Pool Ready Size", 0, m_pool.getReadySize() );
117         assertEquals( "4) Pool Size", 0, m_pool.getSize() );
118     }
119     
120     /*---------------------------------------------------------------
121      * TestCases
122      *-------------------------------------------------------------*/

123     public void testGetPut() throws Exception JavaDoc
124     {
125         Poolable p = m_pool.get();
126         
127         // We can't check the exact pool size, but make sure that it never gets over 3
128
assertTrue( "Pool size <= 3", m_pool.getSize() <= 3 );
129         
130         try
131         {
132             Thread.sleep( 33 );
133         }
134         catch (InterruptedException JavaDoc e) {}
135         m_pool.put( p );
136     }
137 }
138
139
Popular Tags