View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.indexed;
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 junit.framework.TestCase;
23  
24  import org.apache.commons.jcs3.access.TestCacheAccess;
25  import org.apache.commons.jcs3.JCS;
26  import org.apache.commons.jcs3.access.CacheAccess;
27  
28  /**
29   * This is used by other tests to generate a random load on the disk cache.
30   */
31  public class IndexedDiskCacheRandomConcurrentTestUtil
32      extends TestCase
33  {
34  
35      /**
36       * Constructor for the TestDiskCache object.
37       *
38       * @param testName
39       */
40      public IndexedDiskCacheRandomConcurrentTestUtil( final String testName )
41      {
42          super( testName );
43      }
44  
45      /**
46       * Randomly adds items to cache, gets them, and removes them. The range
47       * count is more than the size of the memory cache, so items should spool to
48       * disk.
49       *
50       * @param region
51       *            Name of the region to access
52       * @param range
53       * @param numOps
54       * @param testNum
55       *
56       * @throws Exception
57       *                If an error occurs
58       */
59      public void runTestForRegion( final String region, final int range, final int numOps, final int testNum )
60          throws Exception
61      {
62          // run a rondom operation test to detect deadlocks
63          final TestCacheAccess tca = new TestCacheAccess( "/TestDiskCacheCon.ccf" );
64          tca.setRegion( region );
65          tca.random( range, numOps );
66  
67          // make sure a simple put then get works
68          // this may fail if the other tests are flooding the disk cache
69          final CacheAccess<String, String> jcs = JCS.getInstance( region );
70          final String key = "testKey" + testNum;
71          final String data = "testData" + testNum;
72          jcs.put( key, data );
73          final String value = jcs.get( key );
74          assertEquals( data, value );
75  
76      }
77  
78      /**
79       * Test setup
80       */
81      @Override
82      public void setUp()
83      {
84          JCS.setConfigFilename( "/TestDiskCacheCon.ccf" );
85      }
86  
87  }