View Javadoc
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 java.util.ArrayList;
23  import java.util.Properties;
24  import java.util.concurrent.ThreadPoolExecutor;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.commons.jcs.utils.props.PropertyLoader;
29  
30  /**
31   * Verify that the manager can create pools as intended by the default and
32   * specified file names.
33   *
34   * @author asmuts
35   */
36  public class ThreadPoolManagerUnitTest
37      extends TestCase
38  {
39  
40      /**
41       * Make sure it can load a default cache.ccf file
42       */
43      public void testDefaultConfig()
44      {
45          Properties props = PropertyLoader.loadProperties( "thread_pool.properties" );
46          ThreadPoolManager.setProps( props );
47          ThreadPoolManager mgr = ThreadPoolManager.getInstance();
48          assertNotNull( mgr );
49  
50          ThreadPoolExecutor pool = mgr.getPool( "test1" );
51          assertNotNull( pool );
52  
53          int poolSize = pool.getPoolSize();
54          int expectedPoolSize = Integer.parseInt( props.getProperty( "thread_pool.test1.startUpSize" ) );
55          assertEquals( poolSize, expectedPoolSize );
56  
57          // int qs = ((BoundedBuffer)pool.getQueue()).size();
58  
59          int max = pool.getMaximumPoolSize();
60  
61          int expected = Integer.parseInt( props.getProperty( "thread_pool.test1.maximumPoolSize" ) );
62          assertEquals(expected, max );
63      }
64  
65      /**
66       * Try to get an undefined pool from an existing default file.
67       */
68      public void testDefaultConfigUndefinedPool()
69      {
70          Properties props = PropertyLoader.loadProperties( "thread_pool.properties" );
71          ThreadPoolManager.setProps( props );
72          ThreadPoolManager mgr = ThreadPoolManager.getInstance();
73          assertNotNull( mgr );
74  
75          ThreadPoolExecutor pool = mgr.getPool( "doesnotexist" );
76          assertNotNull( pool );
77  
78          int max = pool.getMaximumPoolSize();
79  
80          int expected = Integer.parseInt( props.getProperty( "thread_pool.default.maximumPoolSize" ) );
81          assertEquals( expected, max );
82      }
83  
84      /**
85       * Get a couple pools by name and then see if they are in the list.
86       *
87       */
88      public void testGetPoolNames()
89      {
90          ThreadPoolManager mgr = ThreadPoolManager.getInstance();
91          assertNotNull( mgr );
92  
93          String poolName1 = "testGetPoolNames1";
94          mgr.getPool( poolName1 );
95  
96          String poolName2 = "testGetPoolNames2";
97          mgr.getPool( poolName2 );
98  
99          ArrayList<String> names = mgr.getPoolNames();
100         assertTrue( "Should have name in list.", names.contains( poolName1 ) );
101         assertTrue( "Should have name in list.", names.contains( poolName2 ) );
102     }
103 
104     /**
105      * Verify that if we specify not to use a buffer boundary that we get a
106      * linked queue.
107      *
108      */
109 //    public void testNoBoundary()
110 //    {
111 //        ThreadPoolManager.setPropsFileName( "thread_pool.properties" );
112 //        ThreadPoolManager mgr = ThreadPoolManager.getInstance();
113 //        assertNotNull( mgr );
114 //
115 //        ThreadPoolExecutor pool = mgr.getPool( "nobound" );
116 //        assertNotNull( "Should have gotten back a pool.", pool );
117 //
118 //        assertTrue( "Should have a linked queue and not a bounded buffer.", pool.getQueue() instanceof LinkedQueue );
119 //    }
120 
121     /**
122      * Verify that if we specify useBoundary=true that we get a BoundedBuffer.
123      *
124      */
125 //    public void testWithBoundary()
126 //    {
127 //        // SETUP
128 //        ThreadPoolManager.setPropsFileName( "thread_pool.properties" );
129 //        ThreadPoolManager mgr = ThreadPoolManager.getInstance();
130 //        assertNotNull( mgr );
131 //
132 //        // DO WORK
133 //        ThreadPoolExecutor pool = mgr.getPool( "withbound" );
134 //
135 //        // VERIFY
136 //        assertNotNull( "Should have gotten back a pool.", pool );
137 //        assertTrue( "Should have a BoundedBuffer and not a linked queue.", pool.getQueue() instanceof BoundedBuffer );
138 //    }
139 }