View Javadoc
1   package org.apache.commons.jcs.auxiliary.disk.indexed;
2   
3   import org.apache.commons.jcs.auxiliary.disk.DiskTestObject;
4   import org.apache.commons.jcs.engine.behavior.ICacheElement;
5   import org.apache.commons.jcs.utils.timing.SleepUtil;
6   
7   /*
8    * Licensed to the Apache Software Foundation (ASF) under one
9    * or more contributor license agreements.  See the NOTICE file
10   * distributed with this work for additional information
11   * regarding copyright ownership.  The ASF licenses this file
12   * to you under the Apache License, Version 2.0 (the
13   * "License"); you may not use this file except in compliance
14   * with the License.  You may obtain a copy of the License at
15   *
16   *   http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing,
19   * software distributed under the License is distributed on an
20   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21   * KIND, either express or implied.  See the License for the
22   * specific language governing permissions and limitations
23   * under the License.
24   */
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * Tests for the optimization routine.
30   * <p>
31   * @author Aaron Smuts
32   */
33  public class IndexedDiskCacheOptimizationUnitTest
34      extends TestCase
35  {
36      /**
37       * Set the optimize at remove count to 10. Add 20. Check the file size. Remove 10. Check the
38       * times optimized. Check the file size.
39       * @throws Exception
40       */
41      public void testBasicOptimization()
42          throws Exception
43      {
44          // SETUP
45          int removeCount = 50;
46  
47          IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
48          cattr.setCacheName( "testOptimization" );
49          cattr.setMaxKeySize( removeCount * 2 );
50          cattr.setOptimizeAtRemoveCount( removeCount );
51          cattr.setDiskPath( "target/test-sandbox/testOptimization" );
52          IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<Integer, DiskTestObject>( cattr );
53  
54          disk.removeAll();
55  
56          int numberToInsert = removeCount * 3;
57          ICacheElement<Integer, DiskTestObject>[] elements = DiskTestObjectUtil
58              .createCacheElementsWithTestObjectsOfVariableSizes( numberToInsert, cattr.getCacheName() );
59  
60          for ( int i = 0; i < elements.length; i++ )
61          {
62              disk.processUpdate( elements[i] );
63          }
64  
65  
66          Thread.sleep( 1000 );
67          long sizeBeforeRemove = disk.getDataFileSize();
68          // System.out.println( "file sizeBeforeRemove " + sizeBeforeRemove );
69          // System.out.println( "totalSize inserted " + DiskTestObjectUtil.totalSize( elements, numberToInsert ) );
70  
71          // DO WORK
72          for ( int i = 0; i < removeCount; i++ )
73          {
74              disk.processRemove( Integer.valueOf( i ) );
75          }
76  
77          SleepUtil.sleepAtLeast( 1000 );
78  
79          disk.optimizeFile();
80          // VERIFY
81          long sizeAfterRemove = disk.getDataFileSize();
82          long expectedSizeAfterRemove = DiskTestObjectUtil.totalSize( elements, removeCount, elements.length );
83  
84          // test is prone to failure for timing reasons.
85          if ( expectedSizeAfterRemove != sizeAfterRemove )
86          {
87              SleepUtil.sleepAtLeast( 2000 );
88          }
89  
90          assertTrue( "The post optimization size should be smaller."
91                  +"sizeAfterRemove=" + sizeAfterRemove + " sizeBeforeRemove= " +sizeBeforeRemove
92                  , sizeAfterRemove < sizeBeforeRemove );
93          assertEquals( "The file size is not as expected size.", expectedSizeAfterRemove, sizeAfterRemove );
94      }
95  }