View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.io.hfile.slab;
21  
22  import static org.junit.Assert.*;
23  import java.nio.ByteBuffer;
24  
25  import org.apache.hadoop.hbase.SmallTests;
26  import org.junit.*;
27  import org.junit.experimental.categories.Category;
28  
29  /**Test cases for Slab.java*/
30  @Category(SmallTests.class)
31  public class TestSlab {
32    static final int BLOCKSIZE = 1000;
33    static final int NUMBLOCKS = 100;
34    Slab testSlab;
35    ByteBuffer[] buffers = new ByteBuffer[NUMBLOCKS];
36  
37    @Before
38    public void setUp() {
39      testSlab = new Slab(BLOCKSIZE, NUMBLOCKS);
40    }
41  
42    @After
43    public void tearDown() {
44      testSlab.shutdown();
45    }
46  
47    @Test
48    public void testBasicFunctionality() throws InterruptedException {
49      for (int i = 0; i < NUMBLOCKS; i++) {
50        buffers[i] = testSlab.alloc(BLOCKSIZE);
51        assertEquals(BLOCKSIZE, buffers[i].limit());
52      }
53  
54      // write an unique integer to each allocated buffer.
55      for (int i = 0; i < NUMBLOCKS; i++) {
56        buffers[i].putInt(i);
57      }
58  
59      // make sure the bytebuffers remain unique (the slab allocator hasn't
60      // allocated the same chunk of memory twice)
61      for (int i = 0; i < NUMBLOCKS; i++) {
62        buffers[i].putInt(i);
63      }
64  
65      for (int i = 0; i < NUMBLOCKS; i++) {
66        testSlab.free(buffers[i]); // free all the buffers.
67      }
68  
69      for (int i = 0; i < NUMBLOCKS; i++) {
70        buffers[i] = testSlab.alloc(BLOCKSIZE);
71        assertEquals(BLOCKSIZE, buffers[i].limit());
72      }
73    }
74  
75  
76  }
77