View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.indexed;
2   
3   import org.apache.commons.jcs3.auxiliary.disk.DiskTestObject;
4   import org.apache.commons.jcs3.utils.timing.SleepUtil;
5   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
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   */
31  public class IndexedDiskCacheOptimizationUnitTest
32      extends TestCase
33  {
34      /**
35       * Set the optimize at remove count to 10. Add 20. Check the file size. Remove 10. Check the
36       * times optimized. Check the file size.
37       * @throws Exception
38       */
39      public void testBasicOptimization()
40          throws Exception
41      {
42          // SETUP
43          final int removeCount = 50;
44  
45          final IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
46          cattr.setCacheName( "testOptimization" );
47          cattr.setMaxKeySize( removeCount * 2 );
48          cattr.setOptimizeAtRemoveCount( removeCount );
49          cattr.setDiskPath( "target/test-sandbox/testOptimization" );
50          final IndexedDiskCache<Integer, DiskTestObject> disk = new IndexedDiskCache<>( cattr );
51  
52          disk.removeAll();
53  
54          final int numberToInsert = removeCount * 3;
55          final ICacheElement<Integer, DiskTestObject>[] elements = DiskTestObjectUtil
56              .createCacheElementsWithTestObjectsOfVariableSizes( numberToInsert, cattr.getCacheName() );
57  
58          for (final ICacheElement<Integer, DiskTestObject> element : elements) {
59              disk.processUpdate( element );
60          }
61  
62  
63          Thread.sleep( 1000 );
64          final long sizeBeforeRemove = disk.getDataFileSize();
65          // System.out.println( "file sizeBeforeRemove " + sizeBeforeRemove );
66          // System.out.println( "totalSize inserted " + DiskTestObjectUtil.totalSize( elements, numberToInsert ) );
67  
68          // DO WORK
69          for ( int i = 0; i < removeCount; i++ )
70          {
71              disk.processRemove( Integer.valueOf( i ) );
72          }
73  
74          SleepUtil.sleepAtLeast( 1000 );
75  
76          disk.optimizeFile();
77          // VERIFY
78          final long sizeAfterRemove = disk.getDataFileSize();
79          final long expectedSizeAfterRemove = DiskTestObjectUtil.totalSize( elements, removeCount, elements.length );
80  
81          // test is prone to failure for timing reasons.
82          if ( expectedSizeAfterRemove != sizeAfterRemove )
83          {
84              SleepUtil.sleepAtLeast( 2000 );
85          }
86  
87          assertTrue( "The post optimization size should be smaller."
88                  +"sizeAfterRemove=" + sizeAfterRemove + " sizeBeforeRemove= " +sizeBeforeRemove
89                  , sizeAfterRemove < sizeBeforeRemove );
90          assertEquals( "The file size is not as expected size.", expectedSizeAfterRemove, sizeAfterRemove );
91      }
92  }