View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.block;
2   
3   import java.util.HashSet;
4   import java.util.Map;
5   import java.util.Set;
6   
7   import org.apache.commons.jcs3.JCS;
8   import org.apache.commons.jcs3.access.CacheAccess;
9   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
10  
11  /*
12   * Licensed to the Apache Software Foundation (ASF) under one
13   * or more contributor license agreements.  See the NOTICE file
14   * distributed with this work for additional information
15   * regarding copyright ownership.  The ASF licenses this file
16   * to you under the Apache License, Version 2.0 (the
17   * "License"); you may not use this file except in compliance
18   * with the License.  You may obtain a copy of the License at
19   *
20   *   http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing,
23   * software distributed under the License is distributed on an
24   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25   * KIND, either express or implied.  See the License for the
26   * specific language governing permissions and limitations
27   * under the License.
28   */
29  
30  import junit.extensions.ActiveTestSuite;
31  import junit.framework.Test;
32  import junit.framework.TestCase;
33  
34  /**
35   * Test which exercises the block disk cache. This one uses three different
36   * regions for three threads.
37   */
38  public class BlockDiskCacheConcurrentUnitTest
39      extends TestCase
40  {
41      /**
42       * Number of items to cache, twice the configured maxObjects for the memory
43       * cache regions.
44       */
45      private static final int items = 200;
46  
47      /**
48       * Constructor for the TestDiskCache object.
49       *
50       * @param testName
51       * @throws Exception
52       */
53      public BlockDiskCacheConcurrentUnitTest( final String testName )
54          throws Exception
55      {
56          super( testName );
57      }
58  
59      /**
60       * A unit test suite for JUnit
61       *
62       * @return The test suite
63       * @throws Exception
64       */
65      public static Test suite()
66          throws Exception
67      {
68          final ActiveTestSuite suite = new ActiveTestSuite();
69  
70          JCS.setConfigFilename( "/TestBlockDiskCache.ccf" );
71          JCS.getInstance( "indexedRegion1" ).clear();
72          JCS.getInstance( "indexedRegion2" ).clear();
73          JCS.getInstance( "indexedRegion3" ).clear();
74  
75          suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache1" )
76          {
77              @Override
78              public void runTest()
79                  throws Exception
80              {
81                  this.runTestForRegion( "indexedRegion1" );
82              }
83          } );
84  
85          suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache2" )
86          {
87              @Override
88              public void runTest()
89                  throws Exception
90              {
91                  this.runTestForRegion( "indexedRegion2" );
92              }
93          } );
94  
95          suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache3" )
96          {
97              @Override
98              public void runTest()
99                  throws Exception
100             {
101                 this.runTestForRegion( "indexedRegion3" );
102             }
103         } );
104 
105         suite.addTest( new BlockDiskCacheConcurrentUnitTest( "testBlockDiskCache4" )
106         {
107             @Override
108             public void runTest()
109                 throws Exception
110             {
111                 this.runTestForRegionInRange( "indexedRegion3", 300, 600 );
112             }
113         } );
114 
115         return suite;
116     }
117 
118     /**
119      * Test setup
120      */
121     @Override
122     public void setUp()
123     {
124         JCS.setConfigFilename( "/TestBlockDiskCache.ccf" );
125     }
126 
127     /**
128      * Adds items to cache, gets them, and removes them. The item count is more
129      * than the size of the memory cache, so items should spool to disk.
130      *
131      * @param region
132      *            Name of the region to access
133      *
134      * @throws Exception
135      *                If an error occurs
136      */
137     public void runTestForRegion( final String region )
138         throws Exception
139     {
140         final CacheAccess<String, String> jcs = JCS.getInstance( region );
141 
142         // Add items to cache
143         for ( int i = 0; i < items; i++ )
144         {
145             jcs.put( i + ":key", region + " data " + i );
146         }
147 
148         // Test that all items are in cache
149         for ( int i = 0; i < items; i++ )
150         {
151             final String value = jcs.get( i + ":key" );
152 
153             assertEquals( region + " data " + i, value );
154         }
155 
156         // Test that getElements returns all the expected values
157         final Set<String> keys = new HashSet<>();
158         for ( int i = 0; i < items; i++ )
159         {
160             keys.add( i + ":key" );
161         }
162 
163         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
164         for ( int i = 0; i < items; i++ )
165         {
166             final ICacheElement<String, String> element = elements.get( i + ":key" );
167             assertNotNull( "element " + i + ":key is missing", element );
168             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
169         }
170 
171         // Remove all the items
172         for ( int i = 0; i < items; i++ )
173         {
174             jcs.remove( i + ":key" );
175         }
176 
177         // Verify removal
178         // another thread may have inserted since
179         for ( int i = 0; i < items; i++ )
180         {
181             assertNull( "Removed key should be null: " + i + ":key" + "\n stats " + jcs.getStats(), jcs
182                 .get( i + ":key" ) );
183         }
184     }
185 
186     /**
187      * Adds items to cache, gets them, and removes them. The item count is more
188      * than the size of the memory cache, so items should spool to disk.
189      *
190      * @param region
191      *            Name of the region to access
192      * @param start
193      * @param end
194      *
195      * @throws Exception
196      *                If an error occurs
197      */
198     public void runTestForRegionInRange( final String region, final int start, final int end )
199         throws Exception
200     {
201         final CacheAccess<String, String> jcs = JCS.getInstance( region );
202 
203         // Add items to cache
204         for ( int i = start; i < end; i++ )
205         {
206             jcs.put( i + ":key", region + " data " + i );
207         }
208 
209         // Test that all items are in cache
210         for ( int i = start; i < end; i++ )
211         {
212             final String value = jcs.get( i + ":key" );
213 
214             assertEquals( region + " data " + i, value );
215         }
216 
217         // Test that getElements returns all the expected values
218         final Set<String> keys = new HashSet<>();
219         for ( int i = start; i < end; i++ )
220         {
221             keys.add( i + ":key" );
222         }
223 
224         final Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
225         for ( int i = start; i < end; i++ )
226         {
227             final ICacheElement<String, String> element = elements.get( i + ":key" );
228             assertNotNull( "element " + i + ":key is missing", element );
229             assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
230         }
231 
232         // Remove all the items
233         for ( int i = start; i < end; i++ )
234         {
235             jcs.remove( i + ":key" );
236         }
237 
238 //        System.out.println( jcs.getStats() );
239 
240         // Verify removal
241         // another thread may have inserted since
242         for ( int i = start; i < end; i++ )
243         {
244             assertNull( "Removed key should be null: " + i + ":key " + "\n stats " + jcs.getStats(), jcs.get( i
245                 + ":key" ) );
246         }
247     }
248 }