001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.util; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import java.nio.ByteBuffer; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.nio.ByteBuff; 028import org.apache.hadoop.hbase.testclassification.MiscTests; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 030import org.junit.ClassRule; 031import org.junit.Test; 032import org.junit.experimental.categories.Category; 033 034@Category({MiscTests.class, SmallTests.class}) 035public class TestByteBufferArray { 036 037 @ClassRule 038 public static final HBaseClassTestRule CLASS_RULE = 039 HBaseClassTestRule.forClass(TestByteBufferArray.class); 040 041 @Test 042 public void testAsSubBufferWhenEndOffsetLandInLastBuffer() throws Exception { 043 int capacity = 4 * 1024 * 1024; 044 ByteBufferAllocator allocator = new ByteBufferAllocator() { 045 @Override 046 public ByteBuffer allocate(long size) throws IOException { 047 return ByteBuffer.allocateDirect((int) size); 048 } 049 }; 050 ByteBufferArray array = new ByteBufferArray(capacity, allocator); 051 ByteBuff subBuf = array.asSubByteBuff(0, capacity); 052 subBuf.position(capacity - 1);// Position to the last byte 053 assertTrue(subBuf.hasRemaining()); 054 // Read last byte 055 subBuf.get(); 056 assertFalse(subBuf.hasRemaining()); 057 } 058 059 @Test 060 public void testByteBufferCreation() throws Exception { 061 int capacity = 470 * 1021 * 1023; 062 ByteBufferAllocator allocator = new ByteBufferAllocator() { 063 @Override 064 public ByteBuffer allocate(long size) throws IOException { 065 return ByteBuffer.allocateDirect((int) size); 066 } 067 }; 068 ByteBufferArray array = new ByteBufferArray(capacity, allocator); 069 assertEquals(119, array.buffers.length); 070 for (int i = 0; i < array.buffers.length; i++) { 071 if (i == array.buffers.length - 1) { 072 assertEquals(0, array.buffers[i].capacity()); 073 } else { 074 assertEquals(ByteBufferArray.DEFAULT_BUFFER_SIZE, array.buffers[i].capacity()); 075 } 076 } 077 } 078 079 @Test 080 public void testByteBufferCreation1() throws Exception { 081 ByteBufferAllocator allocator = new ByteBufferAllocator() { 082 @Override 083 public ByteBuffer allocate(long size) throws IOException { 084 return ByteBuffer.allocateDirect((int) size); 085 } 086 }; 087 ByteBufferArray array = new DummyByteBufferArray(7 * 1024 * 1024, allocator); 088 // overwrite 089 array.bufferCount = 25; 090 array.buffers = new ByteBuffer[array.bufferCount + 1]; 091 array.createBuffers(allocator); 092 for (int i = 0; i < array.buffers.length; i++) { 093 if (i == array.buffers.length - 1) { 094 assertEquals(0, array.buffers[i].capacity()); 095 } else { 096 assertEquals(458752, array.buffers[i].capacity()); 097 } 098 } 099 } 100 101 private static class DummyByteBufferArray extends ByteBufferArray { 102 103 public DummyByteBufferArray(long capacity, ByteBufferAllocator allocator) throws IOException { 104 super(capacity, allocator); 105 } 106 107 @Override 108 int getThreadCount() { 109 return 16; 110 } 111 } 112}