KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > thread > DefaultThreadPoolTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.thread;
17
18 import org.apache.avalon.framework.logger.ConsoleLogger;
19 import org.easymock.MockControl;
20
21 /**
22  * The $classType$ class ...
23  *
24  * @author <a HREF="mailto:giacomo.at.apache.org">Giacomo Pati</a>
25  * @version $Id$
26  */

27 public class DefaultThreadPoolTestCase extends AbstractTestCase
28 {
29     //~ Methods ----------------------------------------------------------------
30

31     /**
32      * DOCUMENT ME!
33      */

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         // We cannot mock the DefaultThreadFactory as the underlying
41
// PooledExecutor of the DefaultThreadPool will again wrapp it into a
42
// PooledExecutor.Worker instance that does some bookeeping.
43
// Using a easymocked DefaultThreadFactory will prevent the
44
// PooledExecutor from shutting down and thus hangs forever.
45
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     /*
71      * Class under test for void execute(Runnable)
72      */

73     public final void testExecuteRunnable( )
74         throws InterruptedException JavaDoc
75     {
76         final MockControl runnableControl =
77             createStrictControl( Runnable JavaDoc.class );
78         final Runnable JavaDoc runnable = (Runnable JavaDoc)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         // We cannot mock the DefaultThreadFactory as the underlying
86
// PooledExecutor of the DefaultThreadPool will again wrapp it into a
87
// PooledExecutor.Worker instance that does some bookeeping.
88
// Using a easymocked DefaultThreadFactory will prevent the
89
// PooledExecutor from shutting down and thus hangs forever.
90
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     /**
106      * DOCUMENT ME!
107      *
108      * @throws InterruptedException DOCUMENT ME!
109      */

110     public final void testShutdown( )
111     throws InterruptedException JavaDoc
112     {
113         final Runnable JavaDoc runnable = new Runnable JavaDoc(){
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 JavaDoc 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