1 package org.apache.jcs.utils.threadpool;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
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
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
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 }