View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.io;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.nio.ByteBuffer;
23  
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  @Category({ SmallTests.class })
31  public class TestBoundedByteBufferPool {
32    final int maxByteBufferSizeToCache = 10;
33    final int initialByteBufferSize = 1;
34    final int maxToCache = 10;
35    BoundedByteBufferPool reservoir;
36  
37    @Before
38    public void before() {
39      this.reservoir =
40        new BoundedByteBufferPool(maxByteBufferSizeToCache, initialByteBufferSize, maxToCache);
41    }
42  
43    @After
44    public void after() {
45      this.reservoir = null;
46    }
47  
48    @Test
49    public void testEquivalence() {
50      ByteBuffer bb = ByteBuffer.allocate(1);
51      this.reservoir.putBuffer(bb);
52      this.reservoir.putBuffer(bb);
53      this.reservoir.putBuffer(bb);
54      assertEquals(3, this.reservoir.buffers.size());
55    }
56  
57    @Test
58    public void testGetPut() {
59      ByteBuffer bb = this.reservoir.getBuffer();
60      assertEquals(initialByteBufferSize, bb.capacity());
61      assertEquals(0, this.reservoir.buffers.size());
62      this.reservoir.putBuffer(bb);
63      assertEquals(1, this.reservoir.buffers.size());
64      // Now remove a buffer and don't put it back so reservoir is empty.
65      this.reservoir.getBuffer();
66      assertEquals(0, this.reservoir.buffers.size());
67      // Try adding in a buffer with a bigger-than-initial size and see if our runningAverage works.
68      // Need to add then remove, then get a new bytebuffer so reservoir internally is doing
69      // allocation
70      final int newCapacity = 2;
71      this.reservoir.putBuffer(ByteBuffer.allocate(newCapacity));
72      assertEquals(1, reservoir.buffers.size());
73      this.reservoir.getBuffer();
74      assertEquals(0, this.reservoir.buffers.size());
75      bb = this.reservoir.getBuffer();
76      assertEquals(newCapacity, bb.capacity());
77      // Assert that adding a too-big buffer won't happen
78      assertEquals(0, this.reservoir.buffers.size());
79      this.reservoir.putBuffer(ByteBuffer.allocate(maxByteBufferSizeToCache * 2));
80      assertEquals(0, this.reservoir.buffers.size());
81      // Assert we can't add more than max allowed instances.
82      for (int i = 0; i < maxToCache; i++) {
83        this.reservoir.putBuffer(ByteBuffer.allocate(initialByteBufferSize));
84      }
85      assertEquals(maxToCache, this.reservoir.buffers.size());
86    }
87  }