1   /**
2    * Copyright 2011 The Apache Software Foundation
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, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.io.hfile.slab;
21  
22  import org.apache.hadoop.hbase.io.hfile.CacheTestUtils;
23  import org.junit.After;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  /**
28   * Tests SingleSlabCache.
29   * <p>
30   *
31   * Tests will ensure that evictions operate when they're supposed to and do what
32   * they should, and that cached blocks are accessible when expected to be.
33   */
34  public class TestSingleSizeCache {
35    SingleSizeCache cache;
36    final int CACHE_SIZE = 1000000;
37    final int NUM_BLOCKS = 100;
38    final int BLOCK_SIZE = CACHE_SIZE / NUM_BLOCKS;
39    final int NUM_THREADS = 100;
40    final int NUM_QUERIES = 10000;
41  
42    @Before
43    public void setup() {
44      cache = new SingleSizeCache(BLOCK_SIZE, NUM_BLOCKS, null);
45    }
46  
47    @After
48    public void tearDown() {
49      cache.shutdown();
50    }
51  
52    @Test
53    public void testCacheSimple() throws Exception {
54      CacheTestUtils.testCacheSimple(cache, BLOCK_SIZE, NUM_QUERIES);
55    }
56  
57    @Test
58    public void testCacheMultiThreaded() throws Exception {
59      CacheTestUtils.testCacheMultiThreaded(cache, BLOCK_SIZE,
60          NUM_THREADS, NUM_QUERIES, 0.80);
61    }
62  
63    @Test
64    public void testCacheMultiThreadedSingleKey() throws Exception {
65      CacheTestUtils.hammerSingleKey(cache, BLOCK_SIZE, NUM_THREADS, NUM_QUERIES);
66    }
67  
68    @Test
69    public void testCacheMultiThreadedEviction() throws Exception {
70      CacheTestUtils.hammerEviction(cache, BLOCK_SIZE, NUM_THREADS, NUM_QUERIES);
71    }
72  
73    @Test
74    public void testHeapSizeChanges(){
75      CacheTestUtils.testHeapSizeChanges(cache, BLOCK_SIZE);
76    }
77  
78  }