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 java.util.ArrayList;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.jcs.utils.props.PropertyLoader;
27  
28  import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
29  import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
30  
31  /***
32   * Verify that the manager can create pools as intended by the default and
33   * specified file names.
34   *
35   * @author asmuts
36   */
37  public class ThreadPoolManagerUnitTest
38      extends TestCase
39  {
40  
41      /***
42       * Make sure it can load a default cache.ccf file
43       */
44      public void testDefaultConfig()
45      {
46          ThreadPoolManager.setPropsFileName( "thread_pool.properties" );
47          ThreadPoolManager mgr = ThreadPoolManager.getInstance();
48          assertNotNull( mgr );
49  
50          ThreadPool pool = mgr.getPool( "test1" );
51          assertNotNull( pool );
52  
53          int poolSize = pool.getPool().getPoolSize();
54          int expectedPoolSize = Integer.parseInt( PropertyLoader.loadProperties( "thread_pool.properties" )
55              .getProperty( "thread_pool.test1.startUpSize" ) );
56          assertEquals( poolSize, expectedPoolSize );
57  
58          // int qs = ((BoundedBuffer)pool.getQueue()).size();
59  
60          int max = pool.getPool().getMaximumPoolSize();
61          System.out.println( max );
62  
63          int expected = Integer.parseInt( PropertyLoader.loadProperties( "thread_pool.properties" )
64              .getProperty( "thread_pool.test1.maximumPoolSize" ) );
65          // "Max should be " + expected",
66          assertEquals( max, expected );
67      }
68  
69      /***
70       * Try to get an undefined pool from an existing default file.
71       */
72      public void testDefaultConfigUndefinedPool()
73      {
74          ThreadPoolManager.setPropsFileName( "thread_pool.properties" );
75          ThreadPoolManager mgr = ThreadPoolManager.getInstance();
76          assertNotNull( mgr );
77  
78          ThreadPool pool = mgr.getPool( "doesnotexist" );
79          assertNotNull( pool );
80  
81          int max = pool.getPool().getMaximumPoolSize();
82          System.out.println( max );
83  
84          int expected = Integer.parseInt( PropertyLoader.loadProperties( "thread_pool.properties" )
85              .getProperty( "thread_pool.default.maximumPoolSize" ) );
86          // "Max should be " + expected",
87          assertEquals( max, expected );
88      }
89  
90      /***
91       * Makes ure we can get a non existent pool from the non exitent config
92       * file.
93       */
94      public void testNonExistentConfigFile()
95      {
96          ThreadPoolManager.setPropsFileName( "somefilethatdoesntexist" );
97          ThreadPoolManager mgr = ThreadPoolManager.getInstance();
98          assertNotNull( mgr );
99  
100         ThreadPool pool = mgr.getPool( "doesntexist" );
101         assertNotNull( "Should have gotten back a pool configured like the default", pool );
102 
103         int max = pool.getPool().getMaximumPoolSize();
104         System.out.println( max );
105 
106         // it will load from the default file
107         int expected = Integer.parseInt( PropertyLoader.loadProperties( "cache.ccf" )
108             .getProperty( "thread_pool.default.maximumPoolSize" ) );
109 
110         // "Max should be " + expected",
111         assertEquals( max, expected );
112     }
113 
114     /***
115      * Get a couple pools by name and then see if they are in the list.
116      *
117      */
118     public void testGetPoolNames()
119     {
120         ThreadPoolManager mgr = ThreadPoolManager.getInstance();
121         assertNotNull( mgr );
122 
123         String poolName1 = "testGetPoolNames1";
124         mgr.getPool( poolName1 );
125 
126         String poolName2 = "testGetPoolNames2";
127         mgr.getPool( poolName2 );
128 
129         ArrayList names = mgr.getPoolNames();
130         assertTrue( "Should have name in list.", names.contains( poolName1 ) );
131         assertTrue( "Should have name in list.", names.contains( poolName2 ) );
132     }
133 
134     /***
135      * Verify that the wait policy gets set correctly.
136      *
137      */
138     public void testWaitPolicyConfig()
139     {
140         ThreadPoolManager.setPropsFileName( "thread_pool.properties" );
141         ThreadPoolManager mgr = ThreadPoolManager.getInstance();
142         // force config from new props file
143         mgr.configure();
144         assertNotNull( mgr );
145 
146         ThreadPool pool = mgr.getPool( "waittest" );
147         assertNotNull( "Should have gotten back a pool.", pool );
148 
149         int max = pool.getPool().getMaximumPoolSize();
150         System.out.println( "testWaitPolicyConfig " + max );
151 
152         // it will load from the default file
153         int expected = Integer.parseInt( PropertyLoader.loadProperties( "thread_pool.properties" )
154             .getProperty( "thread_pool.waittest.maximumPoolSize" ) );
155 
156         // "Max should be " + expected",
157         assertEquals( "Max is wrong", max, expected );
158 
159         PoolConfiguration config = mgr.loadConfig( "thread_pool.waittest" );
160 
161         assertEquals( "Policy is wrong.", PoolConfiguration.POLICY_WAIT, config.getWhenBlockedPolicy() );
162 
163     }
164 
165     /***
166      * Verify that if we specify not to use a buffer boundary that we get a
167      * linked queue.
168      *
169      */
170     public void testNoBoundary()
171     {
172         ThreadPoolManager.setPropsFileName( "thread_pool.properties" );
173         ThreadPoolManager mgr = ThreadPoolManager.getInstance();
174         // force config from new props file
175         mgr.configure();
176         assertNotNull( mgr );
177 
178         ThreadPool pool = mgr.getPool( "nobound" );
179         assertNotNull( "Should have gotten back a pool.", pool );
180 
181         assertTrue( "Should have a linked queue and not a bounded buffer.", pool.getQueue() instanceof LinkedQueue );
182     }
183 
184     /***
185      * Verify that if we specify useBoundary=true that we get a BoundedBuffer.
186      *
187      */
188     public void testWithBoundary()
189     {
190         // SETUP
191         ThreadPoolManager.setPropsFileName( "thread_pool.properties" );
192         ThreadPoolManager mgr = ThreadPoolManager.getInstance();
193         // force config from new props file
194         mgr.configure();
195         assertNotNull( mgr );
196 
197         // DO WORK
198         ThreadPool pool = mgr.getPool( "withbound" );
199 
200         // VERIFY
201         assertNotNull( "Should have gotten back a pool.", pool );
202         assertTrue( "Should have a BoundedBuffer and not a linked queue.", pool.getQueue() instanceof BoundedBuffer );
203     }
204 }