1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.testclassification.SmallTests;
26 import org.junit.*;
27 import org.junit.experimental.categories.Category;
28
29
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
55 for (int i = 0; i < NUMBLOCKS; i++) {
56 buffers[i].putInt(i);
57 }
58
59
60
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]);
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