KickJava   Java API By Example, From Geeks To Geeks.

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


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 ResourceLimitingPoolMultithreadTestCase
30     extends TestCase
31 {
32     private static BufferedLogger m_logger;
33     private static ClassInstanceObjectFactory m_factory;
34     private static ResourceLimitingPool m_pool;
35     
36     /*---------------------------------------------------------------
37      * Constructors
38      *-------------------------------------------------------------*/

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

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

129     public void testGetPut() throws Exception JavaDoc
130     {
131         Poolable p = m_pool.get();
132         try
133         {
134             Thread.sleep(33);
135         }
136         catch ( InterruptedException JavaDoc e ) {}
137         m_pool.put( p );
138     }
139 }
140
141
Popular Tags