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
21 package org.apache.hadoop.hbase.io.hfile.slab;
22
23 import static org.junit.Assert.*;
24 import java.nio.ByteBuffer;
25 import org.junit.*;
26
27 /**Test cases for Slab.java*/
28 public class TestSlab {
29 static final int BLOCKSIZE = 1000;
30 static final int NUMBLOCKS = 100;
31 Slab testSlab;
32 ByteBuffer[] buffers = new ByteBuffer[NUMBLOCKS];
33
34 @Before
35 public void setUp() {
36 testSlab = new Slab(BLOCKSIZE, NUMBLOCKS);
37 }
38
39 @After
40 public void tearDown() {
41 testSlab.shutdown();
42 }
43
44 @Test
45 public void testBasicFunctionality() throws InterruptedException {
46 for (int i = 0; i < NUMBLOCKS; i++) {
47 buffers[i] = testSlab.alloc(BLOCKSIZE);
48 assertEquals(BLOCKSIZE, buffers[i].limit());
49 }
50
51 // write an unique integer to each allocated buffer.
52 for (int i = 0; i < NUMBLOCKS; i++) {
53 buffers[i].putInt(i);
54 }
55
56 // make sure the bytebuffers remain unique (the slab allocator hasn't
57 // allocated the same chunk of memory twice)
58 for (int i = 0; i < NUMBLOCKS; i++) {
59 buffers[i].putInt(i);
60 }
61
62 for (int i = 0; i < NUMBLOCKS; i++) {
63 testSlab.free(buffers[i]); // free all the buffers.
64 }
65
66 for (int i = 0; i < NUMBLOCKS; i++) {
67 buffers[i] = testSlab.alloc(BLOCKSIZE);
68 assertEquals(BLOCKSIZE, buffers[i].limit());
69 }
70 }
71
72 }