1 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 29 public final class ResourceLimitingPoolMultithreadMaxStrictTestCase extends TestCase 30 { 31 private static BufferedLogger m_logger; 32 private static ClassInstanceObjectFactory m_factory; 33 private static ResourceLimitingPool m_pool; 34 35 38 public ResourceLimitingPoolMultithreadMaxStrictTestCase() 39 { 40 this( "ResourceLimitingPool Multithreaded Max Size Strict Test Case" ); 41 } 42 43 public ResourceLimitingPoolMultithreadMaxStrictTestCase( final String name ) 44 { 45 super( name ); 46 } 47 48 51 public static Test suite() 52 { 53 TestSuite suite = new TestSuite(); 54 55 Timer timer = new ConstantTimer( 100 ); 56 int maxUsers = 50; 57 int iterations = 10; 58 long maxElapsedTime = 20000; 59 60 Test testCase = new ResourceLimitingPoolMultithreadMaxStrictTestCase( "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 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, false, 0, 0 ); 85 86 m_pool.enableLogging( m_logger ); 87 } 88 89 public static void oneTimeTearDown() throws Exception 90 { 91 assertEquals( "1) Pool Ready Size", 3, m_pool.getReadySize() ); 93 assertEquals( "1) Pool Size", 3, m_pool.getSize() ); 94 95 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 123 public void testGetPut() throws Exception 124 { 125 Poolable p; 126 try 127 { 128 p = m_pool.get(); 129 } 130 catch ( Exception e ) 131 { 132 assertEquals( "Exception calling get()", 134 "Could not create enough Components to service your request.", 135 e.getMessage() ); 136 return; 137 } 138 139 assertTrue( "Pool size <= 3", m_pool.getSize() <= 3 ); 141 142 try 143 { 144 Thread.sleep( 33 ); 145 } catch ( InterruptedException e ) {} 146 m_pool.put( p ); 147 } 148 } 149 150 | Popular Tags |