1 17 package org.apache.avalon.excalibur.thread.impl.test; 18 19 import junit.framework.TestCase; 20 21 import org.apache.avalon.excalibur.thread.impl.ResourceLimitingThreadPool; 22 23 28 public final class ResourceLimitingThreadPoolTestCase 29 extends TestCase 30 { 31 private volatile int m_completeCount; 32 33 36 public ResourceLimitingThreadPoolTestCase() 37 { 38 this( "ResourceLimitingThreadPool Test Case" ); 39 } 40 41 public ResourceLimitingThreadPoolTestCase( final String name ) 42 { 43 super( name ); 44 } 45 46 49 public void test1Worker1Task() 50 { 51 commonTest( 1, 1, 0L, 200L, 1, true, true, -1, -1 ); 52 } 53 54 public void test1Worker5Tasks() 55 { 56 commonTest( 5, 1, 800L, 1000L, 1, true, true, -1, -1 ); 58 } 59 60 public void test5Workers10Tasks() 61 { 62 commonTest( 10, 5, 200L, 400L, 5, true, true, -1, -1 ); 64 } 65 66 public void test10Workers100Tasks() 67 { 68 commonTest( 100, 10, 1800L, 2000L, 10, true, true, -1, -1 ); 71 } 72 73 public void test5Workers6TasksNoBlock() 74 { 75 commonTest( 6, 5, 0L, 200L, 5, true, false, -1, -1 ); 76 } 77 78 public void test5Workers10TasksNotStrict() 79 { 80 commonTest( 10, 10, 0L, 200L, 5, false, false, -1, -1 ); 81 } 82 83 protected void incCompleteCount() 84 { 85 synchronized( this ) 86 { 87 m_completeCount++; 88 } 89 } 90 91 private void commonTest( int taskCount, 92 int firstSize, 93 long firstTime, 94 long totalTime, 95 int max, 96 boolean maxStrict, 97 boolean blocking, 98 long blockTimeout, 99 long trimInterval ) 100 { 101 BufferedLogger logger = new BufferedLogger(); 102 ResourceLimitingThreadPool pool = new ResourceLimitingThreadPool( 103 "Test Worker Pool", max, maxStrict, blocking, blockTimeout, trimInterval ); 104 pool.enableLogging( logger ); 105 106 Runnable runner = new Runnable () 107 { 108 public void run() 109 { 110 try 111 { 112 Thread.sleep( 200 ); 113 } 114 catch( InterruptedException e ) 115 { 116 } 117 118 incCompleteCount(); 119 } 120 }; 121 122 long start = System.currentTimeMillis(); 123 m_completeCount = 0; 124 for( int i = 0; i < taskCount; i++ ) 125 { 126 if( maxStrict && ( !blocking ) && i >= max ) 127 { 128 try 130 { 131 pool.execute( runner ); 132 fail( "Should have failed when requesting more than max resources." ); 133 } 134 catch( Exception e ) 135 { 136 incCompleteCount(); 138 } 139 } 140 else 141 { 142 pool.execute( runner ); 143 } 144 } 145 long dur = System.currentTimeMillis() - start; 146 147 assertEquals( "The pool size was not what it should be.", firstSize, pool.getSize() ); 149 150 if( Math.abs( dur - firstTime ) > 500 ) 153 { 154 fail( "Time to start all tasks, " + dur + 155 ", was not within 500ms of the expected time, " + firstTime ); 156 } 157 158 while( m_completeCount < taskCount ) 160 { 161 try 162 { 163 Thread.sleep( 10 ); 164 } 165 catch( InterruptedException e ) 166 { 167 } 168 } 169 170 dur = System.currentTimeMillis() - start; 171 172 if( Math.abs( dur - totalTime ) > 500 ) 175 { 176 fail( "Time to complete all tasks, " + dur + 177 ", was not within 500ms of the expected time, " + totalTime ); 178 } 179 180 } 182 } 183 184 | Popular Tags |