1 16 package org.apache.cocoon.components.thread; 17 18 import org.apache.avalon.framework.logger.ConsoleLogger; 19 import org.easymock.MockControl; 20 21 27 public class DefaultThreadPoolTestCase extends AbstractTestCase 28 { 29 31 34 public final void testDefaultThreadPool( ) 35 { 36 final DefaultThreadPool pool = new DefaultThreadPool( ); 37 pool.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) ); 38 pool.setName( "mypool" ); 39 40 final ThreadFactory threadFactory = new DefaultThreadFactory(); 46 threadFactory.setPriority( Thread.MAX_PRIORITY ); 47 pool.setThreadFactory( threadFactory ); 48 pool.setQueue( 230 ); 49 pool.setMaximumPoolSize( 15 ); 50 pool.setMinimumPoolSize( 9 ); 51 pool.setKeepAliveTime( 11000 ); 52 pool.setBlockPolicy( "ABORT" ); 53 pool.setShutdownGraceful( false ); 54 pool.setShutdownWaitTimeMs( 12345 ); 55 56 assertEquals( "block-policy", "ABORT", pool.getBlockPolicy( ) ); 57 assertEquals( "keep-alive-time-ms", 11000L, pool.getKeepAliveTime( ) ); 58 assertEquals( "max-queueu-size", 230, pool.getMaximumQueueSize( ) ); 59 assertEquals( "max-pool-size", 15, pool.getMaximumPoolSize( ) ); 60 assertEquals( "min-pool-size", 9, pool.getMinimumPoolSize( ) ); 61 assertEquals( "name", "mypool", pool.getName( ) ); 62 assertEquals( "priority", Thread.MAX_PRIORITY, pool.getPriority( ) ); 63 assertEquals( "queue-size", 0, pool.getQueueSize( ) ); 64 assertEquals( "isQueued", true, pool.isQueued( ) ); 65 assertEquals( "isTerminatedAfterShutdown", false, 66 pool.isTerminatedAfterShutdown( ) ); 67 verify( ); 68 } 69 70 73 public final void testExecuteRunnable( ) 74 throws InterruptedException 75 { 76 final MockControl runnableControl = 77 createStrictControl( Runnable .class ); 78 final Runnable runnable = (Runnable )runnableControl.getMock( ); 79 runnable.run( ); 80 runnableControl.replay( ); 81 82 final DefaultThreadPool pool = new DefaultThreadPool( ); 83 pool.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) ); 84 pool.setName( "mypool" ); 85 pool.setThreadFactory( new DefaultThreadFactory() ); 91 pool.setQueue( 230 ); 92 pool.setMaximumPoolSize( 15 ); 93 pool.setMinimumPoolSize( 9 ); 94 pool.setKeepAliveTime( 100 ); 95 pool.setBlockPolicy( "ABORT" ); 96 pool.setShutdownGraceful( false ); 97 pool.setShutdownWaitTimeMs( 1234 ); 98 pool.execute( runnable ); 99 Thread.yield( ); 100 Thread.sleep( 100 ); 101 pool.shutdown(); 102 verify( ); 103 } 104 105 110 public final void testShutdown( ) 111 throws InterruptedException 112 { 113 final Runnable runnable = new Runnable (){ 114 public void run() 115 { 116 final ConsoleLogger logger = new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ); 117 logger.info( "runnable runs" ); 118 try 119 { 120 Thread.sleep( 1000 ); 121 } 122 catch( final InterruptedException ie ) 123 { 124 logger.info( "runnable has been interrupted "); 125 } 126 logger.info( "runnable terminated" ); 127 } 128 }; 129 130 final DefaultThreadPool pool = new DefaultThreadPool( ); 131 pool.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) ); 132 pool.setName( "mypool" ); 133 pool.setThreadFactory( new DefaultThreadFactory() ); 134 pool.setQueue( 0 ); 135 pool.setMaximumPoolSize( 15 ); 136 pool.setMinimumPoolSize( 9 ); 137 pool.setKeepAliveTime( 1000 ); 138 pool.setBlockPolicy( "ABORT" ); 139 pool.setShutdownGraceful( true ); 140 pool.setShutdownWaitTimeMs( 100 ); 141 pool.execute( runnable ); 142 pool.execute( runnable ); 143 Thread.yield(); 144 Thread.sleep( 200 ); 145 pool.shutdown( ); 146 Thread.sleep( 200 ); 147 verify( ); 148 } 149 } 150 | Popular Tags |