1 package org.apache.commons.jcs.utils.threadpool;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import junit.framework.TestCase;
23
24 import java.util.concurrent.ThreadPoolExecutor;
25
26 /**
27 * This test is experimental. I'm trying to find out if the max size setting will result in the
28 * removal of threads.
29 * <p>
30 * @author Aaron Smuts
31 */
32 public class ThreadPoolUnitTest
33 extends TestCase
34 {
35 /**
36 * Make sure that the max size setting takes effect before the idle time is reached.
37 * <p>
38 * We just want to ensure that we can adjust the max size of an active pool.
39 * <p>
40 * http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/PooledExecutor.html#setMaximumPoolSize(int)
41 * @throws Exception
42 */
43 public void testMaxReduction()
44 throws Exception
45 {
46 //ThreadPoolManager.setPropsFileName( "thread_pool_test.properties" );
47 ThreadPoolExecutor pool = ThreadPoolManager.getInstance().getPool( "maxtest" );
48
49 //System.out.println( "pool = " + pool );
50 pool.setMaximumPoolSize( 5 );
51 //System.out.println( "current size before execute = " + pool.getPool().getPoolSize() );
52
53 // add 6
54 for ( int i = 1; i < 30; i++ )
55 {
56 final int cnt = i;
57 pool.execute( new Runnable()
58 {
59
60 @Override
61 public void run()
62 {
63 try
64 {
65 //System.out.println( cnt );
66 // System.out.println( "count = " + cnt + " before sleep current size = " + myPool.getPoolSize() );
67 Thread.sleep( 200 / cnt );
68 //System.out.println( "count = " + cnt + " after sleep current size = " + myPool.getPool().getPoolSize() );
69 }
70 catch ( InterruptedException e )
71 {
72 // TODO Auto-generated catch block
73 e.printStackTrace();
74 }
75 }
76 } );
77 }
78
79 //System.out.println( "current size = " + pool.getPool().getPoolSize() );
80 pool.setMaximumPoolSize( 4 );
81 //Thread.sleep( 200 );
82 //System.out.println( "current size after set size to 4= " + pool.getPool().getPoolSize() );
83 Thread.sleep( 200 );
84 //System.out.println( "current size again after sleep = " + pool.getPool().getPoolSize() );
85 assertEquals( "Pool size should have been reduced.", 4, pool.getPoolSize() );
86 }
87 }