1   package org.apache.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  /***
25   * This test is experiemental. I'm trying to find out if the max size setting
26   * will result in the removal of threads.
27   *
28   * @author Aaron Smuts
29   *
30   */
31  public class ThreadPoolUnitTest
32      extends TestCase
33  {
34  
35      /***
36       * Make sure that the max size setting takes effect before the idle
37       * time is reached.
38       *
39       * We just want to ensure that we can adjust the max size of an active pool.
40       *
41       * http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/PooledExecutor.html#setMaximumPoolSize(int)
42       *
43       * @throws Exception
44       */
45      public void testMaxReduction()
46          throws Exception
47      {
48          //ThreadPoolManager.setPropsFileName( "thread_pool_test.properties" );
49          ThreadPool pool = ThreadPoolManager.getInstance().getPool( "maxtest" );
50  
51          System.out.println( "pool = " + pool );
52  
53          pool.getPool().setMaximumPoolSize( 5 );
54  
55          System.out.println( "current size before execute = " + pool.getPool().getPoolSize() );
56  
57  
58          // add 6
59          for ( int i = 1; i < 30; i++ )
60          {
61              final ThreadPool myPool = pool;
62              final int cnt = i;
63              pool.execute( new Runnable()
64              {
65  
66                  public void run()
67                  {
68                      try
69                      {
70                          //System.out.println( cnt );
71                          System.out.println( "count = " + cnt + " before sleep current size = " + myPool.getPool().getPoolSize() );
72                          Thread.sleep( 200 / cnt );
73                          System.out.println( "count = " + cnt + " after sleep current size = " + myPool.getPool().getPoolSize() );
74                      }
75                      catch ( InterruptedException e )
76                      {
77                          // TODO Auto-generated catch block
78                          e.printStackTrace();
79                      }
80                  }
81  
82              } );
83  
84          }
85  
86          System.out.println( "current size = " + pool.getPool().getPoolSize() );
87  
88          pool.getPool().setMaximumPoolSize( 4 );
89  
90          //Thread.sleep( 200 );
91  
92          System.out.println( "current size after set size to 4= " + pool.getPool().getPoolSize() );
93  
94          Thread.sleep( 200 );
95  
96          System.out.println( "current size again after sleep = " + pool.getPool().getPoolSize() );
97  
98          assertEquals( "Pool size should have been reduced.", 4, pool.getPool().getPoolSize() );
99      }
100 
101 }